Dice Roller App using Java

 Dice Roller App using Java In Android Studio



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#29b6f6"
tools:context=".MainActivity">

<ImageView
android:id="@+id/Image_view_dice"
android:layout_width="300dp"
android:layout_height="300dp"
android:onClick="play"

android:src="@drawable/one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DICE ROLLER"
android:textColor="#FFEB3B"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/Image_view_dice"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java code:-

//package use in this app
package com.example.dicerolling;

import androidx.appcompat.app.AppCompatActivity;

import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.Random;


public class MainActivity extends AppCompatActivity {
private ImageView ImageViewDice;
private Random rng = new Random();

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

ImageViewDice = findViewById(R.id.Image_view_dice);
ImageViewDice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rollDice();
}
});

}
private void rollDice()//function to roll dice
{
int randomNumber = rng.nextInt(6)+1;//create random number 1 o 6
switch (randomNumber)
{
case 1:
ImageViewDice.setImageResource(R.drawable.one);


break;
case 2:
ImageViewDice.setImageResource(R.drawable.two);
break;
case 3:
ImageViewDice.setImageResource(R.drawable.three);
break;
case 4:
ImageViewDice.setImageResource(R.drawable.four);
break;
case 5:
ImageViewDice.setImageResource(R.drawable.five);
break;
case 6:
ImageViewDice.setImageResource(R.drawable.six);

}
}
}

Comments

Popular posts from this blog

the distance between two cities (in km is input through the keyboard. write a program to convert and print this distance in meter ,feet inches and centimeter.

CREATE UNIT CONVERTER APP

write a program to check whether a triangle is valid or not, when the threeangles of the triangle are entered through the keyboard. A triangle is valid if the sumof all the three angles is equal to 180 degrees