Wednesday, 25 May 2016

SIMPLE LETTER COUNTING IN ANDROID STUDIO Complete Part

This part completes the letter counting game showing the user interaction and results as seen in the images shown below.


STEPS: As shown in the images below, a user needs to enter his/her name to be able to play this game and only with the use of the menu bar will the game start by creating another page where alphabetical values (array of names) are shown for counting.

ACTIVITY_MAIN.XML

<?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"
    android:background="#4ba9f5"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Game Central"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/player_name"
        android:layout_width="100dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="Player Name" />

    <EditText
        android:id="@+id/Pname"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_alignTop="@+id/player_name"
        android:layout_toRightOf="@+id/player_name" />

    <TextView
        android:id="@+id/current_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Current Score"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/current_score"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="0/0" />

</RelativeLayout>

MAIN_ACTIVITY.JAVA

package com.example.jeffery.BJ1616939Task3;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //EditText et = (EditText) findViewById(R.id.Pname);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        EditText et = (EditText) findViewById(R.id.Pname);

        //noinspection SimplifiableIfStatement
        if (id == R.id.Letter_counting) {
            Intent intent = new Intent(MainActivity.this, Counting_Game.class);
            intent.putExtra("Welcome", et.getText().toString());
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

COUNTING_GAME.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4ba9f5">

    <TextView
        android:id="@+id/users"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="43dp"
        android:text="User"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView3"
        android:layout_alignRight="@+id/textView5"
        android:layout_alignEnd="@+id/textView5" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/users"
        android:layout_centerHorizontal="true"
        android:text="I will choose a random number"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:text="how many letters are in it?"
        android:textSize="20dp"/>

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VALUES"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_above="@+id/editText2"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="175dp" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:src="@drawable/counting"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
        android:text="Check" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:text="No Value" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_above="@+id/textView4"
        android:layout_toLeftOf="@+id/editText2"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignStart="@+id/textView5" />
</RelativeLayout>


COUNTING_GAME.JAVA

package com.example.jeffery.BJ1616939Task3;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by JEFFERY on 4/27/2016.
 */
public class Counting_Game extends Activity {

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

        TextView userView = (TextView)findViewById(R.id.users);
        userView.setText(getIntent().getExtras().getString("Welcome"));


        final EditText userguess = (EditText) findViewById(R.id.editText2);
        final TextView tvInput = (TextView) findViewById(R.id.textView6);
        final TextView tvOutput = (TextView) findViewById(R.id.textView7);
        Button btcheck = (Button)findViewById(R.id.button);

        String[] myNames ={"emmanuel", "Mary", "Patrick", "Janet"};
        int rando = (int)(Math.random()*4);
        tvInput.setText(myNames[rando]);

        btcheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int rando;
                rando = Integer.parseInt(userguess.getText().toString());
                if (tvInput.length() == rando) {
                    tvOutput.setText("Correct Value");
                }else if (tvInput.length()!= rando){
                    tvOutput.setText("Wrong Value");
                }else {
                    tvOutput.setText(null);
                }
            }
        });


    }
}

MENU_MAIN.XML

<menu 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"
    tools:context="com.example.jeffery.BJ1616939Task3.MainActivity">
    <item
        android:id="@+id/Letter_counting"
        android:orderInCategory="100"
        android:title="@string/Letter_Counting"
        app:showAsAction="never" />
    <item
        android:id="@+id/Game_Holder_2"
        android:orderInCategory="101"
        android:title="@string/Game_Holder_2"
        app:showAsAction="never"/>

    <item
        android:id="@+id/Game_Holder_3"
        android:orderInCategory="101"
        android:title="@string/Game_Holder_3"
        app:showAsAction="never"/>

    <item
        android:id="@+id/Game_Holder_4"
        android:orderInCategory="101"
        android:title="@string/Game_Holder_4"
        app:showAsAction="never"/>
</menu>

MANIFEST.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jeffery.BJ1616939Task3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.example.jeffery.BJ1616939Task3.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.jeffery.BJ1616939Task3.Counting_Game">

        </activity>
    </application>

</manifest>


As shown above in the above code of the counting game comprises of two pages connected with menu intent. so once the player name is input with the help of the menu bar, the game can be started . below are the image views of the game accordingly.





Note: This is a simple counting game...!!!

















No comments:

Post a Comment