I have tested the methods individually by calling them in the case which evaluates the equals button. They work fine. My issue is determining whether the multiplication or addition operator was pressed, and then calling the appropriate method.
I searched online and used an if statement within the case which evaluates the multiplication as such:
case R.id.buttonequals:
if (v.equals(btnmult)) {
Multiplication();
}
else if (v.equals(btnplus)) {
Addition();
}
break;
This doesn't work. It seems logical enough. If the equal button is pressed, then depending on what was pushed, the right method will be called. My full code is below:
public class MainActivity extends Activity {
// variables declared to hold buttons and text field.
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnc,
btnplus, btnd, btnpnt, btnequal, btnmult;
TextView text;
int num1, num2;
String input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// reference and assign the resources for the view elements
btn0 = (Button) findViewById(R.id.button0);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
btn5 = (Button) findViewById(R.id.button5);
btn6 = (Button) findViewById(R.id.button6);
btn7 = (Button) findViewById(R.id.button7);
btn8 = (Button) findViewById(R.id.button8);
btn9 = (Button) findViewById(R.id.button9);
btnc = (Button) findViewById(R.id.buttonc);
text = (TextView) findViewById(R.id.textView1);
btnd = (Button) findViewById(R.id.buttondel);
btnpnt = (Button) findViewById(R.id.buttonpnt);
btnplus = (Button) findViewById(R.id.buttonplus);
btnequal = (Button) findViewById(R.id.buttonequal);
btnmult = (Button) findViewById(R.id.buttonmult);
// add the click listeners
btn0.setOnClickListener(onClickListener);
btn1.setOnClickListener(onClickListener);
btn2.setOnClickListener(onClickListener);
btn3.setOnClickListener(onClickListener);
btn4.setOnClickListener(onClickListener);
btn5.setOnClickListener(onClickListener);
btn6.setOnClickListener(onClickListener);
btn7.setOnClickListener(onClickListener);
btn8.setOnClickListener(onClickListener);
btn9.setOnClickListener(onClickListener);
btnc.setOnClickListener(onClickListener);
btnd.setOnClickListener(onClickListener);
btnpnt.setOnClickListener(onClickListener);
btnplus.setOnClickListener(onClickListener);
btnequal.setOnClickListener(onClickListener);
btnmult.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button0:
text.setText(text.getText().toString()
+ btn0.getText().toString());
break;
case R.id.button1:
text.setText(text.getText().toString()
+ btn1.getText().toString());
break;
case R.id.button2:
text.setText(text.getText().toString()
+ btn2.getText().toString());
break;
case R.id.button3:
text.setText(text.getText().toString()
+ btn3.getText().toString());
break;
case R.id.button4:
text.setText(text.getText().toString()
+ btn4.getText().toString());
break;
case R.id.button5:
text.setText(text.getText().toString()
+ btn5.getText().toString());
break;
case R.id.button6:
text.setText(text.getText().toString()
+ btn6.getText().toString());
break;
case R.id.button7:
text.setText(text.getText().toString()
+ btn7.getText().toString());
break;
case R.id.button8:
text.setText(text.getText().toString()
+ btn8.getText().toString());
break;
case R.id.button9:
text.setText(text.getText().toString()
+ btn9.getText().toString());
break;
case R.id.buttondel:
String contents = text.getText().toString();
if (contents.length() > 1) {
text.setText(contents.substring(0, contents.length() - 1));
} else {
text.setText("");
}
break;
case R.id.buttonc:
text.setText("");
break;
case R.id.button8:
String dot = ".";
String concat = text.getText() + dot;
text.setText(concat);
break;
case R.id.buttonplus:
num1 = Integer.parseInt(text.getText().toString());
num2 = Integer.parseInt(text.getText().toString());
text.setText(text.getText() + "+");
break;
case R.id.buttonmult:
num1 = Integer.parseInt(text.getText().toString());
num2 = Integer.parseInt(text.getText().toString());
text.setText(text.getText() + "x");
break;
case R.id.buttonequals:
if (v.equals(btnmult)) {
Multiplication();
}
else if (v.equals(btnplus)) {
Addition();
}
break;
}
}
};
public void Addition() {
text.setText("");
int total = num1 + num2;
text.setText(Integer.toString(total));
}
public void Multiplication() {
text.setText("");
int total = num1 * num2;
text.setText(Integer.toString(total));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Help would be greatly appreciated, and I apologise if the solution is blindly obvious. Thank you all for your time.
Add an onClickListener for your each buttons (multiply, divide etc).
Also add a flag for each as,
boolean multiply = false; //same for rest
which you will have to set to true when they are clicked like,
case R.id.multiply:
multiply = true;
then finally when you chck for onClick event of your = button
public void onClick(View v){
..
case R.id.buttonequals:
if (multiply) {
Multiplication();
}
else if (other flags) {
Addition();
}
break;
}
set a same android:onClick for your all of your Buttons, For example: "clicking"
then use it like this:
public void clicking(View v) {
Button button = (Button) v;
switch (button.getId()) {
case R.id.button0:
text.setText(text.getText().toString()
+ btn0.getText().toString());
break;
case R.id.button1:
text.setText(text.getText().toString()
+ btn1.getText().toString());
break;
and so on...
Related
I want to create a "calculator" that works in this way: when in my MainActivity I press on the TextView, a Dialog (or an Activity with Theme.DeviceDefault.Light.Dialog theme or something like that) opens with the Keyboard where I can make my operations and the result will change the text of the TextView. The result will be something like this:
Image - MainActivity.class
Image - IncreaseReduce.class
The problem is that each time I press "+" or "-" and the application closes the IncreaseReduce activity, the MainActivity is reinitialized and the onCreate method is call (I presume). For example if i want to add 5 the application add 5 to the default value (in this case is 1000). This happens each time, so i can't do operations like: previous_result+5+10 (result 1015). The result will be 1010. How can i resolve? Furthermore, I will prefer to use a dialog but i don't no how.
This is my code:
MainActivity.class
public class MainActivity extends AppCompatActivity {
private TextView textView;
private String toEncreaseReduce = "";
private int operation = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, IncreaseReduce.class);
startActivity(intent);
}
});
Bundle b = getIntent().getExtras();
if(b != null) {
toEncreaseReduce = b.getString("to");
operation = b.getInt("operation");
}
if(operation != -1 && toEncreaseReduce != "")
operate(toEncreaseReduce,operation);
}
public void operate(String toEncreaseReduce, int operation)
{
long lpInt;
String lpString;;
long op;
lpString = textView.getText().toString();
lpInt = Long.parseLong(lpString);
op = Long.parseLong(toEncreaseReduce);
if (operation == 1)
lpInt = lpInt + op;
if(operation == 2)
lpInt = lpInt - op;
textView.setText("" + Long.toString(lpInt));
}
}
EncreaseReduce.class
public class IncreaseReduce extends Activity implements View.OnClickListener{
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button buttonm;
private Button button0;
private Button buttonp;
private String lp;
private String number = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.increase_reduce_layout);
button0 = (Button) findViewById(R.id.b0);
button1 = (Button) findViewById(R.id.b1);
button2 = (Button) findViewById(R.id.b2);
button3 = (Button) findViewById(R.id.b3);
button4 = (Button) findViewById(R.id.b4);
button5 = (Button) findViewById(R.id.b5);
button6 = (Button) findViewById(R.id.b6);
button7 = (Button) findViewById(R.id.b7);
button8 = (Button) findViewById(R.id.b8);
button9 = (Button) findViewById(R.id.b9);
buttonp = (Button) findViewById(R.id.bp);
buttonm = (Button) findViewById(R.id.bm);
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonp.setOnClickListener(this);
buttonm.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch(v.getId())
{
case(R.id.b0):
number = number + "0";
setTitle(number);
break;
case(R.id.b1):
number = number + "1";
setTitle(number);
break;
case(R.id.b2):
number = number + "2";
setTitle(number);
break;
case(R.id.b3):
number = number + "3";
setTitle(number);
break;
case(R.id.b4):
number = number + "4";
setTitle(number);
break;
case(R.id.b5):
number = number + "5";
setTitle(number);
break;
case(R.id.b6):
number = number + "6";
setTitle(number);
break;
case(R.id.b7):
number = number + "7";
setTitle(number);
break;
case(R.id.b8):
number = number + "8";
setTitle(number);
break;
case(R.id.b9):
number = number + "9";
setTitle(number);
break;
case(R.id.bp):
passage(1, number);
break;
case(R.id.bm):
passage(2, number);
break;
}
}
public void passage(int operation, String toIncreaseReduce)
{
/*
* 1: add
* 2: reduce
*/
Intent intent = new Intent(IncreaseReduce.this, MainActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (toIncreaseReduce != "") {
Bundle b = new Bundle();
b.putString("to", number);
b.putInt("operation",1);
intent.putExtras(b); //Put your id to your next Intent
}
startActivity(intent);
finish();
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".IncreaseReduce"
android:theme="#android:style/Theme.DeviceDefault.Light.Dialog"></activity>
</application>
My question is, how do I make a save file using a text file. I want to be able to save the game and to load even after the phone has been restarted.
So what I'm asking is how to make a save file using text file.
public class MainActivity extends AppCompatActivity {
ImageButton Button,Button2,Button3,Button4,Button5,Button6,Button7,Button8,Button9,Button10;
long count = 0;
MediaPlayer mp, mp_pilla,mp_slua;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textview = (TextView) findViewById(R.id.textView);
mp = MediaPlayer.create(MainActivity.this, R.raw.nyaaw);
mp_pilla = MediaPlayer.create(MainActivity.this, R.raw.pillabli);
mp_slua = MediaPlayer.create(MainActivity.this, R.raw.slua);
Button = (ImageButton) findViewById(R.id.myButton);
Button2 = (ImageButton) findViewById(R.id.myButton2);
Button3 = (ImageButton) findViewById(R.id.myButton3);
Button4 = (ImageButton) findViewById(R.id.myButton4);
Button5 = (ImageButton) findViewById(R.id.myButton5);
Button6 = (ImageButton) findViewById(R.id.myButton6);
Button7 = (ImageButton) findViewById(R.id.myButton7);
Button8 = (ImageButton) findViewById(R.id.myButton8);
Button9 = (ImageButton) findViewById(R.id.myButton9);
Button10 = (ImageButton) findViewById(R.id.myButton10);
textview.setText("0");
ImageButton buttonsak = (ImageButton) findViewById(R.id.myButton);
ImageButton buttonsak2 = (ImageButton) findViewById(R.id.myButton2);
ImageButton buttonsak3 = (ImageButton) findViewById(R.id.myButton3);
ImageButton buttonsak4 = (ImageButton) findViewById(R.id.myButton4);
ImageButton buttonsak5 = (ImageButton) findViewById(R.id.myButton5);
ImageButton buttonsak6 = (ImageButton) findViewById(R.id.myButton6);
ImageButton buttonsak7 = (ImageButton) findViewById(R.id.myButton7);
ImageButton buttonsak8 = (ImageButton) findViewById(R.id.myButton8);
ImageButton buttonsak9 = (ImageButton) findViewById(R.id.myButton9);
ImageButton buttonsak10 = (ImageButton) findViewById(R.id.myButton10);
View.OnClickListener clicker = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.myButton:
count++;
mp.start();
textview.setText("" + count);
break;
case R.id.myButton2:
if (count >= 50) {
count += 5;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton3:
if (count >= 100) {
count += 10;
mp_pilla.start();
textview.setText("" + count);
}
break;
case R.id.myButton4:
if (count >= 500) {
count += 20;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton5:
if (count >= 2000) {
count += 50;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton6:
if (count >= 5000) {
count += 100;
mp.start();
textview.setText("" + count);
}
break;
case R.id.myButton7:
if (count >= 10000) {
count += 200;
mp_slua.start();
textview.setText("" + count);
}
break;
case R.id.myButton8:
if (count >= 30000) {
count += 500;
mp_pilla.start();
textview.setText("" + count);
}
break;
case R.id.myButton9:
if (count >= 100000) {
count += 1000;
mp.start();
textview.setText("" + count);
}
break;
case R.id.myButton10:
if (count >= 250000) {
count += 2500;
mp_pilla.start();
textview.setText("" + count);
}
break;
default:
break;
}
}
};
buttonsak.setOnClickListener(clicker);
buttonsak2.setOnClickListener(clicker);
buttonsak3.setOnClickListener(clicker);
buttonsak4.setOnClickListener(clicker);
buttonsak5.setOnClickListener(clicker);
buttonsak6.setOnClickListener(clicker);
buttonsak7.setOnClickListener(clicker);
buttonsak8.setOnClickListener(clicker);
buttonsak9.setOnClickListener(clicker);
buttonsak10.setOnClickListener(clicker);
}
Use SharePreference to save data like this:
SharedPreferences.Editor editor = getSharedPreferences(SAVE_GAME, MODE_PRIVATE).edit();
editor.putString("game", "saved");
editor.commit();
and get the info like this:
SharedPreferences prefs = getSharedPreferences(SAVE_GAME, MODE_PRIVATE);
String restoredText = prefs.getString("game", null);
if (restoredText != null) {
String name = prefs.getString("game", "state");// "state" is the default value.
}
As #Mariano said: SharedPreferences is a good place to save things. The advantage of SharedPreferences is that it doesn't require any additional permissions.
There are other choices though, depending on your needs. You can write to external storage (which requires the WRITE_EXTERNAL) permission and perhaps much more interesting for you is using the service that Google provides for saving game state. There are some nice examples on there for you to follow.
secondScreen.java
public class secondScreen extends Activity implements View.OnClickListener {
//drawables
int res[] = new int[] {R.drawable.brownbars,R.drawable.centeredorangedot, R.drawable.dots, R.drawable.greenlines, R.drawable.lightbulb, R.drawable.orangedots, R.drawable.orangelines, R.drawable.tree, R.drawable.yellow, R.drawable.yellowwithred, R.drawable.brownbars,R.drawable.centeredorangedot,
R.drawable.dots, R.drawable.greenlines, R.drawable.lightbulb, R.drawable.orangedots, R.drawable.orangelines, R.drawable.tree, R.drawable.yellow, R.drawable.yellowwithred};
int lay1, lay2, shuffleCount = 0, gameCount = 0;
ImageButton first, second;
LinearLayout layout;
ImageButton b1;
ImageButton b2;
ImageButton b3;
ImageButton b4;
ImageButton b5;
ImageButton b6;
ImageButton b7;
ImageButton b8;
ImageButton b9;
ImageButton b10;
ImageButton b11;
ImageButton b12;
ImageButton b13;
ImageButton b14;
ImageButton b15;
ImageButton b16;
ImageButton b17;
ImageButton b18;
ImageButton b19;
ImageButton b20;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Intent activityThatCalled = getIntent();
//initialize all buttons in game
b1 = (ImageButton) findViewById(R.id.b1);
b2 = (ImageButton) findViewById(R.id.b2);
b3 = (ImageButton) findViewById(R.id.b3);
b4 = (ImageButton) findViewById(R.id.b4);
b5 = (ImageButton) findViewById(R.id.b5);
b6 = (ImageButton) findViewById(R.id.b6);
b7 = (ImageButton) findViewById(R.id.b7);
b8 = (ImageButton) findViewById(R.id.b8);
b9 = (ImageButton) findViewById(R.id.b9);
b10 = (ImageButton) findViewById(R.id.b10);
b11 = (ImageButton) findViewById(R.id.b11);
b12 = (ImageButton) findViewById(R.id.b12);
b13 = (ImageButton) findViewById(R.id.b13);
b14 = (ImageButton) findViewById(R.id.b14);
b15 = (ImageButton) findViewById(R.id.b15);
b16 = (ImageButton) findViewById(R.id.b16);
b17 = (ImageButton) findViewById(R.id.b17);
b18 = (ImageButton) findViewById(R.id.b18);
b19 = (ImageButton) findViewById(R.id.b19);
b20 = (ImageButton) findViewById(R.id.b20);
iconRandomizer();
};
public void randomCheck(ImageButton btn, int image) {
if (gameCount < 2) {
gameCount++;
if (gameCount == 1) {
first = btn;
lay1 = image;
} else {
second = btn;
lay2 = image;
if (lay1 != lay2) {
ArrayList<View> touchables = layout.getTouchables();
for(View touchable : touchables) {
if (touchable instanceof Button)
((Button) touchable).setEnabled(false);
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
first.setImageResource(R.color.material_blue_grey_800);
second.setImageResource(R.color.material_blue_grey_800);
first.setEnabled(true);
second.setEnabled(true);
}
}, 1000);
}
gameCount = 0;
}
}
}
static void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public void iconRandomizer() {
for (int i = 0; i < 10; i++)
System.out.println(res[i]);
shuffleArray(res);
for (int i = 0; i < 10; i++)
System.out.println(res[i]);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b10.setOnClickListener(this);
b11.setOnClickListener(this);
b12.setOnClickListener(this);
b13.setOnClickListener(this);
b14.setOnClickListener(this);
b15.setOnClickListener(this);
b16.setOnClickListener(this);
b17.setOnClickListener(this);
b18.setOnClickListener(this);
b19.setOnClickListener(this);
b20.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.b1:
b1.setImageResource(res[0]);
b1.setEnabled(false);
randomCheck(b1, res[0]);
break;
case R.id.b2:
b2.setImageResource(res[1]);
b2.setEnabled(false);
randomCheck(b2,res[1]);
break;
case R.id.b3:
b3.setImageResource(res[2]);
b3.setEnabled(false);
randomCheck(b3, res[2]);
break;
case R.id.b4:
b4.setImageResource(res[3]);
b4.setEnabled(false);
randomCheck(b4, res[3]);
break;
case R.id.b5:
b5.setImageResource(res[4]);
b5.setEnabled(false);
randomCheck(b5, res[4]);
break;
case R.id.b6:
b6.setImageResource(res[5]);
b6.setEnabled(false);
randomCheck(b6, res[5]);
break;
case R.id.b7:
b7.setImageResource(res[6]);
b7.setEnabled(false);
randomCheck(b7, res[6]);
break;
case R.id.b8:
b8.setImageResource(res[7]);
b8.setEnabled(false);
randomCheck(b8, res[7]);
break;
case R.id.b9:
b9.setImageResource(res[8]);
b9.setEnabled(false);
randomCheck(b9, res[8]);
break;
case R.id.b10:
b10.setImageResource(res[9]);
b10.setEnabled(false);
randomCheck(b10, res[9]);
break;
case R.id.b11:
b11.setImageResource(res[10]);
b11.setEnabled(false);
randomCheck(b11, res[10]);
break;
case R.id.b12:
b12.setImageResource(res[11]);
b12.setEnabled(false);
randomCheck(b12, res[11]);
case R.id.b13:
b13.setImageResource(res[12]);
b13.setEnabled(false);
randomCheck(b13, res[12]);
break;
case R.id.b14:
b14.setImageResource(res[13]);
b14.setEnabled(false);
randomCheck(b14, res[13]);
case R.id.b15:
b15.setImageResource(res[14]);
b15.setEnabled(false);
randomCheck(b15, res[14]);
break;
case R.id.b16:
b16.setImageResource(res[15]);
b16.setEnabled(false);
randomCheck(b16, res[15]);
break;
case R.id.b17:
b17.setImageResource(res[16]);
b17.setEnabled(false);
randomCheck(b17, res[16]);
break;
case R.id.b18:
b18.setImageResource(res[17]);
b18.setEnabled(false);
randomCheck(b18, res[17]);
break;
case R.id.b19:
b19.setImageResource(res[18]);
b19.setEnabled(false);
randomCheck(b19, res[18]);
break;
case R.id.b20:
b20.setImageResource(res[19]);
b20.setEnabled(false);
randomCheck(b20, res[19]);
break;
}
}
//}
}
error log:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.widget.LinearLayout.getTouchables()' on a null object reference
at eagle.abhishekravi.abhishek.eagle.secondScreen.randomCheck(secondScreen.java:111)
at eagle.abhishekravi.abhishek.eagle.secondScreen.onClick(secondScreen.java:232)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I am making a memory match game, and I need to disable all buttons after the second Button is tapped. Then after the check is complete, it will resume normally. I have got the checks to work but still need the buttons disabled in this case where user taps another Button between the 1 second pause.Let me know if you need more information. Thanks!!
Based on the information provided by the error message, it looks like layout is null. If you're still having trouble, the code where layout is being set would be very helpful.
Your layout should be accessed via R.layout.my_layout.
I want to add a wait till, Button (Enter) is pressed function to my code But i am still new to this.
I know there are some errors in my code I was playing around with it, But what I want to do is when I press the line Button I want it to display Input X,Y,Z then wait till enter is pressed to execute the rest of my code I want to add in. How would I implement something like this in my code?
Here is my MainActivity Class:
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button enter = (Button) findViewById(R.id.enter);
Button line = (Button) findViewById(R.id.line);
Button arc = (Button) findViewById(R.id.arc);
line.setOnClickListener(this);
enter.setOnClickListener(this);
arc.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView vector = (TextView) findViewById(R.id.point);
TextView index = (TextView) findViewById(R.id.index);
TextView info = (TextView) findViewById(R.id.info);
EditText cl = (EditText) findViewById(R.id.editText1);
DrawingUtils call = new DrawingUtils();
switch (v.getId()) {
case R.id.line:
info.setText("Input X,Y,Z");
// This Is Where the Wait Function Will GO
vector.setText(call.addVertice());
index.setText("1");
break;
case R.id.enter:
String In = cl.getText().toString();
call.setInputCoords(In);
break;
case R.id.arc:
info.setText("Enter Vertice1 ");
// Code for entering Vertice1(Also has wait function)
info.setText("Enter Vertice2");
// Code for entering Vertice2(Also has wait function)
info.setText("Enter Height");
//Code for entering Height(Also has wait function)
}
}
}
Here is my DrawingUtils Class:
public class DrawingUtils {
String inputCoords;
String[] vertice;
public String getInputCoords() {
return inputCoords;
}
public void setInputCoords(String inputCoords) {
this.inputCoords = inputCoords;
}
public String addVertice() {
int i = 0;
vertice = inputCoords.split(",");
return vertice[i];
}
}
I think this is what you are after. Apologies if not!
Use a boolean flag to handle state within your app. This way you can execute different code if something has happened.
boolean enterPressed = false;
#Override
public void onClick(View v) {
TextView vector = (TextView) findViewById(R.id.point);
TextView index = (TextView) findViewById(R.id.index);
TextView info = (TextView) findViewById(R.id.info);
EditText cl = (EditText) findViewById(R.id.editText1);
DrawingUtils call = new DrawingUtils();
switch (v.getId()) {
case R.id.line:
if (enterPressed) {
vector.setText(call.addVertice());
index.setText("1");
}
else {
info.setText("Input X,Y,Z");
}
break;
case R.id.enter:
String In = cl.getText().toString();
call.setInputCoords(In);
enterPressed = true;
break;
case R.id.arc:
info.setText("Enter Vertice1 ");
// Code for entering Vertice1
info.setText("Enter Vertice2");
// Code for entering Vertice2
}
}
I was trying to make a rating type questions and i want to set the values in the radio button.. but i can't compute for the sum of the checked buttons.. this is my code..
public class MainActivity extends Activity {
public RadioButton r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
public RadioGroup rg1,rg2;
public TextView tv1;
public Button btn1;
public static int a,b,c,d,e,a1,b1,c1,d1,e1,total;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r0 = (RadioButton)findViewById(R.id.radio0);
r1 = (RadioButton)findViewById(R.id.radio1);
r2 = (RadioButton)findViewById(R.id.radio2);
r3 = (RadioButton)findViewById(R.id.radio3);
r4 = (RadioButton)findViewById(R.id.radio4);
r5 = (RadioButton)findViewById(R.id.radio5);
r6 = (RadioButton)findViewById(R.id.radio6);
r7 = (RadioButton)findViewById(R.id.radio7);
r8 = (RadioButton)findViewById(R.id.radio8);
r9 = (RadioButton)findViewById(R.id.radio9);
btn1 = (Button)findViewById(R.id.button1);
tv1 = (TextView)findViewById(R.id.textView7);
rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
rg2 = (RadioGroup)findViewById(R.id.radioGroup2);
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
switch(rg1.getCheckedRadioButtonId()){
case R.id.radio0:
if (r0.isChecked()){
a =1;
}
break;
case R.id.radio1:
if (r1.isChecked()){
b = 2;
}
break;
case R.id.radio2:
if (r2.isChecked()){
c = 3;
}
break;
case R.id.radio3:
if (r3.isChecked()){
d = 4;
}
break;
case R.id.radio4:
if (r4.isChecked()){
e = 5;
}
break;
}
//no. 2
switch(rg2.getCheckedRadioButtonId()){
case R.id.radio5:
if (r5.isChecked()){
a1=1;
}
break;
case R.id.radio6:
if (r6.isChecked()){
b1 = 2;
}
break;
case R.id.radio7:
if (r7.isChecked()){
c1 = 3;
}
break;
case R.id.radio8:
if (r8.isChecked()){
d1 = 4;
}
break;
case R.id.radio9:
if (r9.isChecked()){
e1 = 5;
}
break;
}
// i want to get the two checked radio button and output the result.. pls help me
total = rg1.getCheckedRadioButtonId() + rg2.getCheckedRadioButtonId();
tv1.setText("result: "+total);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Look at this:
total = rg1.getCheckedRadioButtonId() + rg2.getCheckedRadioButtonId();
you are trying to add RadioButtonId's which are radio1, radio2, etc.
There are likely many ways to do this but I think I would create a sum variable that adds the chosen number to it.
case R.id.radio5:
if (r5.isChecked()){
//a1=1;
sum += 1;
}
and finally
tv1.setText("result: "+ sum);
you would need to reset sum each time so it doesn't keep adding to previous.