Why isn't this code working? - java

I'm making a very simple calculator on Android. It shows no error on eclipse, but when I run it on my Android device, it stops working on clicking the buttons.
Here's my XML:
<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.simplecalculator.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="#string/enter_a_no"
android:inputType="numberSigned|numberDecimal"
/>
<requestFocus />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/plus"
android:layout_alignBottom="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:text="#string/minus"
android:onClick="minusClick" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/plus"
android:layout_below="#+id/plus"
android:text="#string/multiply"
android:onClick="multiplyClick"/>
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/multiply"
android:layout_alignBottom="#+id/multiply"
android:layout_alignLeft="#+id/minus"
android:text="#string/divide"
android:onClick="divideClick" />
<Button
android:id="#+id/equals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/divide"
android:layout_alignTop="#+id/minus"
android:layout_toRightOf="#+id/minus"
android:onClick="equalClick"
android:text="#string/equals" />
<Button
android:id="#+id/_1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/plus"
android:layout_below="#+id/editText1"
android:layout_marginTop="14dp"
android:text="#string/_1"
android:onClick="_1click"/>
<Button
android:id="#+id/_2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_1"
android:layout_alignBottom="#+id/_1"
android:layout_toRightOf="#+id/_1"
android:text="#string/_2"
android:onClick="_2click"/>
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/divide"
android:layout_alignBottom="#+id/divide"
android:layout_toRightOf="#+id/divide"
android:onClick="clearClick"
android:text="#string/ce" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/_1"
android:layout_marginLeft="33dp"
android:layout_marginTop="127dp"
android:hint="#string/plus"
android:onClick="plusClick"
android:text="#string/plus" />
<Button
android:id="#+id/_4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_1"
android:layout_toLeftOf="#+id/_2"
android:text="#string/_4"
android:onClick="_4click"/>
<Button
android:id="#+id/_5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_4"
android:layout_alignBottom="#+id/_4"
android:layout_alignLeft="#+id/_2"
android:text="#string/_5"
android:onClick="_5click" />
<Button
android:id="#+id/_6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_5"
android:layout_alignBottom="#+id/_5"
android:layout_toRightOf="#+id/_5"
android:text="#string/_6"
android:onClick="_6click"/>
<Button
android:id="#+id/_7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_4"
android:layout_toLeftOf="#+id/_5"
android:text="#string/_7"
android:onClick="_7click"/>
<Button
android:id="#+id/_8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_5"
android:layout_toLeftOf="#+id/_6"
android:text="#string/_8"
android:onClick="_8click"/>
<Button
android:id="#+id/_9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_5"
android:layout_toRightOf="#+id/_5"
android:text="#string/_9"
android:onClick="_9click"/>
<Button
android:id="#+id/_3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/_5"
android:layout_toRightOf="#+id/_2"
android:text="#string/_3"
android:onClick="_3click"/>
<Button
android:id="#+id/_0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_6"
android:layout_toRightOf="#+id/_6"
android:text="#string/_0"
android:onClick="_0click"/>
</RelativeLayout>
Here's my Java code:
package com.example.simplecalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private double vrble;
private double rslt;
private double vrble2;
String text;
EditText etext;
Button plus;
Button minus;
Button multiply;
Button divide;
Button equal;
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
vrble = 0;
rslt = 0;
etext = (EditText) findViewById(R.id.editText1);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal = (Button) findViewById(R.id.equals);
} else {
vrble = 0;
rslt = 0;
vrble2 = 0;
etext = (EditText) findViewById(R.id.editText1);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal = (Button) findViewById(R.id.equals);
}
}
public void _0click(View v) {
text = etext.getText().toString();
etext.setText(text + "0");
}
public void _1click(View v) {
text = etext.getText().toString();
etext.setText(text + "1");
}
public void _2click(View v) {
text = etext.getText().toString();
etext.setText(text + "2");
}
public void _3click(View v) {
text = etext.getText().toString();
etext.setText(text + "3");
}
public void _4click(View v) {
text = etext.getText().toString();
etext.setText(text + "4");
}
public void _5click(View v) {
text = etext.getText().toString();
etext.setText(text + "5");
}
public void _6click(View v) {
text = etext.getText().toString();
etext.setText(text + "6");
}
public void _7click(View v) {
text = etext.getText().toString();
etext.setText(text + "7");
}
public void _8click(View v) {
text = etext.getText().toString();
etext.setText(text + "8");
}
public void _9click(View v) {
text = etext.getText().toString();
etext.setText(text + "9");
}
public void plusClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble + vrble2;
}
public void minusClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble - vrble2;
}
public void multiplyClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble * vrble2;
}
public void divideClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble / vrble2;
}
public void clearClick(View v) {
vrble = 0;
rslt = 0;
etext.setText("0");
}
public void equalClick(View v) {
etext.setText("" + rslt);
}#
Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#
Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What should I do?!!?!

One issue that I can se is a NumberFormatException that you might get. See these lines in any of your method like plusClick():
vrble = Double.parseDouble(etext.getText().toString());
etext.setText(""); //setting EditText to empty
vrble2 = Double.parseDouble(etext.getText().toString()); // parsing the empty string to Double
You are setting the eText to "" and then you are parsing it to Double in the next line. This is throwing the error. You might need to clear your login for all the actions like plus minus etc.
For more, your stacktrace will be helpful.

Related

My app is crashing when I use swicth statement or if statement. I dont why, please help me out

I think is happening in the switch case or there is any small error in my code.
Please help me out
Here is my code:
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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/edText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180sp"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="230sp"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="230sp"
android:text="2"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="230sp"
android:text="3"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="280sp"
android:text="4" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="280sp"
android:text="5"/>
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="280sp"
android:text="6"/>
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="330sp"
android:text="7" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="330sp"
android:text="8"/>
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="330sp"
android:text="9"/>
<Button
android:id="#+id/buttonplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="380sp"
android:text="+" />
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="380sp"
android:text="0"/>
<Button
android:id="#+id/buttoneq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="380sp"
android:text="="/>
<Button
android:id="#+id/buttondiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="430sp"
android:text="/" />
<Button
android:id="#+id/buttonmin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="430sp"
android:text="-"/>
<Button
android:id="#+id/buttonmul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="430sp"
android:text="*"/>
</RelativeLayout>
MainActivity.java:
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int operator = 0;
int s1 = 0,s2 = 0,add = 0,min = 0,mul = 0,div = 0;
EditText tv;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,beq,bplus,bmin,bmul,bdiv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
b6 = (Button)findViewById(R.id.button6);
b7 = (Button)findViewById(R.id.button7);
b8 = (Button)findViewById(R.id.button8);
b9 = (Button)findViewById(R.id.button9);
b0 = (Button)findViewById(R.id.button0);
beq = (Button)findViewById(R.id.buttoneq);
bplus = (Button)findViewById(R.id.buttonplus);
bmin = (Button)findViewById(R.id.buttonmin);
bmul = (Button)findViewById(R.id.buttonmul);
bdiv = (Button)findViewById(R.id.buttondiv);
tv = (EditText) findViewById(R.id.edText);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "9");
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText( tv.getText() + "0");
}
});
bplus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 1;
}
});
bmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 2;
}
});
bmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 3;
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 4;
}
});
beq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s2 = Integer.parseInt(tv.getText().toString());
add = s1 + s2;
min = s1 - s2;
mul = s1 * s2;
div = s1 / s2;
switch (operator){
case 1:
tv.setText(add);
break;
case 2:
tv.setText(min);
break;
case 3:
tv.setText(mul);
break;
case 4:
tv.setText(div);
break;
}
}
});
}
}
I have added 15 buttons to my calculator and everything works properly except when I click on equal to the button. Every button does works properly and prints the proper number into the textView when clicked but still when I click on the equal to button, my app just shuts down itself whenever I click on the equal to button on my calculator and nothing happens.
It seems like you need to parse integers to strings before you set the text. Try using String.valueOf() to parse add, min, mul, and div to strings.
Also, if you post your logcat, it'll be much easier to figure out what the problem is as it tells you what and where the error is.
You can not set integer value on Textview, only String will be acceptable. Try with the below code.
switch (operator){
case 1:
tv.setText(""+add);
break;
case 2:
tv.setText(""+min);
break;
case 3:
tv.setText(""+mul);
break;
case 4:
tv.setText(""+div);
break;
}
if you give an int value to setText(), it will be seen as a resId,so ,if system can not find this id, will throw new NotFoundException("String resource ID #0x"
+ Integer.toHexString(id));
Change eq listener to
beq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(tv.getText().toString().length()==0)
return;
s2 = Integer.parseInt(tv.getText().toString());
add = s1 + s2;
min = s1 - s2;
mul = s1 * s2;
div = s1 / s2;
switch (operator) {
case 1:
tv.setText(String.valueOf(add));
break;
case 2:
tv.setText(String.valueOf(min));
break;
case 3:
tv.setText(String.valueOf(mul));
break;
case 4:
tv.setText(String.valueOf(div));
break;
}
}
});
Do not set integer value to text view directly. First cast it to string.
Try replacing this in your code.

My android studio made apk is not working properly

I am using Android Studio 3.2 and My app is showing up but the buttons are not triggering actions please suggest any code changes so that they would trigger events on the app
The MainActivity code is :
package com.example.myfalconcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView expression;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button num1 = findViewById(R.id.num1);
expression = findViewById(R.id.expression);
Button num2 = findViewById(R.id.num2);
Button num3 = findViewById(R.id.num3);
Button num4 = findViewById(R.id.num4);
Button num5 = findViewById(R.id.num5);
Button num6 = findViewById(R.id.num6);
Button num7 = findViewById(R.id.num7);
Button num8 = findViewById(R.id.num8);
Button num9 = findViewById(R.id.num9);
Button num0 = findViewById(R.id.num0);
Button plus = findViewById(R.id.plus);
Button minus = findViewById(R.id.minus);
Button equalsTo = findViewById(R.id.equalsTo);
Button closeBrace = findViewById(R.id.closeBrace);
Button openBrace = findViewById(R.id.openBrace);
Button clear = findViewById(R.id.clear);
Button delete = findViewById(R.id.delete);
Button exponential = findViewById(R.id.exponential);
Button divideBy = findViewById(R.id.divideBy);
num1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "1");
}
});
num2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "2");
}
});
num3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "3");
}
});
num4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "4");
}
});
num5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "5");
}
});
num6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "6");
}
});
num7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "7");
}
});
num8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "8");
}
});
num9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "9");
}
});
num0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "0");
}
});
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "+");
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "-");
}
});
exponential.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "*");
}
});
divideBy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "/");
}
});
equalsTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SimpleMathSolver mathSolver = new SimpleMathSolver(expression.getText().toString());
try {
expression.setText(String.valueOf(mathSolver.solve()));
} catch (Exception e) {
expression.setText("Input Not Valid");
}
}
});
closeBrace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + ")");
}
});
openBrace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "(");
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText("");
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tempExpression = expression.getText().toString().substring(0, expression.getText().toString().length()-1);
expression.setText(tempExpression);
}
});
}
}
The Main Activity file is depending on another file:
package com.example.myfalconcalculator;
import java.util.Scanner;
import java.util.Stack;
public class SimpleMathSolver {
private String inputExp;
private final char PLUS = '+';
private final char MINUS = '-';
private final char MULTIPLY = '*';
private final char DIVIDE = '/';
private final char OPENBRACE = '(';
private final char CLOSEBRACE = ')';
public SimpleMathSolver(String inputExp) {
this.inputExp = inputExp;
}
public int solve() throws Exception {
String withoutBraceInputExp = solveBraces(this.inputExp);
return solveMath(withoutBraceInputExp);
}
private String solveBraces(String inputExp) throws Exception {
String output = inputExp;
int closeBraceIndex = output.indexOf(CLOSEBRACE);
while (closeBraceIndex >= 0) {
int openBraceIndex = output.substring(0, closeBraceIndex).lastIndexOf(OPENBRACE);
String solveStr = output.substring(openBraceIndex + 1, closeBraceIndex);
int value = solveMath(solveStr);
output = output.substring(0, openBraceIndex) + value
+ output.substring(closeBraceIndex + 1, output.length());
closeBraceIndex = output.indexOf(CLOSEBRACE);
}
return output;
}
private int solveMath(String inputExp) throws Exception {
Stack<Integer> numberStack = new Stack<Integer>();
Stack<Character> symbolStack = new Stack<Character>();
updateStacks(inputExp, numberStack, symbolStack);
solveMathSymbol(numberStack, symbolStack, DIVIDE);
solveMathSymbol(numberStack, symbolStack, MULTIPLY);
solveMathSymbol(numberStack, symbolStack, MINUS);
solveMathSymbol(numberStack, symbolStack, PLUS);
return numberStack.pop();
}
private void solveMathSymbol(Stack<Integer> numberStack, Stack<Character> symbolStack, char symbol) {
Stack<Integer> tempNumberStack = new Stack<Integer>();
Stack<Character> tempSymbolStack = new Stack<Character>();
while (symbolStack.size() > 0) {
char ch = symbolStack.pop();
if (ch == symbol) {
int pop1 = numberStack.pop();
int pop2 = numberStack.pop();
int out = 0;
switch (symbol) {
case PLUS:
out = pop2 + pop1;
break;
case MINUS:
out = pop2 - pop1;
break;
case MULTIPLY:
out = pop2 * pop1;
break;
case DIVIDE:
out = pop2 / pop1;
break;
}
numberStack.push(out);
} else {
tempSymbolStack.push(ch);
int numpop = numberStack.pop();
tempNumberStack.push(numpop);
}
}
while (tempSymbolStack.size() > 0) {
char charpop = tempSymbolStack.pop();
symbolStack.push(charpop);
int numpop = tempNumberStack.pop();
numberStack.push(numpop);
}
}
private void updateStacks(String inputExp, Stack<Integer> numberStack, Stack<Character> symbolStack)
throws Exception {
int number = 0;
for (int i = 0; i < inputExp.length(); i++) {
char ch = inputExp.charAt(i);
if (Character.isDigit(ch)) {
number = number * 10 + Integer.parseInt(String.valueOf(ch));
} else {
numberStack.push(number);
number = 0;
if (ch == PLUS || ch == MINUS || ch == MULTIPLY || ch == DIVIDE) {
symbolStack.push(ch);
} else {
throw new Exception("Unknown Math Symbol " + ch);
}
}
}
numberStack.push(number);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String retryOp = "N";
do {
System.out.println("Enter Math Expression");
String inputExp = scanner.nextLine();
SimpleMathSolver solver = new SimpleMathSolver(inputExp);
try {
System.out.println("Result: " + solver.solve());
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Do you want to try this again");
retryOp = scanner.nextLine();
} while (retryOp.equalsIgnoreCase("Y"));
scanner.close();
System.exit(0);
}
}
this is the activity main.xml file:
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="#+id/closeBrace"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="1dp"
android:text=")"
app:layout_constraintBottom_toTopOf="#+id/equalsTo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/openBrace"
app:layout_constraintTop_toBottomOf="#+id/divideBy" />
<Button
android:id="#+id/openBrace"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginBottom="1dp"
android:text="("
app:layout_constraintBottom_toTopOf="#+id/equalsTo"
app:layout_constraintEnd_toStartOf="#+id/closeBrace"
app:layout_constraintStart_toEndOf="#+id/num9"
app:layout_constraintTop_toBottomOf="#+id/exponential" />
<Button
android:id="#+id/divideBy"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="/"
app:layout_constraintBottom_toTopOf="#+id/closeBrace"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/exponential"
app:layout_constraintTop_toBottomOf="#+id/minus" />
<Button
android:id="#+id/exponential"
android:layout_width="74dp"
android:layout_height="76dp"
android:text="*"
app:layout_constraintBottom_toTopOf="#+id/openBrace"
app:layout_constraintEnd_toStartOf="#+id/divideBy"
app:layout_constraintStart_toEndOf="#+id/num6"
app:layout_constraintTop_toBottomOf="#+id/plus" />
<Button
android:id="#+id/plus"
android:layout_width="74dp"
android:layout_height="76dp"
android:text="+"
app:layout_constraintBottom_toTopOf="#+id/exponential"
app:layout_constraintEnd_toStartOf="#+id/minus"
app:layout_constraintStart_toEndOf="#+id/num3"
app:layout_constraintTop_toBottomOf="#+id/delete" />
<Button
android:id="#+id/minus"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="-"
app:layout_constraintBottom_toTopOf="#+id/divideBy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/plus"
app:layout_constraintTop_toBottomOf="#+id/delete" />
<Button
android:id="#+id/num0"
android:layout_width="252dp"
android:layout_height="76dp"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="5dp"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/equalsTo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num8" />
<Button
android:id="#+id/num5"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginBottom="2dp"
android:text="5"
app:layout_constraintBottom_toTopOf="#+id/num8"
app:layout_constraintEnd_toStartOf="#+id/num6"
app:layout_constraintStart_toEndOf="#+id/num4"
app:layout_constraintTop_toBottomOf="#+id/num2" />
<Button
android:id="#+id/num4"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="1dp"
android:text="4"
app:layout_constraintBottom_toTopOf="#+id/num7"
app:layout_constraintEnd_toStartOf="#+id/num5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num1" />
<Button
android:id="#+id/num6"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="2dp"
android:text="6"
app:layout_constraintBottom_toTopOf="#+id/num9"
app:layout_constraintEnd_toStartOf="#+id/exponential"
app:layout_constraintStart_toEndOf="#+id/num5"
app:layout_constraintTop_toBottomOf="#+id/num3" />
<Button
android:id="#+id/num8"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginTop="1dp"
android:text="8"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/num9"
app:layout_constraintStart_toEndOf="#+id/num7"
app:layout_constraintTop_toBottomOf="#+id/num5" />
<Button
android:id="#+id/num7"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp"
android:text="7"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/num8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num4" />
<Button
android:id="#+id/num9"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:text="9"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/openBrace"
app:layout_constraintStart_toEndOf="#+id/num8"
app:layout_constraintTop_toBottomOf="#+id/num6" />
<Button
android:id="#+id/num3"
android:layout_width="84dp"
android:layout_height="76dp"
android:text="3"
app:layout_constraintBottom_toTopOf="#+id/num6"
app:layout_constraintEnd_toStartOf="#+id/plus"
app:layout_constraintStart_toEndOf="#+id/num2"
app:layout_constraintTop_toBottomOf="#+id/clear" />
<Button
android:id="#+id/num2"
android:layout_width="84dp"
android:layout_height="76dp"
android:text="2"
app:layout_constraintBottom_toTopOf="#+id/num5"
app:layout_constraintEnd_toStartOf="#+id/num3"
app:layout_constraintStart_toEndOf="#+id/num1"
app:layout_constraintTop_toBottomOf="#+id/clear" />
<Button
android:id="#+id/equalsTo"
android:layout_width="150dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="5dp"
android:text="="
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/num0"
app:layout_constraintTop_toBottomOf="#+id/num9" />
<Button
android:id="#+id/clear"
android:layout_width="252dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="8dp"
android:text="Clear"
app:layout_constraintEnd_toStartOf="#+id/delete"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/expression" />
<TextView
android:id="#+id/expression"
android:layout_width="400dp"
android:layout_height="331dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="8dp"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/clear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/delete"
android:layout_width="148dp"
android:layout_height="76dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="Delete"
app:layout_constraintBottom_toTopOf="#+id/plus"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/clear"
app:layout_constraintTop_toBottomOf="#+id/expression" />
<Button
android:id="#+id/num1"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:text="1"
app:layout_constraintBottom_toTopOf="#+id/num4"
app:layout_constraintEnd_toStartOf="#+id/num2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clear" />
</android.support.constraint.ConstraintLayout>
please suggest how to solve this problem. The
you can't do final Button num1 = findViewById(R.id.num1); outside of oncreate, all your findViewById(R.id.foo); statements have to be inside onCreate (or at least after onCreate has been called)
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById<Button>(R.id.foo)
}

code for image run in arraylist

i do array list with images thet i want them to run with 2 buttons thet go next pic and beck pic i dont know the code for this i thx all for the time you spend for help.
ackage com.example.hanansanag.mytourneyccreator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by ssh on 25/12/2016.
*/
public class Players extends AppCompatActivity implements View.OnClickListener {
protected Button btnNext, btnBack;
protected String fname;
protected String Lname;
protected String team;
protected ImageView iv;
protected ArrayList array_image;
int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_pic);
ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.bacelona);
array_image.add(R.drawable.athlethiko);
array_image.add(R.drawable.arsenak);
array_image.add(R.drawable.chelsea);
array_image.add(R.drawable.dortmond);
array_image.add(R.drawable.city);
array_image.add(R.drawable.bayernunchen);
array_image.add(R.drawable.intermilan);
array_image.add(R.drawable.psj);
array_image.add(R.drawable.realmadrid);
array_image.add(R.drawable.leverpool);
array_image.add(R.drawable.milan);
array_image.add(R.drawable.juventus);
array_image.add(R.drawable.ashkelon);
array_image.add(R.drawable.macabiheifa);
array_image.add(R.drawable.macabitelaviv);
array_image.add(R.drawable.beitaryeroshlaim);
array_image.add(R.drawable.apoelbersheva);
iv = (ImageView) findViewById(R.id.imageView);
btnNext = (Button) findViewById(R.id.btnNextPic);
btnBack = (Button) findViewById(R.id.btnBeckPic);
btnBack.setOnClickListener(this);
btnNext.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
}
**what can i do here in this line of code thet the pic will move : **
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
this is the 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"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/teamimage">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView2" />
<ImageView
android:layout_height="80dp"
android:id="#+id/imageView"
android:layout_width="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:id="#+id/Lname"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/imageView" />
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView3" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true">
<Button
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnBeckPic"
android:background="#drawable/btnbeck"
android:layout_marginStart="30dp"
android:layout_alignBottom="#+id/Lname"
android:layout_toEndOf="#+id/textView2" />
<Button
android:background="#drawable/btnnext"
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnNextPic"
android:layout_below="#+id/imageView"
android:layout_toEndOf="#+id/btnBeckPic"
android:layout_marginStart="14dp" />
</GridLayout>
</RelativeLayout>
int currentImage = 0;
String[] strArr = new String[array_image.size()];
strArr = array_image.toArray(stockArr);
#Override
public void onClick(View v) {
if (btnNext == v) {
currentImage++;
currentImage = currentImage % strArr .length;
iv.setImageResource(strArr .length[currentImage]);
} else if (btnBack == v) {
currentImage--;
currentImage = (currentImage + strArr .length) % strArr .length;
iv.setImageResource(strArr[currentImage]);
}
}

How do I show complete calculation in textview?

How do I show complete calculation in textview using parenthesis? For example: 2+3-((4/2)*9)
I am making calculator app which uses parenthesis and remember calculations history and should display all math operation in textview.
Here is my code:
package com.example.calculater;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView textdisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textdisplay = (TextView) findViewById(R.id.editText1);
Button b1 = (Button) findViewById(R.id.btn1);
Button b2 = (Button) findViewById(R.id.btn2);
Button b3 = (Button) findViewById(R.id.btn3);
Button b4 = (Button) findViewById(R.id.btn4);
Button b5 = (Button) findViewById(R.id.btn5);
Button b6 = (Button) findViewById(R.id.btn6);
Button b7 = (Button) findViewById(R.id.btn7);
Button b8 = (Button) findViewById(R.id.btn8);
Button b9 = (Button) findViewById(R.id.btn9);
Button b0 = (Button) findViewById(R.id.btn0);
Button multiply1 = (Button) findViewById(R.id.multiply);
Button divide1 = (Button) findViewById(R.id.divide);
Button plus1 = (Button) findViewById(R.id.plus);
Button minus1 = (Button) findViewById(R.id.minus);
Button equal1 = (Button) findViewById(R.id.equal);
Button clear1 = (Button) findViewById(R.id.clear);
Button back1 = (Button) findViewById(R.id.backspace);
Button dot1 = (Button) findViewById(R.id.decimal);
Button plusminus1 = (Button) findViewById(R.id.plusminus);
Button history1= (Button) findViewById(R.id.history);
Button open1 = (Button) findViewById(R.id.open);
Button close1 = (Button) findViewById(R.id.close);
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);
b0.setOnClickListener(this);
multiply1.setOnClickListener(this);
divide1.setOnClickListener(this);
plus1.setOnClickListener(this);
minus1.setOnClickListener(this);
equal1.setOnClickListener(this);
clear1.setOnClickListener(this);
back1.setOnClickListener(this);
dot1.setOnClickListener(this);
plusminus1.setOnClickListener(this);
history1.setOnClickListener(this);
open1.setOnClickListener(this);
close1.setOnClickListener(this);
}
#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;
}
int clear_flag = 0;
String sign_flag = "";
Double total = 0.0;
int last_button = 0;
public void shownum (String number){
if(clear_flag==1){
textdisplay.setText("");
clear_flag=0;
}
else if(textdisplay.getText()=="0"){
textdisplay.setText("");
}
textdisplay.setText(textdisplay.getText() + number);
}
public void showsign(String sign){
if(last_button==R.id.plus || last_button==R.id.minus || last_button==R.id.multiply
|| last_button==R.id.divide){
}
else{
clear_flag = 1;//set flag
Double newNumber = Double.parseDouble(textdisplay.getText().toString());
if(sign_flag == "" || sign_flag == "="){
total = newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "+"){
total = total + newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "-"){
total = total - newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "*"){
total = total*newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "/"){
total = total/newNumber;
textdisplay.setText(total.toString());
}}
sign_flag = sign;
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.btn0){
shownum ("0");
}
else if(v.getId() == R.id.btn1){
shownum ("1");
}
else if(v.getId() == R.id.btn2){
shownum ("2");
}
else if(v.getId() == R.id.btn3){
shownum ("3");
}
else if(v.getId() == R.id.btn4){
shownum ("4");
}
else if(v.getId() == R.id.btn5){
shownum ("5");
}
else if(v.getId() == R.id.btn6){
shownum ("6");
}
else if(v.getId() == R.id.btn7){
shownum ("7");
}
else if(v.getId() == R.id.btn8){
shownum ("8");
}
else if(v.getId() == R.id.btn9){
shownum ("9");
}
else if(v.getId() == R.id.clear){
textdisplay.setText("");////ORIGINALLY ITS WAS 0 ""
total = 0.0;
sign_flag = "";
}
else if(v.getId() == R.id.decimal){
if(clear_flag==1){
textdisplay.setText("");
clear_flag = 0;
}
if(textdisplay.getText().toString().indexOf(".")<0){
textdisplay.setText(textdisplay.getText() + ".");
}}
else if(v.getId() == R.id.backspace){
if(textdisplay.getText().toString().length()>0){
int start = 0;
int end = textdisplay.getText().toString().length()-1;
String newText = textdisplay.getText().toString().substring(start,end);
textdisplay.setText(newText);
}}
else if(v.getId() == R.id.plus){
showsign("+");
}
else if(v.getId() == R.id.minus){
showsign("-");
}
else if(v.getId() == R.id.multiply){
showsign("*");
}
else if(v.getId() == R.id.divide){
showsign("/");
}
else if(v.getId() == R.id.equal){
Double newNumber = Double.parseDouble(textdisplay.getText().toString());
if(sign_flag == "+"){
total = total+newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "-"){
total = total-newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "*"){
total = total*newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "/"){
total = total/newNumber;
textdisplay.setText(total.toString());
}
sign_flag = "=";
} //when minus is pressed before any number input, applications closes.
else if (v.getId() == R.id.plusminus){
String number = textdisplay.getText().toString();
if(number == null) return; //exits function
if(number.equals("")) return; //exits function
// textdisplay.setText("Please enter number first then press +/-");
Double newNumber = Double.parseDouble(number);
total = newNumber * (-1);
textdisplay.setText(total.toString());
}
else if (v.getId()== R.id.open){
}
else if (v.getId()== R.id.close){
}
last_button = v.getId();
}}
Here is my XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:layout_weight="0.58"
android:inputType="numberDecimal" >
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<Button
android:id="#+id/backspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="#+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="#+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="#+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="#+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="#+id/plusminus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+/-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="#+id/decimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="." />
<Button
android:id="#+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="( " />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.58" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.20" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.04"
android:orientation="vertical" >
<Button
android:id="#+id/history"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:text="History" />
<Button
android:id="#+id/equal"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:text="#string/_" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
What codeMagic said, store each in a variable and then maybe make one final String variable to hold it with String.format. So maybe
String finalString = String.format("This is the result %s and %s" ,
variableName, variableName);
then
textdisplay.setText(finalString);

How would i make a button to switch where points go in a counter?

I am currently a high school student who decided to try and take up Android dev for fun, but I am stumped. I have an image button for blue team and for red team. The score goes up automatically for blue team. What i dont know how to do is when you hit the red button, the image buttons make the red teams score go up and vice versa.
Here is my java code
package com.example.puremmacompetitionjiujitsuscorer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
class MainActivity extends Activity {
private int blueScore = 0;
private int redScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton sweep = (ImageButton) findViewById(R.id.imageButton5);
final ImageButton pass = (ImageButton) findViewById(R.id.imageButton8);
final ImageButton mount = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton backMount = (ImageButton) findViewById(R.id.imageButton3);
final ImageButton kneeOnBelly = (ImageButton) findViewById(R.id.imageButton7);
final ImageButton takeDown = (ImageButton) findViewById(R.id.imageButton6);
final TextView blueScoreCount = (TextView) findViewById(R.id.blueScore1);
takeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
sweep.setOnClickListener(new View.OnClickListener(){
public void onClick(View w) {
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
pass.setOnClickListener(new View.OnClickListener(){
public void onClick(View q){
blueScore += 3;
blueScoreCount.setText("" + blueScore);
}
});
mount.setOnClickListener(new View.OnClickListener(){
public void onClick(View t){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
backMount.setOnClickListener(new View.OnClickListener(){
public void onClick(View s){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
kneeOnBelly.setOnClickListener(new View.OnClickListener(){
public void onClick(View g){
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
};
#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;
}
Here is my XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/purebig" >
<TextView
android:id="#+id/redScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint"
android:maxLength="2"
android:textIsSelectable="false" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/stop" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/play"/>
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton2"
android:layout_alignParentLeft="true"
android:src="#drawable/kneeonbelly"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageButton7"
android:src="#drawable/backmount"
android:onClick="addFour" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton3"
android:layout_alignParentRight="true"
android:src="#drawable/mount"
android:onClick="addFour" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton7"
android:layout_centerHorizontal="true"
android:src="#drawable/pass"
android:onClick="addThree" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageButton8"
android:src="#drawable/takedown"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton8"
android:layout_below="#+id/imageButton8"
android:src="#drawable/sweep"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/imageButton9"
android:src="#drawable/red" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/imageButton1"
android:src="#drawable/blue" />
<TextView
android:id="#+id/blueScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/hint"
android:maxLength="2"
android:textIsSelectable="false"/>
<requestFocus />
</RelativeLayout>
I think I possibly understand you. If I do, you can use a flag to see which Button was pressed. So the flow would be like
Red button pressed -> flag = red -> Score Button pressed -> red score += score
In code it would be like
String flag = "";
public void onRedBtnClick(View v)
{
flag = "red";
}
public void scoreBtnClick(View v)
{
if (!flag.equals(""));
{
if ("red".equals(flag))
{
redScore += score;
}
if ("blue".equals(flag))
{
blueScore += score;
}
}
This is obviously done really quick and you will have to adjust your variables to your needs. But if I understand what you want then this would give you the basic logic

Categories