android calculator buttons doesnt work - java

I'm trying to make a basic android calculator. when i press button numbers show at edittext but nothing's happining. Thanks for any help.
here is my MainActivity.java`
public class MainActivity extends AppCompatActivity {
private EditText Scr;
private float NumerBf;
private String Operation;
private ButtonClickListener btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Scr=(EditText)findViewById(R.id.editText3);
Scr.setEnabled(false);
int idList[]={R.id.button0,R.id.button1,R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
R.id.button6,R.id.button7,R.id.button8,R.id.button9,R.id.buttonAdd,R.id.buttonC,R.id.buttonDivide,
R.id.buttonEqu,R.id.buttonMinus,R.id.buttonMultiply};
for(int id:idList){
View v=(View)findViewById(id);
v.setOnClickListener(btnClick);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void mMath(String str){
NumerBf=Float.parseFloat(Scr.getText().toString());
Operation =str;
Scr.setText("0");
}
public void getKeyboard(String str){
String SrcCurrent = Scr.getText().toString();
if(SrcCurrent.equals("0"))
SrcCurrent="";
SrcCurrent +=str;
Scr.setText(SrcCurrent);
}
public void mResult(){
float NumAf=Float.parseFloat(Scr.getText().toString());
float result=0;
if(Operation.equals("+")){
result=NumAf+NumerBf;
}
else if(Operation.equals("-")){
result=NumerBf-NumAf;
}
else if(Operation.equals("*")){
result=NumAf*NumerBf;
}
else if(Operation.equals("/")){
result=NumerBf/NumAf;
}
Scr.setText(String.valueOf(result));
}
private class ButtonClickListener implements View.OnClickListener {
public void onClick(View v){
switch(v.getId()){
case R.id.buttonC: //ekranı temizle
Scr.setText("0");
NumerBf=0;
Operation="";
break;
case R.id.buttonAdd:
mMath("+");
break;
case R.id.buttonMinus:
mMath("-");
break;
case R.id.buttonMultiply:
mMath("*");
break;
case R.id.buttonDivide:
mMath("/");
break;
case R.id.buttonEqu:
mResult();
break;
default:
String numb= ((Button) v).getText().toString();
getKeyboard(numb);
break;
}
}
}
`
and here is my content_main.xml`
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:textStyle="bold"
android:layout_above="#+id/button5"
android:layout_toRightOf="#+id/button5"
android:layout_toEndOf="#+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="#+id/buttonC"
android:textStyle="bold"
android:longClickable="true"
android:layout_alignTop="#+id/button3"
android:layout_alignRight="#+id/buttonDivide"
android:layout_alignEnd="#+id/buttonDivide"
android:layout_alignLeft="#+id/buttonDivide"
android:layout_alignStart="#+id/buttonDivide" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button1"
android:textStyle="bold"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:textStyle="bold"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:textStyle="bold"
android:layout_below="#+id/button3"
android:layout_toRightOf="#+id/button5"
android:layout_toEndOf="#+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:textStyle="bold"
android:layout_above="#+id/button8"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:textStyle="bold"
android:layout_above="#+id/button0"
android:layout_toRightOf="#+id/button7"
android:layout_toEndOf="#+id/button7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:textStyle="bold"
android:layout_below="#+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:textStyle="bold"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button8"
android:layout_toEndOf="#+id/button8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button0"
android:textStyle="bold"
android:layout_above="#+id/buttonMinus"
android:layout_alignLeft="#+id/button8"
android:layout_alignStart="#+id/button8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/buttonAdd"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/buttonMinus"
android:textStyle="bold"
android:layout_alignTop="#+id/buttonAdd"
android:layout_alignLeft="#+id/button0"
android:layout_alignStart="#+id/button0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="#+id/buttonMultiply"
android:textStyle="bold"
android:layout_alignBottom="#+id/buttonMinus"
android:layout_toRightOf="#+id/buttonMinus"
android:layout_toEndOf="#+id/buttonMinus" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="#+id/buttonDivide"
android:textStyle="bold"
android:layout_alignTop="#+id/buttonMultiply"
android:layout_toRightOf="#+id/buttonMultiply"
android:layout_toEndOf="#+id/buttonMultiply" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/buttonEqu"
android:textStyle="bold"
android:layout_below="#+id/buttonMinus"
android:layout_alignLeft="#+id/buttonMinus"
android:layout_alignStart="#+id/buttonMinus" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText3"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toLeftOf="#+id/button3"
android:gravity="right"
android:text="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:textStyle="bold"
android:layout_alignBottom="#+id/button3"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3" />
`

ButtonClickListener btnClick is not initialized. You are setting a null onClickListener. So it makes sence that pressing the buttons does nothing

Related

Simple Calculator app keeps crashes

I'm a beginner in Android. Just started working on a Calculator app and don't know why the app keeps crashing all the time.
I have Two Activities... MainActivity and CalculatorActivity
MainActivity:
public class MainActivity extends AppCompatActivity
{
Button calcubtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calcubtn = (Button) findViewById(R.id.calculatorbtn);
calcubtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent calculator = new Intent(MainActivity.this, CalculatorActivity.class);
startActivity(calculator);
}
});
}
}
MainActivity Layout:
<?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"
tools:context="com.example.rfkha.myfirstapp.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:textSize="27sp"
android:textColor="#android:color/holo_red_light"
android:text="#string/functions_text" />
<Button
android:id="#+id/calculatorbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginStart="43dp"
android:layout_marginTop="70dp"
android:text="#string/calcu_string"
/>
</RelativeLayout>
CalculatorActivity: (i.e child activity)
Here in the activity class, I've added implements View.OnclickListener . I found it in stackoverflow somewhere. I think the problem is from this thing. Also it required to used the keyword abstractto use it with the activity class, so i used that too.
public abstract class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, zerobtn, dotbtn, equalbtn, minusbtn, sumbtn, divbtn, mulbtn;
double num1, num2;
boolean sum, minus, div, mul;
EditText textarea= (EditText) findViewById(R.id.textarea);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
(((Same Code for buttons upto 9 and SUM, MINUS, DIVISION and MULtiplication Buttons)))
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn1: {
textarea.setText(textarea.getText()+"1");
break;
}
case R.id.btn2: {
textarea.setText(textarea.getText()+"2");
break;
}
case R.id.btn3:
((((( And So on... Upto Button 9 ))))))
case R.id.zerobtn: {
textarea.setText(textarea.getText()+"0");
break;
}
case R.id.dotbtn: {
textarea.setText(textarea.getText()+".");
break;
}
case R.id.sumbtn: {
num1=Double.parseDouble(textarea.getText().toString());
sum=true;
textarea.setText(null);
break;
}
(((((Likewise for Minus, Division and Multiplication buttons))))))))))
case R.id.equalbtn:
{
if (num1!=0)
{
num2=Double.parseDouble(textarea.getText().toString());
if (sum) {
textarea.setText(num1+num2+"");
sum=false;
}
else if(mul) {
textarea.setText(num1*num2+"");
mul=false;
}
else if(div) {
textarea.setText(num1/num2+"");
div=false;
}
else if(minus) {
textarea.setText(num1-num2+"");
minus=false;
}
}
else{
Toast.makeText(getApplicationContext(), "First select a number!!", Toast.LENGTH_SHORT);
}
break;
}
}
}
}
Calculator Activity Layout:
<LinearLayout 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"
android:weightSum="1"
tools:context="com.example.rfkha.myfirstapp.CalculatorActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="#string/calculatortxt"
android:textSize="25sp" />
<EditText
android:id="#+id/textarea"
android:layout_width="351dp"
android:layout_height="154dp"
android:layout_marginStart="16dp"
android:ems="10"
android:hint="#string/resultsString"
android:textSize="33sp"
android:textAlignment="textEnd"
android:padding="11dp"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/clearbt"
android:layout_width="293dp"
android:layout_height="51dp"
android:text="#string/cetxt"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn7"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="7"
android:textSize="24sp" />
<Button
android:id="#+id/btn8"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="8"
android:textSize="24sp" />
<Button
android:id="#+id/btn9"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="9"
android:textSize="24sp" />
<Button
android:id="#+id/mulbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="x"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn4"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="4"
android:textSize="24sp" />
<Button
android:id="#+id/btn5"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="5"
android:textSize="24sp" />
<Button
android:id="#+id/btn6"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="6"
android:textSize="24sp" />
<Button
android:id="#+id/minusbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="-"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="1"
android:textSize="24sp" />
<Button
android:id="#+id/btn2"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="2"
android:textSize="24sp" />
<Button
android:id="#+id/btn3"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="3"
android:textSize="24sp" />
<Button
android:id="#+id/sumbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="+"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/dotbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="."
android:textSize="24sp" />
<Button
android:id="#+id/zerobtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="0"
android:textSize="24sp" />
<Button
android:id="#+id/equalbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="="
android:textSize="24sp" />
<Button
android:id="#+id/divbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="/"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
Logcat: (Errors)
Link to Logcat: https://pastebin.com/hbwZVUZg
Delete the abstract keyword. then the line textarea= (EditText) findViewById(R.id.textarea); change for EditText textarea; and last put this on the onCreate() textarea = (EditText)findViewById(R.id.textarea);
Remember that you cannot instantiate an abstract class. here
And check how to debug here
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, zerobtn, dotbtn, equalbtn, minusbtn, sumbtn, divbtn, mulbtn;
double num1, num2;
boolean sum, minus, div, mul;
EditText textarea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
textarea = (EditText) findViewById(R.id.textarea);
Put this code in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
textarea = (EditText)findViewById(R.id.textarea);
...
}
Check whether you are using dependencies of same version or not. There should not be any version conflict among dependencies.

Android studio calculator - editText content disappearing after clicking operation button

I'm doing a simple calculator in android studio, my code looks like this:
MainActivity.java:
package com.example.ja.calculator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
String total="";
double v1, v2;
String sign="";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void onClick(View v){
Button button = (Button)v;
String str=button.getText().toString();
total+=str;
EditText edit=(EditText)findViewById(R.id.editText);
edit.setText(total);
}
public void OnAdd(View v){
v1 = Double.parseDouble(total);
total = "";
Button button = (Button)v;
String str = button.getText().toString();
sign = str;
EditText edit = (EditText)findViewById(R.id.editText);
edit.setText("");
}
public void OnCalculate(View v){
EditText edit = (EditText)findViewById(R.id.editText);
String str2 = edit.getText().toString();
v2 = Double.parseDouble(str2);
double grand_total = 0;
if(sign.equals("+")){
grand_total = v1 + v2;
}
else if(sign.equals("-")){
grand_total = v1 - v2;
}
else if(sign.equals("x")){
grand_total = v1 * v2;
}
else if(sign.equals(":")){
grand_total = v1 / v2;
}
edit.setText(grand_total+"");
}
public void OnClear(View v){
EditText edit = (EditText)findViewById(R.id.editText);
edit.setText("");
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.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
content main:
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.ja.calculator.MainActivity"
tools:showIn="#layout/activity_main">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:layout_alignTop="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_toEndOf="#+id/button2"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:layout_above="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:layout_above="#+id/button2"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:layout_above="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button5"
android:layout_toEndOf="#+id/button5"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button0"
android:layout_below="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="#+id/buttonDot"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/buttonEQ"
android:layout_below="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:height="80dp"
android:onClick="OnCalculate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/buttonAdd"
android:height="64dp"
android:layout_alignBottom="#+id/buttonEQ"
android:layout_alignLeft="#+id/buttonSub"
android:layout_alignStart="#+id/buttonSub"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/buttonSub"
android:height="64dp"
android:layout_above="#+id/buttonAdd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="#+id/buttonMul"
android:height="64dp"
android:layout_above="#+id/buttonSub"
android:layout_alignLeft="#+id/buttonSub"
android:layout_alignStart="#+id/buttonSub"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="#+id/buttonDiv"
android:layout_above="#+id/buttonMul"
android:layout_alignLeft="#+id/buttonMul"
android:layout_alignStart="#+id/buttonMul"
android:height="64dp"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CE"
android:id="#+id/buttonCE"
android:height="64dp"
android:layout_above="#+id/buttonDiv"
android:layout_toRightOf="#+id/button9"
android:layout_toEndOf="#+id/button9"
android:onClick="OnClear" />
The problem is, when I click some number and after that I click the operation (+,-,/,*) the number clicked before disappears, how to fix it, so I can see full operation, for example 5*5 before clicking "=" and after that changing just for result?
public void OnAdd(View v) {
v1 = Double.parseDouble(total);
total = "";
Button button = (Button) v;
String str = button.getText().toString();
sign = str;
EditText edit = (EditText) findViewById(R.id.editText); ** edit.setText("");
}
The reason for number clicked before gets cleared is that you set text as "". (see **)
You should do the following changes to your code if your calculator to work in the correct way.
public void OnAdd(View v) {
v1 = Double.parseDouble(total);
Button button = (Button) v;
String str = button.getText().toString();
sign = str;
total += str;
EditText edit = (EditText) findViewById(R.id.editText);
edit.setText(total);
}
Now the sign will be appended to the total string. And it will be written back to the editText.

my button cannot find a method in the MainActivity.java file

Hi i'm a beginner android developer trying to make a basic calculator app
Here is my layout code from the content_main.xml folder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView
android:id="#+id/displayer"
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="113dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/minus"
android:onClick="subtract"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/displayer"
android:layout_toStartOf="#+id/displayer"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="+"
android:id="#+id/plus"
android:layout_below="#+id/minus"
android:layout_alignLeft="#+id/minus"
android:layout_alignStart="#+id/minus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="setNumber"
android:tag="1"
android:id="#+id/one"
android:layout_above="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:layout_toEndOf="#+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="4"
android:id="#+id/four"
android:tag="4"
android:layout_alignTop="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:layout_toEndOf="#+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="2"
android:id="#+id/two"
android:tag="2"
android:layout_above="#+id/four"
android:layout_toRightOf="#+id/one"
android:layout_toEndOf="#+id/one" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="3"
android:tag="3"
android:id="#+id/three"
android:layout_above="#+id/five"
android:layout_toRightOf="#+id/two"
android:layout_toEndOf="#+id/two" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="5"
android:tag="5"
android:id="#+id/five"
android:layout_below="#+id/two"
android:layout_alignLeft="#+id/two"
android:layout_alignStart="#+id/two" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="6"
android:tag="6"
android:id="#+id/six"
android:layout_above="#+id/nine"
android:layout_toRightOf="#+id/five"
android:layout_toEndOf="#+id/five" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="7"
android:tag="7"
android:id="#+id/seven"
android:layout_below="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:layout_toEndOf="#+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="8"
android:tag="8"
android:id="#+id/eight"
android:layout_below="#+id/five"
android:layout_alignLeft="#+id/five"
android:layout_alignStart="#+id/five" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="9"
android:tag="9"
android:id="#+id/nine"
android:layout_below="#+id/five"
android:layout_alignLeft="#+id/six"
android:layout_alignStart="#+id/six" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/equal"
android:onClick="equal"
android:layout_below="#+id/plus"
android:layout_alignLeft="#+id/plus"
android:layout_alignStart="#+id/plus" />
</RelativeLayout>
Here is my Activity from the MainActivity folder:
package com.example.android.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private int firstNumber = -1;
private int secondNumber = -1;
private int operation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void equal(View view){
TextView displayerTextView=(TextView)
findViewById(R.id.displayer);
if (operation == 0) {
displayerTextView.setText(firstNumber+secondNumber);
}else if (operation == 1){
displayerTextView.setText(firstNumber-secondNumber);
}else{
displayerTextView.setText("error");
}
}
private void add(View view){
operation = 0;
}
private void subtract(View view){
operation = 1;
}
private void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
}
When I click on the 3 button in my calculator app this error appears here is the error:
java.lang.IllegalStateException: Could not find a method setNumber(View) in the activity class com.example.android.calculator.MainActivity for onClick handler on view class android.widget.Button with id 'three'
Because you method is private. Change on public.
More you can find here http://developer.android.com/reference/android/widget/Button.html
It should be
public void setNumber(View view){
For the issue mentioned, change
private void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
to
public void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
To avoid similar errors in your code with add and subtract, change those functions also from private to public.

Updating button in android

I am creating an address Book app but I have a problem. When I am trying to set text to the button it doesn't update on the screen.
Here is a part of my main layout:
public void call(View v){
setContentView(R.layout.call);
Numbers num = new Numbers(cont);
final Names names = new Names(cont);
button = (Button) findViewById(R.id.name1);
if(num.getNumber("1").isEmpty()) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_aseta_numero);
Button button1 = (Button) findViewById(R.id.readyButton);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText text = (EditText) findViewById(R.id.Number);
Numbers j = new Numbers(cont);
j.setNumber(text.getText().toString(), "1");
EditText tex = (EditText) findViewById(R.id.NameField);
names.adName(tex.getText().toString(), "1");
button.setText(names.getNimi("1") + "moi");
setContentView(R.layout.call);
System.out.println(button.getText());
}
});
}
});
}else {
}
Here is my name class:
public class Names extends Activity{
SharedPreferences pres;
Button Nappi;
Context context;
public Names(Context mcontext){
context = mcontext;
pres = context.getSharedPreferences("1",0);
}
public void adName(String name, String place){
SharedPreferences.Editor editor = pres.edit();
editor.putString(name,place);
editor.commit();
System.out.println(pres.getString("1", ""));
}
public String getName(String place){
return pres.getString(place,"");
}
}
and here is my setnumber 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"
tools:context="oo.seniorlauncher.com.o_seniorlauncher.setnumber">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Syötä"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:height="100dp"
android:textSize="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="numero"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:width="250dp"
android:textSize="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ja nimi"
android:id="#+id/textView3"
android:height="100dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="66dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="50dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/NameField"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="50dp"
android:layout_centerVertical="true"
android:text="Nimi" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/Number"
android:layout_below="#+id/NameField"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="50dp"
android:text="Numero"
android:phoneNumber="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valmis"
android:id="#+id/readyButton"
android:layout_below="#+id/Number"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="75dp" />
and here is the call layout
<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"
tools:context="oo.seniorlauncher.com.o_seniorlauncher.soita1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="100dp"
android:width="200dp"
android:textSize="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name2"
android:height="100dp"
android:width="200dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/name1"
android:layout_toEndOf="#+id/name1"
android:textSize="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name3"
android:height="100dp"
android:width="200dp"
android:textSize="25dp"
android:layout_below="#+id/name1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name4"
android:height="100dp"
android:width="200dp"
android:textSize="25dp"
android:layout_below="#+id/name1"
android:layout_toRightOf="#+id/name3"
android:layout_toEndOf="#+id/name3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name5"
android:height="100dp"
android:width="200dp"
android:textSize="25dp"
android:layout_below="#+id/name3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name6"
android:height="100dp"
android:width="200dp"
android:textSize="25dp"
android:layout_below="#+id/name3"
android:layout_toRightOf="#+id/name5"
android:layout_toEndOf="#+id/name5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:id="#+id/home1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/name6"
android:layout_alignEnd="#+id/name6"
android:height="100dp"
android:textSize="80dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show all numbers"
android:id="#+id/button4"
android:height="100dp"
android:textSize="30dp"
android:layout_below="#+id/name5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/home1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
recreate() and .invalide didn't help.
Thanks in advance.

Menu actions and sums in Android app

So I have been trying to develop an app that adds a select quantity together in a menu, all while in Android studio/eclipse.
I am listing off drinks and to the right of each drink is a "-" (the minus) and "+" (the plus) signs, which are buttons respectively, with a "0" in the middle.
At the bottom of the screen, I have a "Total" section.
I have a screen shot of what the menu appears as thus far:
I am looking to have the + and - buttons affect the number that lies in the middle of them. So when the "+" is pressed it adds 1 and if pressed again, it adds 1 and so on, the "-" button will take 1 away from the number back to zero if need be.
It should be able to tally the total price of each drink all together and output the sum to the
"$ 0.00" near the total. The "Order" button just takes me to another screen, which will be tackled at a later date.
I have tried searching for a good many hours and there are so many links I am drowning that I can not find any help that sticks.
I know that the Java code should use the ID of the EditText's but I am confused as to how.
I will post what code I have for the xml and java code. If there is also a manifest I have to add, I can not find it.
drinkmenu.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/mainmenulayout"
android:background="#34D6D9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Drinks:"
android:id="#+id/textView"
android:background="#FFFFFF"
android:textStyle="bold"
android:textSize="24dp"
android:layout_toStartOf="#+id/textView2"
android:layout_toLeftOf="#+id/textView2"
android:layout_marginRight="75dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Quantity:"
android:id="#+id/textView2"
android:textSize="24dp"
android:textStyle="bold"
android:background="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:layout_alignEnd="#+id/orderbtn" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Espresso"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Macciato"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Con Panna"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Americano"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Latte"
android:id="#+id/button5"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button6"
android:layout_alignTop="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ffff45df"
android:textSize="24dp" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_above="#+id/button2" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button7"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button8"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText4"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24sp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_alignBottom="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button9"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button10"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button3"
android:layout_alignRight="#+id/button8"
android:layout_alignEnd="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button11"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button12"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText5"
android:layout_toStartOf="#+id/button10"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_above="#+id/button4" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText6"
android:layout_toStartOf="#+id/button11"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_alignBottom="#+id/button11" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText7"
android:layout_toStartOf="#+id/button12"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_alignBottom="#+id/button12" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button14"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button15"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button16"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText8"
android:text="Total:"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textView"
android:layout_toStartOf="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText9"
android:layout_alignBottom="#+id/editText8"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:text=" $ 0.00"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignTop="#+id/editText8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:id="#+id/orderbtn"
android:textSize="24sp"
android:background="#drawable/buttonround"
android:layout_alignBottom="#+id/editText9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp"/>
</RelativeLayout>
java code:
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DrinkMenu extends Activity {
Button button;
Button buttton6; Button buttton7; Button buttton8; Button buttton9;
Button buttton10; Button buttton11; Button buttton16; Button buttton14;
Button buttton12; Button buttton15;
// EditText editText2; EditText editText4; EditText editText5;
// EditText editText6; EditText editText7; EditText editText9;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.orderbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ThirdPartyPaymentMethod.class);
startActivity(intent);
}
});
}
}
I already have my manifest to allow button actions.
I know there are methods out there, but my searches have ended in my frustration. Any help would be appreciated. I am new to this, I enjoy the front end, but the back end is new.
I'll do a part and for remaining you have to do.
Consider,
- is a button with id 'button1' and '+' has button2
Quantity is a EditText which has id qty.
Right, coming to the code :
public class MainActivity extends Activity {
EditText quantity;
Button plus,minus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
plus = (Button) findViewById(R.id.button1);
minus = (Button) findViewById(R.id.button2);
quantity = (EditText) findViewById(R.id.qty);
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1+1;
quantity.setText(Integer.toString(inum));
}
});
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1-1;
quantity.setText(Integer.toString(inum));
}
});
}
}
For sub total, you just need to multiply quantity and the item price. For the Total Price, you just need to add all the subtotals. Hope it helps!
UPDATE about Catching Zero:
You are getting the quantity here.
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
So, if the number is 0, perform if else
if (num1==0)
{
//Create a alert showing your quantity is zero.
//here i use toast to display
Toast.makeText(this,"Your quantity is Zero. You can't reduce the quantity", Toast.LENGTH_LONG).show();
}
else
{
// do the stuffs here
}
what you want to do is something like this:
Button button = (Button) findViewById(R.id.my_button);
final TextView textView = (TextView) findViewById(R.id.my_text_view);
button.setOnClickListener(new View.OnClickListener() {
#Overrride
public void onClick(View aView) {
textView.setText("Hi");
}
});
You can write this in the onCreate() method of the Activity. Not tested yet, but this will change the text of the TextView to the desired value. Let me know if I have understood your problem correctly.

Categories

Resources