Android studio calculator - editText content disappearing after clicking operation button - java

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.

Related

How can i changing text button by user input

in my exercise in which there are a grid of 3x3 buttons some with numbers others without, I should put those without numbers a number from 0-9, however, put by the user then changing the text of the button. How can I do? I tried something but it doesn't work even when I search on the internet it didn't give me the result I wanted.
This is the code
public class MainActivity extends AppCompatActivity {
private Button btn;
private EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button_X00);
edit = (EditText) findViewById(R.id.edit_dialog);
btn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
showDialog(edit.getText().toString());
return true;
}
});
}
private void showDialog(String str) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("input text");
View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
final EditText edit_dialog = (EditText) view.findViewById(R.id.edit_dialog);
edit_dialog.setText(str);
builder.setView(view);
builder.setNegativeButton("cancel",null);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
btn.setText(edit_dialog.getText().toString());
}
});
builder.show();
}
}
THIS IS THE XML CODE
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_p8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="GRIGLIAMOD10"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="418dp"
android:layout_height="586dp"
android:layout_marginTop="49dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/linearLayout2">
<androidx.gridlayout.widget.GridLayout
android:id="#+id/tabellaX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="60dp">
<EditText
android:id="#+id/edit_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button_X22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="2" />
<Button
android:id="#+id/button_X00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="0"
android:focusable="false"/>
<Button
android:id="#+id/button_X01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="" />
<Button
android:id="#+id/button_X12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="1" />
<Button
android:id="#+id/button_X02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="0" />
<Button
android:id="#+id/button_X20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="2" />
<Button
android:id="#+id/button_X21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="" />
<Button
android:id="#+id/button_X10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="1" />
<Button
android:id="#+id/button_X11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="1"
app:layout_row="1" />
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
So here is one possible solution.
First in your XML you can avoid using both Linear layouts since you are using a Constraint layout. So the XML looks as follow.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:id="#+id/text_view_p8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="GRIGLIAMOD10"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.gridlayout.widget.GridLayout
android:id="#+id/tabellaX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view_p8">
<Button
android:id="#+id/button_X00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="0" />
<Button
android:id="#+id/button_X01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
/>
<Button
android:id="#+id/button_X12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="1" />
<Button
android:id="#+id/button_X02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="0" />
<Button
android:id="#+id/button_X20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="2" />
<Button
android:id="#+id/button_X21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
/>
<Button
android:id="#+id/button_X22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="2"
app:layout_row="2" />
<Button
android:id="#+id/button_X10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="0"
app:layout_row="1" />
<Button
android:id="#+id/button_X11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text=""
app:layout_column="1"
app:layout_row="1" />
</androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I also changed the Java file and I left some comments in case you want a different behavior from your app.
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get all the buttons from the grid - be careful if you add another type of Touchable
ArrayList<View> gridButtons = ((GridLayout) findViewById(R.id.tabellaX)).getTouchables();
for (View v : gridButtons) {
((Button) v).setOnLongClickListener(this);
}
}
#Override
public boolean onLongClick(View v) {
// show the dialog ONLY if the button isn't set
if (((Button) v).getText().toString().isEmpty())
showDialog(v);
return true;
}
private void showDialog(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Input a number");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// get only the first inserted number(i.e. input: 45; take only the 4)
((Button) v).setText(input.getText().toString().substring(0, 1));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
EDIT
In the comment section below the answer you asked me if I can edit the behavior of the App.
If you click on the first button where there is the number, for example "4", this must be added to all the other buttons that have a number, if the second button has a number 3, this becomes 7. But if for example 5 to 6 is added this must not become 11 but 1 must be of module 10.
package hr.hello.dm.test;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener, View.OnClickListener {
private ArrayList<View> gridButtons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get all the buttons from the grid - be careful if you add another type of Touchable
gridButtons = ((GridLayout) findViewById(R.id.tabellaX)).getTouchables();
for (View v : gridButtons) {
((Button) v).setOnLongClickListener(this);
((Button) v).setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
if(((Button) v).((Button) v).getText().toString().isEmpty())
return;
int value = Integer.parseInt(((Button) v).getText().toString());
for (View btn : gridButtons) {
if(v.getId() != btn.getId() && !((Button) btn).getText().toString().isEmpty()) {
int btnValue = Integer.parseInt(((Button) btn).getText().toString());
((Button) btn).setText(String.valueOf((value + btnValue)%10));
}
}
}
#Override
public boolean onLongClick(View v) {
if (((Button) v).getText().toString().isEmpty())
showDialog(v);
return true;
}
private void showDialog(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Input a number");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
int value = Integer.parseInt(input.getText().toString());
((Button) v).setText(String.valueOf(value));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
As far as i understand what you are trying to do, codes below may help you.
public class MainActivity extends AppCompatActivity{
Button[] buttons;
private final String TAG = this.getClass().getSimpleName();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons = new Button[9];
buttons[0] = (Button) findViewById(R.id.button_X00);
buttons[1] = (Button) findViewById(R.id.button_X10);
buttons[2] = (Button) findViewById(R.id.button_X20);
buttons[3] = (Button) findViewById(R.id.button_X01);
buttons[4] = (Button) findViewById(R.id.button_X11);
buttons[5] = (Button) findViewById(R.id.button_X21);
buttons[6] = (Button) findViewById(R.id.button_X02);
buttons[7] = (Button) findViewById(R.id.button_X12);
buttons[8] = (Button) findViewById(R.id.button_X22);
for (int a = 0; a<buttons.length;a++){
showDialog(buttons[a]);
}
}
private void showDialog(final Button btn) {
btn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("input text");
View view = View.inflate(MainActivity.this,R.layout.dialog_activity, null);
builder.setView(view);
final EditText edit_dialog = view.findViewById(R.id.edit_dialog);
builder.setNegativeButton("cancel",null);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
btn.setText(edit_dialog.getText().toString());
}catch (Exception e){
Log.e(TAG, e.getMessage() + " " );
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
}
});
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:id="#+id/tabellaX"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="60dp">
<Button
android:id="#+id/button_X22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="2"
app:layout_row="2" />
<Button
android:id="#+id/button_X00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="0"
app:layout_row="0"
android:focusable="false"/>
<Button
android:id="#+id/button_X01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true" />
<Button
android:id="#+id/button_X12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="2"
app:layout_row="1" />
<Button
android:id="#+id/button_X02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="2"
app:layout_row="0" />
<Button
android:id="#+id/button_X20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="0"
app:layout_row="2" />
<Button
android:id="#+id/button_X21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="1"
app:layout_row="2"/>
<Button
android:id="#+id/button_X10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="0"
app:layout_row="1" />
<Button
android:id="#+id/button_X11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
app:layout_column="1"
app:layout_row="1" />
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
and here is dialog_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_view_p8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="GRIGLIAMOD10"
android:textSize="30sp" />
<EditText
android:id="#+id/edit_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>

android calculator buttons doesnt work

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

Android Launcher Shows Calculator activity layout but does nothing

Hi i'm working on a Android Launcher that Shows Calculator activity. It shows the layout fine but does nothing when you click on any of the buttons I need help I have tried everything I can and i'm getting upset! any help would be amazing!!
Calculator.java:
package com.dva.schooltoolshome;
import java.text.DecimalFormat;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Calculator extends FragmentActivity implements OnClickListener {
private TextView calculatorDisplay;
private static final String DIGITS = "0123456789.";
private Boolean userIsInTheMiddleOfTypingANumber = false;
DecimalFormat df = new DecimalFormat("############");
CalculatorBrain brain;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
// hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
// hide the status bar and other OS-level chrome
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
brain = new CalculatorBrain();
calculatorDisplay = (TextView) findViewById(R.id.textView1);
df.setMinimumFractionDigits(0);
df.setMinimumIntegerDigits(1);
df.setMaximumIntegerDigits(8);
findViewById(R.id.button0).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
findViewById(R.id.buttonAdd).setOnClickListener(this);
findViewById(R.id.buttonSubtract).setOnClickListener(this);
findViewById(R.id.buttonMultiply).setOnClickListener(this);
findViewById(R.id.buttonDivide).setOnClickListener(this);
findViewById(R.id.buttonToggleSign).setOnClickListener(this);
findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
findViewById(R.id.buttonEquals).setOnClickListener(this);
findViewById(R.id.buttonClear).setOnClickListener(this);
findViewById(R.id.buttonClearMemory).setOnClickListener(this);
findViewById(R.id.buttonAddToMemory).setOnClickListener(this);
findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this);
findViewById(R.id.buttonRecallMemory).setOnClickListener(this);
// The following buttons only exist in layout-land (Landscape mode) and require extra attention.
// The messier option is to place the buttons in the regular layout too and set android:visibility="invisible".
if (findViewById(R.id.buttonSquareRoot) != null) {
findViewById(R.id.buttonSquareRoot).setOnClickListener(this);
}
if (findViewById(R.id.buttonInvert) != null) {
findViewById(R.id.buttonInvert).setOnClickListener(this);
}
if (findViewById(R.id.buttonCos) != null) {
findViewById(R.id.buttonCos).setOnClickListener(this);
}
if (findViewById(R.id.buttonSin) != null) {
findViewById(R.id.buttonSin).setOnClickListener(this);
}
// Another way to hide the window title and actionbar, but only in newer sdk's
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// ActionBar actionBar = getActionBar();
// actionBar.setDisplayShowHomeEnabled(false);
// actionBar.setDisplayShowTitleEnabled(false);
// actionBar.hide();
// }
}
// #Override
public void onClick(View view) {
String buttonPressed = ((Button) view).getText().toString();
// String digits = "0123456789.";
if (DIGITS.contains(buttonPressed)) {
// digit was pressed
if (userIsInTheMiddleOfTypingANumber) {
calculatorDisplay.append(buttonPressed);
} else {
calculatorDisplay.setText(buttonPressed);
userIsInTheMiddleOfTypingANumber = true;
}
} else {
// operation was pressed
if (userIsInTheMiddleOfTypingANumber) {
brain.setOperand(Double.parseDouble(calculatorDisplay.getText().toString()));
userIsInTheMiddleOfTypingANumber = false;
}
brain.performOperation(buttonPressed);
calculatorDisplay.setText(df.format(brain.getResult()));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save variables on screen orientation change
outState.putDouble("OPERAND", brain.getResult());
outState.putDouble("MEMORY", brain.getMemory());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore variables on screen orientation change
brain.setOperand(savedInstanceState.getDouble("OPERAND"));
brain.setMemory(savedInstanceState.getDouble("MEMORY"));
calculatorDisplay.setText(df.format(brain.getResult()));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
CalculatorBrain.java:
package com.dva.schooltoolshome;
public class CalculatorBrain {
// 3 + 6 = 9
// 3 & 6 are called the operand.
// The + is called the operator.
// 9 is the result of the operation.
private double operand = 0;
private double waitingOperand = 0;
private String waitingOperator = "";
private double calculatorMemory = 0;
public void setOperand(double operand) {
this.operand = operand;
}
public double getResult() {
return operand;
}
// used on screen orientation change
public void setMemory(double calculatorMemory) {
this.calculatorMemory = calculatorMemory;
}
// used on screen orientation change
public double getMemory() {
return calculatorMemory;
}
public String toString() {
return Double.toString(operand);
}
protected double performOperation(String operator) {
/*
* If you are using Java 7, then you can use switch in place of if statements
*
* switch (operator) {
* case "MC":
* calculatorMemory = 0;
* break;
* case "M+":
* calculatorMemory = calculatorMemory + operand;
* break;
* }
*/
if (operator.equals("MC")) {
calculatorMemory = 0;
} else if (operator.equals("M+")) {
calculatorMemory = calculatorMemory + operand;
} else if (operator.equals("M-")) {
calculatorMemory = calculatorMemory - operand;
} else if (operator.equals("MR")) {
operand = calculatorMemory;
} else if (operator.equals("C")) {
operand = 0;
waitingOperator = "";
waitingOperand = 0;
calculatorMemory = 0;
} else if (operator.equals("Sqrt")) {
operand = Math.sqrt(operand);
} else if (operator.equals("1/x")) {
if (operand != 0) {
operand = 1 / operand;
}
} else if (operator.equals("+/-")) {
operand = -operand;
} else if (operator.equals("sin")) {
operand = Math.sin(operand);
} else if (operator.equals("cos")) {
operand = Math.cos(operand);
} else {
performWaitingOperation();
waitingOperator = operator;
waitingOperand = operand;
}
return operand;
}
protected void performWaitingOperation() {
if (waitingOperator.equals("+")) {
operand = waitingOperand + operand;
} else if (waitingOperator.equals("*")) {
operand = waitingOperand * operand;
} else if (waitingOperator.equals("-")) {
operand = waitingOperand - operand;
} else if (waitingOperator.equals("/")) {
if (operand != 0) {
operand = waitingOperand / operand;
}
}
}
}
activity_calculator.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:maxLines="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1" >
<Button
android:id="#+id/buttonAddToMemory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="M+" />
<Button
android:id="#+id/buttonSubtractFromMemory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="M-" />
<Button
android:id="#+id/buttonRecallMemory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="MR" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout2" >
<Button
android:id="#+id/buttonClear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="C" />
<Button
android:id="#+id/buttonToggleSign"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="+/-" />
<Button
android:id="#+id/buttonDivide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="/" />
<Button
android:id="#+id/buttonMultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="*" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout3" >
<Button
android:id="#+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="#string/button7" />
<Button
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="#string/button8" />
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="#string/button9" />
<Button
android:id="#+id/buttonSubtract"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="-" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout4" >
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="#string/button4" />
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="#string/button5" />
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="#string/button6" />
<Button
android:id="#+id/buttonAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="+" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout5"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".75"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout7"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:text="#string/button1" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:text="#string/button2" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".34"
android:text="#string/button3" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout8"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".66"
android:text="#string/button0" />
<Button
android:id="#+id/buttonDecimalPoint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".34"
android:text="." />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/buttonEquals"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".25"
android:text="=" />
</LinearLayout>
<ImageButton
android:id="#+id/apps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/wbrowser"
android:background="#drawable/appdrawer"
android:src="#drawable/appdrawer" />
<ImageButton
android:id="#+id/wbrowser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/browser" />
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/wbrowser"
android:background="#drawable/appdrawer"
android:src="#drawable/appdrawer" />
<Button
android:id="#+id/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/ImageButton01"
android:layout_centerHorizontal="true"
android:text="Tools" />
<Button
android:id="#+id/buttonClearMemory"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_above="#+id/ImageButton01"
android:layout_alignLeft="#+id/tools"
android:layout_marginBottom="163dp"
android:layout_weight=".25"
android:text="MC" />
</RelativeLayout>
I'm still new to android development so please don't judge
Regards
Rapsong11
First of all use switch statement in OnClick(View v) function for every button.
like this.
public void onClick(View v)
{
case R.Id.button1
// the functionality code goes here
break;
case R.id.button2
break;
}

LogCat error Error opening trace file: no such file or directory

Sorry if this is a very noob question, but the code below is not giving an output and I the logcat view isn't clear so I can't tell how far it is getting when I run the emulator.
Have I got the radio buttons coded correctly?
Are the variable statements coded correctly?
Can you see any other errors?
Layout xml is as follows:
<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=".MainActivity" >
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:layout_marginTop="27dp"
android:ems="10" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_marginTop="19dp"
android:ems="10" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="16dp"
android:ems="10" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:ems="10" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:text="#string/pressu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_toRightOf="#+id/editText2"
android:text="#string/volu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/textView2"
android:text="#string/molu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText4"
android:layout_alignLeft="#+id/textView3"
android:text="#string/tempu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/editText4"
android:layout_marginTop="35dp"
android:text="#string/calc" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button1"
android:layout_marginRight="33dp" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/pressure" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/vol" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mol" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp" />
</RadioGroup>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="22dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
The Java for the main activity is here
package com.minichanic.idealgas;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText pressure;
private EditText volume;
private EditText moles;
private EditText temperature;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pressure = (EditText) findViewById(R.id.editText1);
volume = (EditText) findViewById(R.id.editText2);
moles = (EditText) findViewById(R.id.editText3);
temperature= (EditText) findViewById(R.id.editText4);
}
public void onClick(View view) {
//set values to the input
float pressuren = Float.parseFloat(pressure.getText().toString());
float volumen = Float.parseFloat(volume.getText().toString());
float molesn = Float.parseFloat(moles.getText().toString());
float temperaturen = Float.parseFloat(temperature.getText().toString());
//change values to standard conditions if blank
if (pressuren == 0) {pressuren = 100000;
}
if (volumen == 0) {volumen = (float)0.0247;
}
if (molesn == 0) {molesn= 1;
}
if (temperaturen == 0) {temperaturen = 298;
}
//set input values as answers for set text later
float pressans = (float) pressuren;
float volans = (float) volumen;
float moleans = (float) molesn;
float temperaturans = (float) temperaturen;
//set calculation depending on radio button
switch (view.getId()) {
case R.id.radio0:
pressans = (float) (molesn * 8.31 * temperaturen ) / volumen;
break;
case R.id.radio1:
volans = (float) (molesn * 8.31 * temperaturen ) / pressuren;
break;
case R.id.radio2:
moleans = (float) ((float) (pressuren * volumen)/(8.31 * temperaturen));
break;
case R.id.radio3:
temperaturans = (float) ((float) (pressuren * volumen)/(8.31 * molesn));
break;
}
//print values in the EditText fields
pressure.setText(""+pressans);
volume.setText(""+volans);
moles.setText(""+moleans);
temperature.setText(""+temperaturans);
}
#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;
}
}
The logCat i am getting for the package is:
Error opening trace file: no such file or directory
Any ideas will be greatly appreciated.
Its a system level error log and it does not affect execution of your application, so you don't have to worry about it.
Generally Its occurs Jelly Bean (android 4.1).And mainly its because of some SD-card issue.

How to make Views with an Invisible attribute 'Visible' after clicking a button

I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}

Categories

Resources