In the continuing adventures of higher education and personal learning, I made a simple little guessing game for Android, as instructed by my online course for Android app development. It is a very basic game, where the user must guess the number Android is thinking of. You can download it here:
https://www.mediafire.com/download/rf7du2ysaddwos5

Android chooses a new number every time you open the app, so it has infinite replay value!

Here is the breakdown of the major components:
androidmanifest.xml

[CODE]

[/CODE]
main.xml

[CODE]

[/CODE]
mainactivity.java

[CODE]

package com.alaskalinuxuser.guessthenumber;
import android.app.*;

import android.os.*;

import java.util.*;

import android.view.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.*;

public class MainActivity extends Activity 

{

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

Random appRandom = new Random();

int hidRandom = appRandom.nextInt(81) + 10;

public void takeGuess(View v)

{

EditText userGuess = (EditText) findViewById(R.id.myGuess);

Double uGuess = Double.parseDouble(userGuess.getText().toString());

if (uGuess hidRandom) {
Toast toast = Toast.makeText(getApplicationContext(), “Too high!”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), “That’s right, you guessed it!”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();
}

}

}

[/CODE]
As you can see, there is not much to this app. I was pleased that I was able to put it together in about 15 minutes (Praise God!), and, while simple, it is somewhat fun, if you’re board!
Linux – keep it simple.

2 Replies to “​Android number guessing game!”

Leave a Reply

Your email address will not be published. Required fields are marked *