Android: Simple Password Generator

Submitted by: 
Language: 
Visitors have accessed this post 317 times.

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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplepaswordgenerator.MainActivity">
  11.  
  12. <TextView
  13. android:id="@+id/tv_password"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:fontFamily="monospace"
  17. android:textSize="40sp"
  18. android:gravity="center"
  19. android:layout_centerInParent="true"
  20. android:layout_marginBottom="40dp"
  21. android:hint="Password"/>
  22. android:id="@+id/btn_generate"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_centerInParent="true"
  26. android:layout_below="@+id/tv_password"
  27. android:text="Generate"/>
  28.  
  29. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplepaswordgenerator">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23.  
  24. </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!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.