Random numbers between 1 to 100 Java android program with source code.
Android gives us random Java class which provides us pseudo random values. So developer can set values limit in function and generate random will be between them. So here is the complete step by step tutorial for Generate unique random number android example with code.
Generate unique random number android example with code.
Code for MainActivity.java file.
package com.randomnumber_android_examples.com; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { TextView RandomNumber; Button GenRan; Random Number; int Rnumber; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RandomNumber = (TextView)findViewById(R.id.textView1); GenRan = (Button)findViewById(R.id.button1); GenRan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Number = new Random(); Rnumber = Number.nextInt(100); RandomNumber.setText(Integer.toString(Rnumber)); } }); } }
Code for activity_main.xml layout file.
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.randomnumber_android_examples.com.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Ramdom Number Display Here" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="35dp" android:text="Click Here to Generate Random Number between 1 to 100" /> </RelativeLayout>
Screenshot: