Developing a simple memory game - android

I'm kinda new to Android, now I'm developing a very simple game, the logic is described as following: The user sees a 10-digit value after pressing a "Ready" Button. After 5 seconds, the value changes to
* * * * * * * and the user has to enter it in a text field (type "number") below. Under that text field there are 2 Buttons: "Check" and "Give a hint". The check Button compares the user value to original value and changes a TextView according to the result("Correct"/"Incorrect"). The hint Button should show 3 random digits in the original value.
I have some questions on that small application:
I'm not sure which type I have to use to show the original value to the user: a
TextView, a text field or something else?
How do I force the app to show 3 random digits from the original value in the case
when user presses the hint Button?
Any help is appreciated.

1). I'm not sure which type I have to use to show the original value to the user: a TextView, a text field or something else?
For this TextView will do.
2). How do I force the app to show 3 random digits from the original value in the case when user presses the hint button?
Hopefully you are using int for storing 10 digit value (original value)
Make an array of int having size 10
int[] digits = new int[10];
Use for loop to separate all 10 digits from the 10 digit number.
int number = 1234567891
for(int i = 0; i < 10; i++){
digits[i] = number % 2;
number = number / 10;
}
This will give you array of 10 int values
Make object of java.util.Random class and fetch random values from 0-9
Random r = new Random();
int pos1 = r.nextInt(9);
nextInt(int n) returns a pseudo-random uniformly distributed int in the half-open range [0, n).
These values are positions from which you are going to retrieve digit from array
So this will give you random position between 0 and 9
Just make sure that each time you generate random value from Random, it should not be equal to previously generated values e.g. if you are generating random position for second digit you should check that it should be equal to first
you can show all 3 digits(fetched from 3 randomly generated positions) in TextView as a hint
Hope this will solve your problem
Edited Part
Set visibility of Textview to View.GONE
something like this
private TextView tv;
tv = (TextView)findViewById(R.id.textView01);
tv.setVisibility(View.GONE);
Button btn = (Button)findViewById(R.id.button01);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View view){
tv.setVisibility(View.VISIBLE);
}
});

Related

Android Counter Application Score List

This is the interface of an application i am trying to succeed the last days.
It is just a simple counter, of a card game. I have managed to create the buttons, do the counting and show it in a different textview, stoping in a limit i wanted.
The thing is, that i want the scores of each round separetely, to be shown until the end of the game, like a list i draw in ms paint for preview.
I have managed to put the score in the 1st round on the txt1 and txt2 fields, but i cant figure out, how to put the 2nd round on txt3 and txt4 field, the 3rd on txt5 and txt6 field etc.
This is the code i ve created, of the void that puts the txt1 and txt2 into that fields.
private void setScore() {
Double skor1 = Double.parseDouble(editText1.getText().toString());
Double skor2 = Double.parseDouble(editText2.getText().toString());
txt1.setText(Double.toString(skor1));
txt2.setText(Double.toString(skor2));
while (telikoOmada1 < 64 && telikoOmada2 < 64) {
skor1 = Double.parseDouble(editText1.getText().toString());
skor2 = Double.parseDouble(editText2.getText().toString());
telikoOmada1 = telikoOmada1 + skor1;
telikoOmada2 = telikoOmada2 + skor2;
telikoTxt1.setText(Double.toString(telikoOmada1));
telikoTxt2.setText(Double.toString(telikoOmada2));
editText1.setText(null);
editText2.setText(null);
break;
}
}

Restricting Android NumberPicker to Numeric Keyboard for Numeric Input (not Alpha keyboard)

Is there a way to suggest or restrict keyboard input when selecting a NumberPicker so only the number controls are shown when entering values, similar to how you can use android:inputType="number" with a EditText?
I have a series of values, from 0.0 to 100.0 in 0.1 increments that I'd like to be able to use a NumberPicker to select in Android 4.3. In order to have the numbers selectable, I've created an array of strings which corresponds to these values, as shown below:
NumberPicker np = (NumberPicker) rootView.findViewById(R.id.programmingNumberPicker);
int numberOfIntensityOptions = 1001;
BigDecimal[] intensityDecimals = new BigDecimal[numberOfIntensityOptions];
for(int i = 0; i < intensityDecimals.length; i++ )
{
// Gets exact representations of 0.1, 0.2, 0.3 ... 99.9, 100.0
intensityDecimals[i] = BigDecimal.valueOf(i).divide(BigDecimal.TEN);
}
intensityStrings = new String[numberOfIntensityOptions];
for(int i = 0; i < intensityDecimals.length; i ++)
{
intensityStrings[i] = intensityDecimals[i].toString();
}
// this will allow a user to select numbers, and bring up a full keyboard. Alphabetic keys are
// ignored - Can I somehow change the keyboard for this control to suggest to use *only* a number keyboard
// to make it much more intuitive?
np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
np.setDisplayedValues(intensityStrings);
np.setWrapSelectorWheel(false);
As more info, I've noticed that if I dont use the setDisplayedValues() method and instead set the integers directly, the numeric keyboard will be used, but the problem here is that the number being entered is 10 times more than it should be - e.g. if you enter "15" into the control its interpreted as "1.5"
// This will allow a user to select using a number keyboard, but input needs to be 10x more than it should be.
np.setMinValue(0);
np.setMaxValue(numberOfIntensityOptions-1);
np.setFormatter(new NumberPicker.Formatter() {
#Override
public String format(int value) {
return BigDecimal.valueOf(value).divide(BigDecimal.TEN).toString();
}
});
Any suggestions on how to raise a numeric keyboard to allow a user to enter decimal numbers like this?
I have successfully achieved this, borrowing heavily from #LuksProg's helpful answer to another question. The basic idea is to search for the EditText component of the NumberPicker and then to assign the input type as numeric. First add this method (thanks again to #LuksProg):
private EditText findInput(ViewGroup np) {
int count = np.getChildCount();
for (int i = 0; i < count; i++) {
final View child = np.getChildAt(i);
if (child instanceof ViewGroup) {
findInput((ViewGroup) child);
} else if (child instanceof EditText) {
return (EditText) child;
}
}
return null;
}
Then, in my Activity's onCreate() method I call this and set the input type:
np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
EditText input = findInput(np);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
That did the trick for me!
My answer is to improve on the basis of Alan Moore, just change the last line
input.setInputType(InputType.TYPE_CLASS_NUMBER);
to
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
then there will no more problem like personne3000 said, no more crashes.
I'm sorry my English is not good, but I hope you can understand what I mean.

Reading a TableRow cell value and returning an integer

Basically I have a TableLayout with several TableRows, each containg 5 cells (Name, Price, Count, IncreaseButton, DecreaseButton), and then I have written a function to return the price of a certain row, but I keep getting an exception.
This is the code:
private int getRowPrice(View target) //target is a Button
{
TableRow row = (TableRow) target.getParent();
TextView priceCell = (TextView) row.getChildAt(1);
String price = priceCell.getText().toString();
//The cell text may start with $ or another currency
if (price.startsWith(getString(R.string.currency)))
{
price = price.substring(getString(R.string.currency).length()).toString();
}
return Integer.getInteger(price); //Here I get a "java.lang.NullPointerException"
}
If I Log.d() the price it outputs the correct value, and if I try to return getInteger("10") that works fine, also the debugger classifies 'price' as a string. So I must admit I have no idea why this error keeps occuring.
And please let me know if there is a better, less-error-prone way to get a cell of a certain row)
You need to use Integer.valueOf() instead of Integer.getInteger(). If you look at the API docs for Integer.getInteger() you'll see that it "Returns the Integer value of the system property identified by the given string". The method name is a bit misleading.

How set selection in spinner of some string if spinner contains the same? [duplicate]

This question already has answers here:
How to set selected item of Spinner by value, not by position?
(25 answers)
Closed 8 years ago.
I posted similar question recently but nobody helped me. So I'll try to explain my problem better than before.
So I read some text from one file (this file may include more words) and then I read text from second file (this file always includes one word which is the same as one word in first file). Text in both files may be different everytime.
So for example:
First string contains: black blue yellow red green
Second string contains: yellow
Then I make spinner from first string, so spinner contains in this example these words (black blue yellow red green), so default option is black (coz it is first in my array), but I need to make third position as default in my spinner in this example, because second string contains yellow and yellow is on the third position in my spinner.
How can I make it without repopulating spinner?
btw. these strings are only example. Files may always includes different words than before.
Solution:
s1.setSelection(getIndex(s1, prefNameCurGOV));
and then:
private int getIndex(Spinner s1, String prefNameCurGOV){
int index = 0;
for (int i=0;i<s1.getCount();i++){
if (s1.getItemAtPosition(i).equals(prefNameCurGOV)){
index = i;
}
}
return index;
Something like:
String secondString = secondSpinner.getSelectedItem();
firstSpinner.setSelection(getIndex(firstSpinner,secondString));
then use
private int getIndex(Spinner spinner,String string){
//Pseudo code because I dont remember the API
int index = 0;
for (int i = 0; i < firstSpinner.size(); i++){
if (firstSpinner.getItemAtPosition(i).equals(string)){
index = i;
}
}
return index;
}

Detecting only the last two digits in text entry box for a calculator app

I'm still pretty new to this, but I'm trying to make a calculator for myself to use at work; similar to a resistor calculator.
I am hope to change a textview and imageview according to the last two digits entered into a edittext box. For example, if I were to type in "9,000,011", I would want to display a certain color of image and text that corresponds with "11" and the same color and text for say 1,000,011. Also different for 12, 13, and so on. this way No matter what number I type it only looks at the last two digits. Does anyone know the way to do this or maybe can just point me in the right direction?
here is how I'm
private void calculate() {
number = Double.parseDouble(inputnumber.getText().toString());
ImageView iv = (ImageView) this.findViewById(R.id.pairimage);
if (number == 6000001) {
iv.setImageResource(R.drawable.white);
txtnumber.setText("White");
} else if (number == 6000002) {
iv.setImageResource(R.drawable.red);
txtnumber.setText("Red");
}
//*and so on, all the way up to 99*
}
If you want just the last two, then I would use
String wholeNumber = inputnumber.getText().toString();
int n = Integer.valueOf(wholeNumber.subString(wholeNumber.length()-2, wholeNumber.length()-1);
and then a switch block:
switch(n) {
case 1:
iv.setImageResource(R.drawable.white);
txtnumber.setText("White");
break;
case 2:
iv.setImageResource(R.drawable.red);
txtnumber.setText("Red");
break;
}
etc.
Maybe convert the number to a String and then look at that e.g.
String theNumber = String.valueOf(number);
String lastTwoDigits =
theNumber.substring(theNumber.length() -2, theNumber.length());
Then you can have your if statements to read the lastTwoDigits. Or convert back to int (Integer.valueOf(lastTwoDigits); and use a switch statement. Or put the values 0-99 into a Map and have a Command object or something as the value which gets executed.
Obviously you'll need some validation here on the user input.
You have a couple of options. The easiest one is to use the modulus operator like:
number = number % 100;
switch (number) {
case 1:
// do stuff;
break;
case 2:
// do stuff;
break;
}
For this to work, though, "number" will have to be an integer. It's an integer in your examples, so that may work for you; otherwise you can try some math tricks like:
int number2 = (int)(number * 100) % 100;
Go with the sub-string approach. I've found that some number combinations don't multiply/divide well in java.

Categories

Resources