Friday, April 8, 2016

Play A Game - Enter Your lucky Number

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a small game in which you have to enter your lucky Number .
Every Time when you run the App auto generated number will be differ.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Special Thanks Prerna Nagori (Student) 

Please Like us & put your valuable suggestions in comment box.

A. Output           


B. XML file    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:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.student.random1.MainActivity"

    android:background="#b0e5e6">


    <EditText        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/ed1"        android:gravity="center"

        android:layout_below="@+id/textView"

        android:layout_marginTop="6dp"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true" />


    <EditText        android:layout_width="match_parent"

        android:layout_height="wrap_content"        android:id="@+id/et2"

        android:textColor="#935703"

        android:hint="Enter a Number between 1 to 11"        android:gravity="center"

        android:layout_below="@+id/ed1"

        android:layout_alignRight="@+id/b1"

        android:layout_alignEnd="@+id/b1"

        android:layout_marginTop="42dp"        />

    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"        android:text="Play"

        android:id="@+id/b1"        android:background="#f3ecaa"

        android:textColor="#ad770b"        android:onClick="show"

        android:layout_centerVertical="true"

        android:layout_alignRight="@+id/ed1"

        android:layout_alignEnd="@+id/ed1" />

    <TextView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/tv3"        android:textColor="#935703"

        android:textSize="30dp"        android:layout_alignBottom="@+id/b1"

        android:layout_alignLeft="@+id/et2"

        android:layout_alignStart="@+id/et2" />

    <TextView        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Enter Your Lucky Number"

        android:gravity="center"        android:textSize="25dp"

        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />

</RelativeLayout>

C. Java File    MainActivity.java

package com.example.student.random1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    EditText e1,e2;
    TextView e3;
    int b;
    Random r= new Random();
    int i=r.nextInt(10)+1;
    int c=0;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=(EditText)findViewById(R.id.ed1);
        e2=(EditText)findViewById(R.id.et2);
        e3=(TextView)findViewById(R.id.tv3);
    }
    public void show(View v)
    {

        String s=e1.getText().toString();
        b=Integer.parseInt(s);

        if(i==b)
        {
            e2.setText("Congratulations");
            e3.setText("You Win");
        }
        else if (i<b)
            {
                e1.setText("");
             e2.setText("Enter small number");

            }
            else        {
            e1.setText("");
            e2.setText("Enter greater number");

        }

    }

}




No comments:

Post a Comment