+91-7906818313 / +91-7500893874 Monday - Sunday 10:00am - 06:00pm
How to Create Random Number Generator in Android

How to Create Random Number Generator in Android

How to Create Random Number Generator in Android

 

If you want to create an application which can generate random numbers then you should read this article. Here you can get all information about random number generator in android with all xml code and java code. Here you will also get output of this application.



activity_main.xml of Random Number Generator in Android

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="sumit.net.random.MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Generated Number"
        android:textSize="75sp"
        android:textColor="#ff0000"
        >
    </TextView>
    <Button
        android:id="@+id/push_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Here"
        android:textSize="20sp"
        android:layout_below="@id/text_view"
        >
    </Button>

</RelativeLayout>




MainActivity.java of Random Number Generator in Android

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv = (TextView) findViewById(R.id.text_view);
    final Button b=(Button)findViewById(R.id.push_button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Random r = new Random();
                        int randomNumber = r.nextInt(100);
                        tv.setText(String.valueOf(randomNumber));
                    }
                },2000);

            }
        });

    }
}

The output of Random Number Generator in Android

random number generator             random number generator



This Post Has 2 Comments

  1. Thank you for this valuable information.

Leave a Reply

Close Menu
Translate »