Thursday, April 4, 2013

Android Generate Random String

Android Generate Random String

This post about how to  generate random char using Random class.

Random random = new Random();
This is list of char 

private static final String _CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random string length
private static final int RANDOM_STR_LENGTH = 12;
This method for generate only numbers

private int getRandomNumber() {
            int randomInt = 0;
            randomInt = random.nextInt(_CHAR.length());
            if (randomInt - 1 == -1) {
                  return randomInt;
            } else {
                  return randomInt - 1;
            }
      }
This method for generate numbers with char.
    public String getRandomString(){

            StringBuffer randStr = new StringBuffer();

            for (int i = 0; i < RANDOM_STR_LENGTH; i++) {

                  int number = getRandomNumber();
                  char ch = _CHAR.charAt(number);
                  randStr.append(ch);
            }
            return randStr.toString();
      }
  

Screen Shot
Full Source Code



package com.androidbunch.generaterandomnumber;

import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


/**
 *
 * @author VIJAYAKUMAR M
 *
 */
public class MainActivity extends Activity {
      Random random = new Random();
      private static final String _CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
      private static final int RANDOM_STR_LENGTH = 12;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = (Button) findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                        TextView text_Random = (TextView) findViewById(R.id.textView1);
                        text_Random.setText(getRandomString());
                  }
            });
      
    }
    public String getRandomString(){
            StringBuffer randStr = new StringBuffer();
            for (int i = 0; i < RANDOM_STR_LENGTH; i++) {
                  int number = getRandomNumber();
                  char ch = _CHAR.charAt(number);
                  randStr.append(ch);
            }
            return randStr.toString();
      }

      private int getRandomNumber() {
            int randomInt = 0;
            randomInt = random.nextInt(_CHAR.length());
            if (randomInt - 1 == -1) {
                  return randomInt;
            } else {
                  return randomInt - 1;
            }
      }
}


Layout source code


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginTop="58dp"
        android:text="Generate Random String" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:textColor="#000000"
        android:textSize="10pt"
        android:textStyle="bold" />

</RelativeLayout>


 

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...