user3432888 user3432888 - 1 month ago 11
Android Question

Calculator Android Studio

I am a beginner in android studio.I am trying to build a calculator here is my code but the buttons on my calculator are not working Here is the java code.The problem seems to be with clicklistener when i click the button on my android virtual machine the text doesnot changes and the operators are also not working what is the problem in my code if there is any.

package com.example.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;

import static android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {
private EditText Scr;
private float NumberBf;
private String Operation;
private ButtonClickListener btnClick;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Scr = (EditText)findViewById(R.id.textView);
int idList[]= {

R.id.button,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.button10,R.id.buttonC,
R.id.buttonEq,R.id.buttonAdd,R.id.buttonSub,R.id.buttonDiv,
R.id.buttonDot,R.id.buttonMul};
for(int id:idList)
{
View v = (View) findViewById(id);
v.setOnClickListener(btnClick);

}


}


public void mMath(String str){
NumberBf=Float.parseFloat(Scr.getText().toString());
Operation=str;
Scr.setText("0");

}
public void getKeyboard(String str)
{
String ScrCurrent = Scr.getText().toString();
ScrCurrent += str;
Scr.setText(ScrCurrent);

}
public void mResult()
{
float NumAf=Float.parseFloat(Scr.getText().toString());
float result=0;
if(Operation.equals("+"))
{
result =NumAf + NumberBf;
}

if(Operation.equals("-"))
{
result =NumAf - NumberBf;
}

if(Operation.equals("*"))
{
result =NumAf * NumberBf;
}

if(Operation.equals("/"))
{
result =NumAf / NumberBf;
}
Scr.setText(String.valueOf(result));
}
private class ButtonClickListener implements OnClickListener {
public void onClick(View v){
switch (v.getId()){
case R.id.buttonC:
Scr.setText("0");
NumberBf=0;
Operation="";
break;
case R.id.buttonAdd:
mMath("+");
break;
case R.id.buttonSub:
mMath("-");
break;
case R.id.buttonDiv:
mMath("/");
break;
case R.id.buttonMul:
mMath("*");
break;
case R.id.buttonEq:
mResult();
break;
default :
String numb;
numb = ((Button)v).getText().toString();
getKeyboard(numb);
break;


}

}
}

}


And Here is the XML Code.


<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="54dp"
android:layout_marginTop="73dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="right"
android:onClick="setContentView" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/button"
android:textStyle="bold"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:onClick="onClick" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_toRightOf="@+id/button"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/button5"
android:layout_below="@+id/button2"
android:layout_toRightOf="@+id/button4"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/button6"
android:layout_below="@+id/button3"
android:layout_toRightOf="@+id/button5"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/button7"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/button8"
android:layout_alignTop="@+id/button7"
android:layout_toRightOf="@+id/button7"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/button9"
android:layout_below="@+id/button6"
android:layout_toRightOf="@+id/button8"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/button10"
android:layout_below="@+id/button7"
android:layout_alignLeft="@+id/button7"
android:layout_alignStart="@+id/button7"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/buttonDot"
android:layout_alignTop="@+id/button10"
android:layout_toRightOf="@+id/button10"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/buttonC"
android:layout_alignTop="@+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/buttonAdd"
android:layout_alignTop="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/buttonSub"
android:layout_above="@+id/button9"
android:layout_toRightOf="@+id/button6"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/buttonMul"
android:layout_above="@+id/buttonDot"
android:layout_toRightOf="@+id/button9"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/buttonDiv"
android:layout_alignTop="@+id/buttonDot"
android:layout_alignLeft="@+id/buttonMul"
android:layout_alignStart="@+id/buttonMul"
android:textStyle="bold" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/buttonEq"
android:layout_alignTop="@+id/buttonDiv"
android:layout_toLeftOf="@+id/buttonDiv"
android:textStyle="bold" />



Answer

edit 1: implement OnClickListener

public class MainActivity extends ActionBarActivity implements OnClickListener

edit 2:

v.setOnClickListener(this);

edit 3: add this to the end of the MainActivity

@Override
public void onClick(View v)
{
    switch (v.getId())
    {

    }
}

This should work. Or you could try to instantiate your btnClick before you set the btnClick as a listener to v.

btnClick = new ButtonClickListener();
Comments