In this blog we create a Lottery application in Android Studio that simulates a lottery game. The application allows the player to type a number to generate five numbers and one red ball. The constructor will use the Random class to generate a random number in the range of 1 through 69 for the red ball 1 through 26.
Download Android Studio from the link given blow according to your operating system Windows, Mac, Linux, Chrome OS etc.
Link: https://developer.android.com/studio
More downloads are available here.
Link: https://developer.android.com/studio/archive
For this application we are using the API 16 and an empty activity naming the activity as MainActivity we choose finish:
Firstly we will design the user interface after that we will go for MainActivity.java.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:orientation="vertical"
// you can add any kind of logo if you want by changing from android view to project view//
// add the required fields i.e. text field, button from design, and go to text to edit the texts//
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:textSize="36sp"
android:layout_marginTop="3pt"
android:string="@string/Lottery"
android:gravity="center_horizontal"
android:fontFamily=”casual” />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="3pt">
<Textview
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/firstnumber"
android:layout_width="match_parent"
android:inputType="numberDecimal">
android:text="01" />
<Textview
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/secondnumber"
android:layout_width="match_parent"
android:inputType="numberDecimal">
android:text="02" />
<Textview
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/thirdnumber"
android:layout_width="match_parent"
android:inputType="numberDecimal">
android:text="03" />
<Textview
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/fourthnumber"
android:layout_width="match_parent"
android:inputType="numberDecimal">
android:text="04" />
<Textview
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/fifthnumber"
android:layout_width="match_parent"
android:inputType="numberDecimal">
android:text="05" />
<Textview
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/power_ball"
android:layout_width="match_parent"
android:inputType="numberDecimal">
android:text="06" />
// you can add the text size, color, other alignment also as per your requirement //
</LinearLayout>
<view
android:layout_height="80dp"
android:layout_width="wrap_content" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:textSize="8pt"
android:text="@string/Generate">
android:background=”@color/red”
android:id=”+@id/generateButton”
</Button>
[Remember:
In the resource provide string name
<string name = “generate”>Generate</string>
When this button will be pressed it will generate random value
]
MainActivity.java
package com.lottery;
public class MainActivity extends AppCompatActivity
{
//we have to declare/ referral all the variables to java, so that if any changes occurs to the variable it should also get reflected to the layout.//
private TextView mfirstnumber, msecondnumber, mthirdnumber, mfourthnumber, mfifthnumber, mpower_ball;
private Button mgenerateButton;
private String[] lotteryBalls = {
“1”,”2”,”3”,”4”,”5”,”6”,”7”,”8”,”9”,”10”,”11”,”12”,”13”,”14”,”15”,”16”,
“17”,”18”,”19”,”20”,”21”,”22”,”23”
}
//make it upto 69 as per your requirement
float result_num;
int num1, num2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
bindViews();
generateRandomNumber();
mgenerateButton.setOnClickListener(new onOnClickListener(){
public void onClick(View view){
generateRandomNumber();
}
});
}
private void generateRandomNumber(){
collections.shuffle(Arrays.asList(lotteryBalls));
mfirstnumber.setText(lotteryBalls[0]);
msecondnumber.setText(lotteryBalls[1]);
mthirdnumber.setText(lotteryBalls[2]);
mfourthnumber.setText(lotteryBalls[3]);
mfifthnumber.setText(lotteryBalls[4]);
Random rand = new Random();
int power_balls= (rand. nextInt(10)+1)
mpower_balls.setText(power_balls + “” );
}
private void bindViews(){
mfirstnumber = (Textview)findviewbyId(R.id.firstnumber);
msecondnumber = (Textview)findviewbyId(R.id.secondnumber);
mthirdnumber = (Textview)findviewbyId(R.id.thirdnumber);
mfourthnumber = (Textview)findviewbyId(R.id.fourthnumber);
mfifthnumber = (Textview)findviewbyId(R.id.fifthnumber);
mpower_ball = (Textview)findviewbyId(R.id.power_ball);
mgenerateButton = (Button)findviewbyId(R.id.generatebutton);
}
//SAVE ALL THE CHANGES AND RUN THE APPLIACTION//