Android: Simple Password Generator

In this tutorial we will try to create a Simple Password Generator Using Android. Android is most commonly comes installed on a variety of smartphones and tablets, and even in TV. Android is an open source so that developer find it easy to develop and expand new features. So let's do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now add some layout to the application. Just kindly copy the code below and paste it inside the code editor
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.razormist.simplepaswordgenerator.MainActivity">
  8.  
  9.     <TextView
  10.         android:id="@+id/tv_password"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="wrap_content"
  13.         android:fontFamily="monospace"
  14.         android:textSize="40sp"
  15.         android:gravity="center"
  16.         android:layout_centerInParent="true"
  17.         android:layout_marginBottom="40dp"
  18.         android:hint="Password"/>
  19.     <Button
  20.         android:id="@+id/btn_generate"
  21.         android:layout_width="wrap_content"
  22.         android:layout_height="wrap_content"
  23.         android:layout_centerInParent="true"
  24.         android:layout_below="@+id/tv_password"
  25.         android:text="Generate"/>
  26.  
  27. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.razormist.simplepaswordgenerator">
  4.  
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/AppTheme">
  12.         <activity android:name=".MainActivity"
  13.             android:configChanges="orientation"
  14.             android:screenOrientation="portrait">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />
  17.  
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.     </application>
  22.  
  23. </manifest>

The Main Function

This code contains the main function of the application. This code will randomly display a certain character the generate button is clicked.
  1. package com.razormist.simplepaswordgenerator;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8.  
  9. import java.util.Random;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13.     TextView tv_passowrd;
  14.     Button btn_generate;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.  
  21.         tv_passowrd = (TextView)findViewById(R.id.tv_password);
  22.         btn_generate = (Button)findViewById(R.id.btn_generate);
  23.  
  24.  
  25.         btn_generate.setOnClickListener(new View.OnClickListener() {
  26.             @Override
  27.             public void onClick(View v) {
  28.                 int length = 12;
  29.  
  30.                 tv_passowrd.setText(GetPassword(length));
  31.  
  32.             }
  33.         });
  34.  
  35.     }
  36.  
  37.     public String GetPassword(int length){
  38.         char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
  39.         StringBuilder stringBuilder = new StringBuilder();
  40.  
  41.         Random rand = new Random();
  42.  
  43.         for(int i = 0; i < length; i++){
  44.             char c = chars[rand.nextInt(chars.length)];
  45.             stringBuilder.append(c);
  46.         }
  47.  
  48.         return stringBuilder.toString();
  49.     }
  50. }
There you have it we successfully created a Simple Password Generator using Android. I hope that this simple tutorial help you to your projects. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Tags

Comments

good job

Add new comment