• We just launched and are currently in beta. Join us as we build and grow the community.

Android: Simple Password Generator

preinix

Traffic Strategist
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
68
Likes
57
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <

    TextView
  9. android:

    id=

    "@+id/tv_password"
  10. android:

    layout_width=

    "match_parent"
  11. android:

    layout_height=

    "wrap_content"
  12. android:

    fontFamily=

    "monospace"
  13. android:

    textSize=

    "40sp"
  14. android:

    gravity=

    "center"
  15. android:

    layout_centerInParent=

    "true"
  16. android:

    layout_marginBottom=

    "40dp"
  17. android:

    hint=

    "Password"

    />
  18. <

    Button


  19. android:

    id=

    "@+id/btn_generate"
  20. android:

    layout_width=

    "wrap_content"
  21. android:

    layout_height=

    "wrap_content"
  22. android:

    layout_centerInParent=

    "true"
  23. android:

    layout_below=

    "@+id/tv_password"
  24. android:

    text=

    "Generate"

    />

  25. </

    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. <

    application
  5. android:

    allowBackup=

    "true"
  6. android:

    icon=

    "@mipmap/ic_launcher"
  7. android:

    label=

    "@string/app_name"
  8. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  9. android:

    supportsRtl=

    "true"
  10. android:

    theme=

    "@Style/AppTheme"

    >
  11. <

    activity android:

    name=

    ".MainActivity"
  12. android:

    configChanges=

    "orientation"
  13. android:

    screenOrientation=

    "portrait"

    >
  14. <

    intent-

    filter>
  15. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  16. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  17. </

    intent-

    filter>
  18. </

    activity>
  19. </

    application>

  20. </

    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. import

    android.support.v7.app.AppCompatActivity

    ;
  3. import

    android.os.Bundle

    ;
  4. import

    android.view.View

    ;
  5. import

    android.widget.Button

    ;
  6. import

    android.widget.TextView

    ;

  7. import

    java.util.Random

    ;

  8. public

    class

    MainActivity extends

    AppCompatActivity {

  9. TextView tv_passowrd;
  10. Button

    btn_generate;

  11. @Override
  12. protected

    void

    onCreate(

    Bundle savedInstanceState)

    {
  13. super

    .onCreate

    (

    savedInstanceState)

    ;
  14. setContentView(

    R.layout

    .activity_main

    )

    ;

  15. tv_passowrd =

    (

    TextView)

    findViewById(

    R.id

    .tv_password

    )

    ;
  16. btn_generate =

    (

    Button

    )

    findViewById(

    R.id

    .btn_generate

    )

    ;


  17. btn_generate.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  18. @Override
  19. public

    void

    onClick(

    View

    v)

    {
  20. int

    length =

    12

    ;

  21. tv_passowrd.setText

    (

    GetPassword(

    length)

    )

    ;

  22. }
  23. }

    )

    ;

  24. }

  25. public

    String

    GetPassword(

    int

    length)

    {
  26. char

    [

    ]

    chars =

    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

    .toCharArray

    (

    )

    ;
  27. StringBuilder stringBuilder =

    new

    StringBuilder(

    )

    ;

  28. Random

    rand =

    new

    Random

    (

    )

    ;

  29. for

    (

    int

    i =

    0

    ;

    i <

    length;

    i++

    )

    {
  30. char

    c =

    chars[

    rand.nextInt

    (

    chars.length

    )

    ]

    ;
  31. stringBuilder.append

    (

    c)

    ;
  32. }

  33. return

    stringBuilder.toString

    (

    )

    ;
  34. }
  35. }

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!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

329,696

329,704

Top