Twitter Facebook Delicious Digg Stumbleupon Favorites More

Monday 8 January 2018

Android App Magic Ball 8

Android App Magic Ball 8 


package com.example.viraj.magic_8_ball;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
private Button rollbutton;
private ImageView imageview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rollbutton=(Button)findViewById(R.id.id_button);
        imageview=(ImageView)findViewById(R.id.id_image);
final int [] arrayimage={
        R.drawable.ball1,
        R.drawable.ball2,
        R.drawable.ball3,
        R.drawable.ball4,
        R.drawable.ball5
};
        rollbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Random random=new Random();
                int randomnogene=random.nextInt(arrayimage.length);//or int randomnogene=random.nextInt(5);
                imageview.setImageResource(arrayimage[randomnogene]);
            }
        });
    }
}



<?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="com.example.viraj.magic_8_ball.MainActivity"
    android:background="#4FC3F7">

    <TextView
        android:id="@+id/text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="@string/txttitle"
        android:textAlignment="center"
        android:textColor="#E91E63"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/id_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/ball1" />

    <Button
        android:id="@+id/id_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/id_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:shadowColor="#FF5722"
        android:textColor="#E91E63"
        android:textStyle="bold"
        android:text="@string/buttonname" />
</RelativeLayout>




Share:

0 comments:

Post a Comment

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab