package com.example.viraj.diceapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button rollbutton;
ImageView leftimage, rightimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollbutton = (Button) findViewById(R.id.rollbutton);
leftimage = (ImageView) findViewById(R.id.leftdice);
rightimage = (ImageView) findViewById(R.id.rightdice);
final int[] dicearray = {
R.drawable.dice1,
R.drawable.dice2,
R.drawable.dice3,
R.drawable.dice4,
R.drawable.dice5,
R.drawable.dice6,
};
rollbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random random_nu_generator = new Random();
int random_no = random_nu_generator.nextInt(6);
leftimage.setImageResource(dicearray[random_no]);
int random_no_2 = random_nu_generator.nextInt(6);
rightimage.setImageResource(dicearray[random_no_2]);
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:background="@drawable/newbackground"
tools:context="com.example.viraj.diceapp.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<ImageView
android:id="@+id/logoimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/dicee_logo" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/leftdice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/dice3" />
<ImageView
android:id="@+id/rightdice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/dice1" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/rollbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/button_text"
android:textStyle="bold"
android:background="#3F51B5"
android:textColor="#FFEB3B"/>
</RelativeLayout>
</LinearLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button rollbutton;
ImageView leftimage, rightimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rollbutton = (Button) findViewById(R.id.rollbutton);
leftimage = (ImageView) findViewById(R.id.leftdice);
rightimage = (ImageView) findViewById(R.id.rightdice);
final int[] dicearray = {
R.drawable.dice1,
R.drawable.dice2,
R.drawable.dice3,
R.drawable.dice4,
R.drawable.dice5,
R.drawable.dice6,
};
rollbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random random_nu_generator = new Random();
int random_no = random_nu_generator.nextInt(6);
leftimage.setImageResource(dicearray[random_no]);
int random_no_2 = random_nu_generator.nextInt(6);
rightimage.setImageResource(dicearray[random_no_2]);
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:background="@drawable/newbackground"
tools:context="com.example.viraj.diceapp.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<ImageView
android:id="@+id/logoimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/dicee_logo" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/leftdice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/dice3" />
<ImageView
android:id="@+id/rightdice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/dice1" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/rollbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/button_text"
android:textStyle="bold"
android:background="#3F51B5"
android:textColor="#FFEB3B"/>
</RelativeLayout>
</LinearLayout>
0 comments:
Post a Comment