A simple Fact app Using Random Class in Android tutorial is guiding you to generate some facts about India already stored in the array are displayed randomly when you click on the button. When the facts are displayed, the background colors are also changing randomly.
Random Number Generation in Android
An instance of Random class is used to generate a stream of pseudorandom numbers. For example I need to make a random number between 1 and 20, use the following code for generation value between 0 and 20.
Random Class In Android
Random rand = new Random(); int n = rand.nextInt(20); // Gives n such that 0 <= n < 20
The random class in android returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence. Thus, from above example code, we’ll have a number between 0 and 19.
For a beginner to learn about installation of Android studio in Ubuntu and Creation of sample projects using android studio click here.
Create a new android project in android studio by click on File>New>New Project>Empty Activity. Give a name to your android application. Here I have given a name to my android application as funfactappwithdifferentcolor.
Android Layout
Here I have created a Button to generate different facts about India when the user clicks on the button. At the same time the background color is also changing. Here I just created an array for storing the facts and also created an array for storing the colors. When the user clicks on the button, one of the fact from the stored array and color from the array is returned.
Modify the activity_main.xml file as follows.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="KNOWN FACTS ABOUT INDIA" android:id="@+id/textView" android:textSize="40dp" android:textColor="@color/colorAccent"/> <Button android:text="Click Here To Know Some Facts About India" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="56dp" android:id="@+id/button1" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:textColor="@color/colorAccent"/> <TextView android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:textSize="40dp"/> </RelativeLayout>
Activity Classes
The code for the MainActivity.java class is shown below.
MainActivity.java
package com.example.funfactappwithdifferentcolor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button btn; TextView txt1; RelativeLayout r1; private FactBook mFactBook=new FactBook(); private ColorWheel cColorWheel=new ColorWheel(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn= (Button) findViewById(R.id.button1); txt1= (TextView) findViewById(R.id.textView2); r1= (RelativeLayout) findViewById(R.id.activity_main); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Toast.makeText(getApplication(),"new facts",Toast.LENGTH_SHORT).show(); String fact=mFactBook.getFact(); txt1.setText(fact); int color=cColorWheel.getColor(); r1.setBackgroundColor(color); btn.setTextColor(color); } }); } }
Create two new java class by right click on the Java>com.example.funfactappwithdifferentcolor , that appears on the left side of your android studio as shown below.
Give a name to your new java class. Here I named FactBook.java, modify this java class as follows.
FactBook.java
package com.example.funfactappwithdifferentcolor; import java.util.Random; public class FactBook { public String[] mFacts = {"India has a total of 29 states and 7 union territories", "The capital of India is New Delhi.", "A citizen of India, who is 18 years old or above, is entitled to vote.", "The Telephone Country Code of India is +91.", "National Fruit-Mango ", "National Flower-Lotus", "National Currency-Indian Rupee ", "National Sport-Hockey", "National Bird-Peacock ","National Animal-Royal Bengal Tiger","National Anthem-Jana Gana Mana, written by Rabindranath Tagore ","Population Density-382 persons per square kilometer ","Official Name-Bharat (Sanskrit), Republic of India (English)","Population-1,210,569,573 (2011 Census) ","India is the largest and the oldest civilisation in the world","Mahatma Gandhi was assassinated on 30 January 1948.","Ayurveda, which is the earliest school of medicine in the world, was born in India thousands of years ago","The Indian National Congress was formed in the year 1885","India has got a long coastline of 7,516.6 km","India is the largest producer of banana in the world","RBI is the only authority in India that can issue currency notes","Odisha is one of the major producers of graphite in India.","The first Special Economic Zone (SEZ) was established in Kandla in Gujarat in the year 1965.","India happens to be the second largest English speaking country in the world","The country's largest news agency is Press Trust of India (PTI)","Indian Railways is the world's eighth largest employer, employing over 1.4 million people","India is the first country in the world to enter the Martian orbit in its very first attempt","India is the largest producer of milk in the world.","It is four times the size of Pakistan, 13 times the size of UK and nine times the size of Japan","India has the largest number of Post Offices in the world.","Chess was invented in India.","India never invaded any country in her last 100000 years of history.","Algebra, Trigonometry and Calculus are studies, which originated in India","The 'Place Value System' and the 'Decimal System' were developed in India in 100 B.C.","Until 1896, India was the only source of diamonds in the world ","Islam is India's and the world's second largest religion.","There are 300,000 active mosques in India, more than in any other country, including the Muslim world.","Jews and Christians have lived continuously in India since 200 B.C. and 52 A.D. respectively.","Yoga has its origins in India and has existed for over 5,000 years.","Martial Arts were first created in India, and later spread to Asia by Buddhist missionaries.","The four religions born in India - Hinduism, Buddhism, Jainism, and Sikhism, are followed by 25% of the world's population.","India exports software to 90 countries."," The highest award in the country is Bharat Ratna.","India has never hosted the Olympic Games.","The largest dam is the Hirakud Dam in Orrisa.","The world’s highest battlefield (Siachin) is controlled by India.","The British Raj, or British rule, lasted from 1858 to 1947.","Takshila, the first ever university in the world started around 700 BC in India.","Apart from the U.S. and Japan, only India has built a super computer indigenously."}; public String getFact() { String fact = ""; Random rm = new Random(); int rn = rm.nextInt(mFacts.length); fact = mFacts[rn]; return fact; } }
Create another java class as above and also give a name to the newly created java class. Here I have named java class is ColorWheel.java.
ColorWheel.java
package com.example.funfactappwithdifferentcolor; import android.graphics.Color; import java.util.Random; public class ColorWheel { public String[] mColor={"#e84a5f","#008000","#008080","#0000FF"}; public int getColor(){ Random rm=new Random(); int rn=rm.nextInt(mColor.length); String color=mColor[rn]; int colorAsInt= Color.parseColor(color); return colorAsInt; } }