Tutorial how to generate random number between 0 and 99 in Android Studio 3.3.1. Create new project with minimum SDK: API 15Android 4.0.3 (IceCreamSandwich). In app->res->layout->activity_main.xml write the code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@+id/generate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Generate random number"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/generatenumber"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:maxLines="1"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
In app->java->MainActivity.java write the code:
package com.example.myapplication;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Random myRandom = new Random();


        Button buttonGenerate = (Button)findViewById(R.id.generate);
        final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);

        buttonGenerate.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                textGenerateNumber.setText(String.valueOf(myRandom.nextInt(100)));
            }
        });
    }
}
Run app in mobile device or Emulator (API 15 – Android 4.0.3) and see how it works. Follow video tutorial: Download this app for free and install it on your mobile device to see how it works: http://android-coffee.com/wp-content/uploads/projecs/Random-Number-Generator.apk Download project with Source Code from here: http://android-coffee.com/wp-content/uploads/projecs/Random-Number-Generator.7z Please subscribe here to our YouTube channel. Thank you! Check next tutorials to learn all about Android. For further questions leave a message.

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image Refresh

*