How to Generate a Random Number From a Range in Android?

Many libraries for Android enable developers to build meaningful applications by giving them access to mathematical and logical functions. The random function, which produces a random value, is one example of such a function. But, we can modify this function such that it finds the value from a range of numbers rather than just at random.

In this article, you will learn how to create your Random in Android. So let’s take example of QR codes  in Android of Mobile application.

 Step-by-Step Implementation
 Step 1: Create a New Project in Android ​studio (File >new  project). 


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file. 

 <?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=".MainActivity">

<EditText
android:id="@+id/edit_text_1"
android:layout_width="100sp"
android:layout_height="50sp"
android:layout_toStartOf="@id/button_1"
android:layout_above="@id/button_1"
android:layout_marginBottom="20sp"
android:inputType="number"
android:hint="Min."/>

<EditText
android:id="@+id/edit_text_2"
android:layout_width="100sp"
android:layout_height="50sp"
android:layout_toEndOf="@id/button_1"
android:layout_above="@id/button_1"
android:layout_marginBottom="20sp"
android:inputType="number"
android:hint="Max."/>

<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:layout_centerInParent="true"/>

<TextView
android:id="@+id/text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_below="@id/button_1"
android:layout_marginTop="20sp"
android:hint="Result"/>

</RelativeLayout>
Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​lowing code inside MainActivity.kt file.

 import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val mEditText1 = findViewById<EditText>(R.id.edit_text_1)
val mEditText2 = findViewById<EditText>(R.id.edit_text_2)
val mButton = findViewById<Button>(R.id.button_1)
val mTextView = findViewById<TextView>(R.id.text_view_1)

mButton.setOnClickListener {
val minVal = mEditText1.text.toString().toInt()
val maxVal = mEditText2.text.toString().toInt()
val mResult = (minVal..maxVal).random()
mTextView.text = mResult.toString()
}
}
}

Step 4 : Output of above example.

                                         

Happy coding!


365Bloggy July 16, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive