How do I show complete calculation in textview using parenthesis? For example: 2+3-((4/2)*9)
I am making calculator app which uses parenthesis and remember calculations history and should display all math operation in textview.
Here is my code:
package com.example.calculater;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView textdisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textdisplay = (TextView) findViewById(R.id.editText1);
Button b1 = (Button) findViewById(R.id.btn1);
Button b2 = (Button) findViewById(R.id.btn2);
Button b3 = (Button) findViewById(R.id.btn3);
Button b4 = (Button) findViewById(R.id.btn4);
Button b5 = (Button) findViewById(R.id.btn5);
Button b6 = (Button) findViewById(R.id.btn6);
Button b7 = (Button) findViewById(R.id.btn7);
Button b8 = (Button) findViewById(R.id.btn8);
Button b9 = (Button) findViewById(R.id.btn9);
Button b0 = (Button) findViewById(R.id.btn0);
Button multiply1 = (Button) findViewById(R.id.multiply);
Button divide1 = (Button) findViewById(R.id.divide);
Button plus1 = (Button) findViewById(R.id.plus);
Button minus1 = (Button) findViewById(R.id.minus);
Button equal1 = (Button) findViewById(R.id.equal);
Button clear1 = (Button) findViewById(R.id.clear);
Button back1 = (Button) findViewById(R.id.backspace);
Button dot1 = (Button) findViewById(R.id.decimal);
Button plusminus1 = (Button) findViewById(R.id.plusminus);
Button history1= (Button) findViewById(R.id.history);
Button open1 = (Button) findViewById(R.id.open);
Button close1 = (Button) findViewById(R.id.close);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
multiply1.setOnClickListener(this);
divide1.setOnClickListener(this);
plus1.setOnClickListener(this);
minus1.setOnClickListener(this);
equal1.setOnClickListener(this);
clear1.setOnClickListener(this);
back1.setOnClickListener(this);
dot1.setOnClickListener(this);
plusminus1.setOnClickListener(this);
history1.setOnClickListener(this);
open1.setOnClickListener(this);
close1.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
int clear_flag = 0;
String sign_flag = "";
Double total = 0.0;
int last_button = 0;
public void shownum (String number){
if(clear_flag==1){
textdisplay.setText("");
clear_flag=0;
}
else if(textdisplay.getText()=="0"){
textdisplay.setText("");
}
textdisplay.setText(textdisplay.getText() + number);
}
public void showsign(String sign){
if(last_button==R.id.plus || last_button==R.id.minus || last_button==R.id.multiply
|| last_button==R.id.divide){
}
else{
clear_flag = 1;//set flag
Double newNumber = Double.parseDouble(textdisplay.getText().toString());
if(sign_flag == "" || sign_flag == "="){
total = newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "+"){
total = total + newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "-"){
total = total - newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "*"){
total = total*newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "/"){
total = total/newNumber;
textdisplay.setText(total.toString());
}}
sign_flag = sign;
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.btn0){
shownum ("0");
}
else if(v.getId() == R.id.btn1){
shownum ("1");
}
else if(v.getId() == R.id.btn2){
shownum ("2");
}
else if(v.getId() == R.id.btn3){
shownum ("3");
}
else if(v.getId() == R.id.btn4){
shownum ("4");
}
else if(v.getId() == R.id.btn5){
shownum ("5");
}
else if(v.getId() == R.id.btn6){
shownum ("6");
}
else if(v.getId() == R.id.btn7){
shownum ("7");
}
else if(v.getId() == R.id.btn8){
shownum ("8");
}
else if(v.getId() == R.id.btn9){
shownum ("9");
}
else if(v.getId() == R.id.clear){
textdisplay.setText("");////ORIGINALLY ITS WAS 0 ""
total = 0.0;
sign_flag = "";
}
else if(v.getId() == R.id.decimal){
if(clear_flag==1){
textdisplay.setText("");
clear_flag = 0;
}
if(textdisplay.getText().toString().indexOf(".")<0){
textdisplay.setText(textdisplay.getText() + ".");
}}
else if(v.getId() == R.id.backspace){
if(textdisplay.getText().toString().length()>0){
int start = 0;
int end = textdisplay.getText().toString().length()-1;
String newText = textdisplay.getText().toString().substring(start,end);
textdisplay.setText(newText);
}}
else if(v.getId() == R.id.plus){
showsign("+");
}
else if(v.getId() == R.id.minus){
showsign("-");
}
else if(v.getId() == R.id.multiply){
showsign("*");
}
else if(v.getId() == R.id.divide){
showsign("/");
}
else if(v.getId() == R.id.equal){
Double newNumber = Double.parseDouble(textdisplay.getText().toString());
if(sign_flag == "+"){
total = total+newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "-"){
total = total-newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "*"){
total = total*newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "/"){
total = total/newNumber;
textdisplay.setText(total.toString());
}
sign_flag = "=";
} //when minus is pressed before any number input, applications closes.
else if (v.getId() == R.id.plusminus){
String number = textdisplay.getText().toString();
if(number == null) return; //exits function
if(number.equals("")) return; //exits function
// textdisplay.setText("Please enter number first then press +/-");
Double newNumber = Double.parseDouble(number);
total = newNumber * (-1);
textdisplay.setText(total.toString());
}
else if (v.getId()== R.id.open){
}
else if (v.getId()== R.id.close){
}
last_button = v.getId();
}}
Here is my XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:layout_weight="0.58"
android:inputType="numberDecimal" >
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<Button
android:id="#+id/backspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="#+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="#+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="#+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="#+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="#+id/plusminus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+/-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="#+id/decimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="." />
<Button
android:id="#+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="( " />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.58" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.20" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.04"
android:orientation="vertical" >
<Button
android:id="#+id/history"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:text="History" />
<Button
android:id="#+id/equal"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:text="#string/_" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
What codeMagic said, store each in a variable and then maybe make one final String variable to hold it with String.format. So maybe
String finalString = String.format("This is the result %s and %s" ,
variableName, variableName);
then
textdisplay.setText(finalString);
Related
My app(API 11) creates EditTexts based on user input and gives each an Id. When the user fills each edit text I need to perform a null check, get and send the data to the next activity. But now I'm stuck because I can not refer to them. what should I do?
Thanks
The XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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.wima.civilengineeringcalculator.twodtruss"
tools:showIn="#layout/activity_twodtruss"
android:orientation="vertical"
android:background="#4A148C"
android:layoutDirection="ltr">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_alignParentTop="true"
android:id="#+id/scr1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تحلیل خرپاهای دو بعدی:"
android:textColor="#FFFFFF"
android:id="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/textView1"
android:background="#4A148C"
android:id="#+id/V3"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/V3"
android:background="#4A148C"
android:id="#+id/V5"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/V5"
android:background="#FF909090"
android:id="#+id/V4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تعداد اعضا:"
android:textColor="#FFFFFF"
android:id="#+id/textView4"
android:layout_below="#+id/V4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/textView4"
android:layout_toStartOf="#+id/textView4"
android:textColor="#FFFFFF"
android:ems="10"
android:id="#+id/editT"
android:layout_below="#+id/V4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تعداد گره های سازه:"
android:id="#+id/textView5"
android:textColor="#FFFFFF"
android:layout_below="#+id/editT"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:textColor="#FFFFFF"
android:id="#+id/editT2"
android:layout_below="#+id/editT"
android:layout_toLeftOf="#+id/textView5"
android:layout_toStartOf="#+id/textView5"
android:layout_alignLeft="#+id/editT"
android:layout_alignStart="#+id/editT"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تعداد مقاطع مورد نیاز:"
android:textColor="#FFFFFF"
android:id="#+id/textView6"
android:layout_below="#+id/editT2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/editT3"
android:textColor="#FFFFFF"
android:layout_below="#+id/editT2"
android:layout_alignLeft="#+id/editT"
android:layout_toLeftOf="#+id/textView6"
android:layout_alignStart="#+id/editT"
android:layout_toStartOf="#+id/textView6"
android:inputType="number" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="مرحله بعد"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button5"
android:layout_below="#+id/editT3"
android:layout_alignLeft="#+id/editT3"
android:layout_alignRight="#+id/editT3"
android:layout_alignStart="#+id/editT3"
android:layout_alignEnd="#+id/editT3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="راهنما"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button6"
android:layout_below="#+id/editT3"
android:layout_alignLeft="#+id/textView5"
android:layout_alignRight="#+id/textView4"
android:layout_alignStart="#+id/textView5"
android:layout_alignEnd="#+id/textView4"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/button5"
android:background="#4A148C"
android:id="#+id/V1"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/V1"
android:background="#FF909090"
android:id="#+id/V2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="مشخصات مقاطع را وارد نمایید:"
android:textColor="#FFFFFF"
android:id="#+id/textView7"
android:layout_below="#+id/V1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/sectable"
android:layout_below="#+id/textView7">
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="مرحله بعد"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button1"
android:layout_below="#+id/sectable"
android:layout_alignLeft="#+id/button6"
android:layout_alignRight="#+id/button6"
android:layout_alignStart="#+id/button6"
android:layout_alignEnd="#+id/button6"
android:onClick="next1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="شروع مجدد"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button2"
android:layout_below="#+id/sectable"
android:layout_alignLeft="#+id/editT3"
android:layout_alignRight="#+id/editT3"
android:layout_alignStart="#+id/editT3"
android:layout_alignEnd="#+id/editT3"/>
</RelativeLayout>
</ScrollView>
The Java code :
public class MatrixAnalysis extends AppCompatActivity {
private TableLayout mLayout;
private EditText noofsecs;
private EditText Noele;
private EditText Nonode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twodtruss);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Noele = (EditText) findViewById(R.id.editT);
Nonode = (EditText) findViewById(R.id.editT2);
noofsecs = (EditText) findViewById(R.id.editT3);
title = (TextView) findViewById(R.id.textView1);
Button bt_calculate1 = (Button) findViewById(R.id.button5);
Button bt_help = (Button) findViewById(R.id.button6);
Button bt_N1 = (Button) findViewById(R.id.button1);
Button bt_reset = (Button) findViewById(R.id.button2);
TextView inter = (TextView) findViewById(R.id.textView7);
bt_help.setVisibility(View.INVISIBLE);
bt_N1.setVisibility(View.INVISIBLE);
bt_reset.setVisibility(View.INVISIBLE);
inter.setVisibility(View.INVISIBLE);
bt_calculate1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if ( Noele.getText().toString().trim().equals("")
|| Nonode.getText().toString().trim().equals("")
|| noofsecs.getText().toString().trim().equals("")) {
Snackbar.make(findViewById(android.R.id.content), "لطفا مقادیر تمامی متغیرها را وارد کنید ", Snackbar.LENGTH_LONG)
.show();
}
else {
double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
mLayout = (TableLayout) findViewById(R.id.sectable);
prepuserinput();
}
}
}
});
Double Dnoofsecs = 5.0;
int aRows = Dnoofsecs.intValue();
double [] Asec = new double[aRows];
double [] Esec = new double[aRows];
double [] Gsec = new double[aRows];
double [] Iysec = new double[aRows];
double [] Izsec = new double[aRows];
double [] Jsec = new double[aRows];
public void prepuserinput() {
Button bt_calculate1 = (Button) findViewById(R.id.button5);
Button bt_help = (Button) findViewById(R.id.button6);
Button bt_N1 = (Button) findViewById(R.id.button1);
Button bt_reset = (Button) findViewById(R.id.button2);
TextView inter = (TextView) findViewById(R.id.textView7);
Noele = (EditText) findViewById(R.id.editT);
Nonode = (EditText) findViewById(R.id.editT2);
noofsecs = (EditText) findViewById(R.id.editT3);
bt_calculate1.setVisibility(View.INVISIBLE);
bt_help.setVisibility(View.VISIBLE);
bt_N1.setVisibility(View.VISIBLE);
bt_reset.setVisibility(View.VISIBLE);
inter.setVisibility(View.VISIBLE);
Noele.setEnabled(false);
Nonode.setEnabled(false);
noofsecs.setEnabled(false);
Intent i = getIntent();
String pro = i.getStringExtra("Problem");
double pro1 = Double.parseDouble(pro.replace(",", "."));
int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
int width1 = width * 13 / 100;
int width2 = width * 12 / 100;
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PAREN));
TextView textview = new TextView(this);
textview.setId(nsecctr + 0);
textview.setText(":" + nsecctr + "مقطع");
textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
textview.setTextColor(Color.parseColor("#FFFFFF"));
tr.addView(textview);
EditText A = new EditText(this);
A.setId(nsecctr + 0);
A.setHint("A");
A.setLayoutParams(new TableRow.LayoutParams(width2, 50));
A.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
A.setTextColor(Color.parseColor("#FFFFFF"));
A.setHintTextColor(Color.parseColor("#bdbdbd"));
//width, height
tr.addView(A);
EditText E = new EditText(this);
E.setId(nsecctr + 0);
E.setHint("E");
E.setLayoutParams(new TableRow.LayoutParams(width2, 50));
E.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
E.setTextColor(Color.parseColor("#FFFFFF"));
E.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(E);
EditText G = new EditText(this);
G.setId(nsecctr + 0);
G.setHint("G");
G.setLayoutParams(new TableRow.LayoutParams(width2, 50));
G.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
G.setTextColor(Color.parseColor("#FFFFFF"));
G.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(G);
EditText Iy = new EditText(this);
Iy.setId(nsecctr + 0);
Iy.setHint("Iy");
Iy.setLayoutParams(new TableRow.LayoutParams(width1, 50));
Iy.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Iy.setTextColor(Color.parseColor("#FFFFFF"));
Iy.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(Iy);
EditText Iz = new EditText(this);
Iz.setId(nsecctr + 0);
Iz.setHint("Iz");
Iz.setLayoutParams(new TableRow.LayoutParams(width1, 50));
Iz.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Iz.setTextColor(Color.parseColor("#FFFFFF"));
Iz.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(Iz);
EditText J = new EditText(this);
J.setId(nsecctr + 0);
J.setHint("J");
J.setLayoutParams(new TableRow.LayoutParams(width2, 50));
J.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
J.setTextColor(Color.parseColor("#FFFFFF"));
J.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(J);
mLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT));
}
public void next1 (View view) {
double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
Jnew = (EditText) findViewById(R.id.editT3);
for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
mLayout = (TableLayout) findViewById(R.id.sectable);
Asec[nsecctr] = A.getId();
Esec[nsecctr] = E.getId();
Gsec[nsecctr] = G.getId();
Iysec[nsecctr] = Iy.getId();
Izsec[nsecctr] = Iz.getId();
Jsec[nsecctr] = J.getId();
}
}
You should create a HashMap for stores all your EditText
HashMap <String, EditText > mapEditText = new HashMap<String, EditText >();
Whenever you create a EditText programmatically, remember to put it to HashMap
EditText A = new EditText(this);
...
mapEditText.put("A",A);
EditText E = new EditText(this);
...
mapEditText.put("E",E);
....
And in next method, you can access to your EditText via
public void next1 (View view) {
...
Asec[nsecctr] = mapEditText.get("A").getId();
Esec[nsecctr] = mapEditText.get("E").getId();
}
Try changing your next1 as below
public void next1 (View view) {
double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
Jnew = (EditText) findViewById(R.id.editT3);
for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
mLayout = (TableLayout) findViewById(R.id.sectable);
Asec[nsecctr] = view.A.getId();
Esec[nsecctr] = view.E.getId();
Gsec[nsecctr] = view.G.getId();
Iysec[nsecctr] = view.Iy.getId();
Izsec[nsecctr] = view.Iz.getId();
Jsec[nsecctr] = view.J.getId();
}
}
This is the code.
What more information do i need to put here?
I'm trying to find out why my text and button views are widening and then returning to normal during running of this code.
It keeps saying i need more details so i guess im just gonna talk about some nonsense here.
package com.notesquirrel.johnald.memorymagic;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class GameActivity extends AppCompatActivity implements View.OnClickListener {
Animation wobble;
SharedPreferences prefs;
SharedPreferences.Editor editor;
String dataName = "MyData";
String intName = "MyInt";
int defaultInt = 0;
int highScore;
TextView textScore;
TextView textDifficulty;
TextView textWatchGo;
Button button1;
Button button2;
Button button3;
Button button4;
Button buttonRestart;
int difficultyLevel = 3;
int[] sequenceToCopy = new int[100];
private Handler myHandler;
boolean playSequence = false;
int elementToPlay = 0;
int playerResponses;
int playerScore;
boolean isResponding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
wobble = AnimationUtils.loadAnimation(this, R.anim.wobble);
prefs = getSharedPreferences(dataName, MODE_PRIVATE);
editor = prefs.edit();
highScore = prefs.getInt(intName, defaultInt);
final MediaPlayer powerup7 = MediaPlayer.create(this, R.raw.poweru);
final MediaPlayer powerup8 = MediaPlayer.create(this, R.raw.poweru2);
final MediaPlayer powerup9 = MediaPlayer.create(this, R.raw.poweru3);
final MediaPlayer powerup10 = MediaPlayer.create(this, R.raw.poweru4);
textScore = (TextView) findViewById(R.id.textScore);
assert textScore != null;
textScore.setText("Score: " + playerScore);
textDifficulty = (TextView) findViewById(R.id.textDifficulty);
assert textDifficulty != null;
textDifficulty.setText("Level: " + difficultyLevel);
textWatchGo = (TextView) findViewById(R.id.textWatchGo);
button1 = (Button) findViewById(R.id.button21);
button2 = (Button) findViewById(R.id.button32);
button3 = (Button) findViewById(R.id.button43);
button4 = (Button) findViewById(R.id.button54);
buttonRestart = (Button) findViewById(R.id.buttonRestart);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
buttonRestart.setOnClickListener(this);
//thread
myHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (playSequence) {
button1.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
button4.setVisibility(View.VISIBLE);
switch (sequenceToCopy[elementToPlay]) {
case 1:
// button1.setVisibility(View.INVISIBLE);
button1.startAnimation(wobble);
powerup7.start();
break;
case 2:
//button2.setVisibility(View.INVISIBLE);
button2.startAnimation(wobble);
powerup8.start();
break;
case 3:
//button3.setVisibility(View.INVISIBLE);
button3.startAnimation(wobble);
powerup9.start();
break;
case 4:
// button4.setVisibility(View.INVISIBLE);
button4.startAnimation(wobble);
powerup10.start();
break;
}
elementToPlay++;
if (elementToPlay == difficultyLevel) {
sequenceFinished();
}
}
myHandler.sendEmptyMessageDelayed(0, 900);
}
};
myHandler.sendEmptyMessage(0);
playASequence();
}
#Override
public void onClick(View v) {
final MediaPlayer powerup7 = MediaPlayer.create(this, R.raw.poweru);
final MediaPlayer powerup8 = MediaPlayer.create(this, R.raw.poweru2);
final MediaPlayer powerup9 = MediaPlayer.create(this, R.raw.poweru3);
final MediaPlayer powerup10 = MediaPlayer.create(this, R.raw.poweru4);
if (!playSequence) {
switch (v.getId()) {
case R.id.button21:
powerup7.start();
checkElement(1);
break;
case R.id.button32:
powerup8.start();
checkElement(2);
break;
case R.id.button43:
powerup9.start();
checkElement(3);
break;
case R.id.button54:
powerup10.start();
checkElement(4);
break;
case R.id.buttonRestart:
difficultyLevel = 3;
playerScore = 0;
textScore.setText("Score: " + playerScore);
playASequence();
break;
}
}
}
public void createSequence() {
//For choosing a random button
Random randInt = new Random();
int ourRandom;
for (int i = 0; i < difficultyLevel; i++) {
//get a random number between 1 and 4
ourRandom = randInt.nextInt(4);
ourRandom++;//make sure it is not zero
//Save that number to our array
sequenceToCopy[i] = ourRandom;
}
}
public void playASequence() {
createSequence();
isResponding = false;
elementToPlay = 0;
playerResponses = 0;
textWatchGo.setText("WATCH!");
playSequence = true;
}
public void sequenceFinished() {
playSequence = false;
//make sure all the buttons are made visible
// button1.setVisibility(View.VISIBLE);
// button2.setVisibility(View.VISIBLE);
// button3.setVisibility(View.VISIBLE);
// button4.setVisibility(View.VISIBLE);
textWatchGo.setText("GO!");
isResponding = true;
}
public void checkElement(int thisElement) {
if (isResponding) {
playerResponses++;
if (sequenceToCopy[playerResponses - 1] == thisElement) {//Correct
playerScore = playerScore + ((thisElement + 1) * 2);
textScore.setText("Score: " + playerScore);
if (playerResponses == difficultyLevel) {//got the whole sequence
//don't checkElelment anymore
isResponding = false;
//now raise the difficulty
difficultyLevel++;
//and play another sequence
playASequence();
}
} else {//wrong answer
textWatchGo.setText("FAILED!");
//don't checkElelment anymore
isResponding = false;
if (playerScore > highScore) {
highScore = playerScore;
editor.putInt(intName, highScore);
editor.commit();
Toast.makeText(getApplicationContext(), "New high score!!", Toast.LENGTH_LONG).show();
}
}
}
}
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.notesquirrel.johnald.memorymagic.GameActivity"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score: 999"
android:textSize="40sp"
android:id="#+id/textScore"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Difficulty: 4"
android:textSize="25sp"
android:id="#+id/textDifficulty"
android:layout_below="#+id/textScore"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Watch/Go"
android:id="#+id/textWatchGo"
android:layout_marginTop="48dp"
android:textSize="25sp"
android:layout_below="#+id/textDifficulty"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button21"
android:layout_marginTop="50dp"
android:layout_below="#+id/textWatchGo"
android:layout_alignRight="#+id/textScore"
android:layout_alignEnd="#+id/textScore"
android:layout_alignLeft="#+id/textScore"
android:layout_alignStart="#+id/textScore" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button32"
android:layout_marginTop="10dp"
android:layout_below="#+id/button21"
android:layout_alignRight="#+id/button21"
android:layout_alignEnd="#+id/button21"
android:layout_alignLeft="#+id/button21"
android:layout_alignStart="#+id/button21" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_marginTop="10dp"
android:id="#+id/button43"
android:layout_below="#+id/button32"
android:layout_alignRight="#+id/button32"
android:layout_alignEnd="#+id/button32"
android:layout_alignLeft="#+id/button32"
android:layout_alignStart="#+id/button32" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_marginTop="10dp"
android:id="#+id/button54"
android:layout_below="#+id/button43"
android:layout_alignRight="#+id/button43"
android:layout_alignEnd="#+id/button43"
android:layout_alignLeft="#+id/button43"
android:layout_alignStart="#+id/button43" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart"
android:id="#+id/buttonRestart"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I have no problem with my device. But I removed some constraints.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.notesquirrel.johnald.memorymagic.GameActivity"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score: 999"
android:textSize="40sp"
android:id="#+id/textScore"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Difficulty: 4"
android:textSize="25sp"
android:id="#+id/textDifficulty"
android:layout_below="#+id/textScore"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Watch/Go"
android:id="#+id/textWatchGo"
android:layout_marginTop="48dp"
android:textSize="25sp"
android:layout_below="#+id/textDifficulty"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button21"
android:layout_marginTop="50dp"
android:layout_below="#+id/textWatchGo"
android:layout_alignRight="#+id/textScore"
android:layout_alignLeft="#+id/textScore"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button32"
android:layout_marginTop="10dp"
android:layout_below="#+id/button21"
android:layout_alignRight="#+id/button21"
android:layout_alignLeft="#+id/button21"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_marginTop="10dp"
android:id="#+id/button43"
android:layout_below="#+id/button32"
android:layout_alignRight="#+id/button32"
android:layout_alignLeft="#+id/button32"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_marginTop="10dp"
android:id="#+id/button54"
android:layout_below="#+id/button43"
android:layout_alignRight="#+id/button43"
android:layout_alignLeft="#+id/button43"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart"
android:id="#+id/buttonRestart"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
I wanted to create a simple quadratic equation solver but when I run it, it gets caught at the first if statement of each button. Any ideas?
public class MainActivity extends AppCompatActivity {
Button button1, button2;
String a_temp;
EditText b_temp;
EditText c_temp;
TextView tv1, tv2;
Double a,b,c;
#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();
}
});
a_temp = ((EditText)findViewById(R.id.editText)).getText().toString();
if (a_temp.equals("")) {
a = Double.valueOf(0);
} else {
a = Double.valueOf(a_temp);
}
b_temp = (EditText)findViewById(R.id.editText2);
if (!b_temp.getText().toString().equals("")) {
b = Double.parseDouble(b_temp.getText().toString());
}else {
b = Double.valueOf(0);
}
c_temp = (EditText)findViewById(R.id.editText3);
if (!c_temp.getText().toString().equals("")) {
c = Double.parseDouble(c_temp.getText().toString());
}else {
c = Double.valueOf(0);
}
tv1 = (TextView) findViewById(R.id.textView2);
tv2 = (TextView) findViewById(R.id.textView3);
button1 = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (a == 0) {
tv1.setText("That's not a quadratic equation");
}else {
if (b*b - 4*a*c < 0) {
tv1.setText("This quadratic equation has no real roots");
} else {
double root1 = (-b + Math.sqrt(Math.pow(b,2) - 4 * a * c))/(2*a);
double root2 = (-b - Math.sqrt(Math.pow(b,2) - 4 * a * c))/(2*a);
double result = Math.max(root1,root2);
tv1.setText(String.valueOf(result));
}
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (a == 0) {
tv2.setText("That's not a quadratic equation");
}else {
if (b * b - 4 * a * c < 0) {
tv2.setText("This quadratic equation has no real roots");
} else {
double root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
double root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
double result = Math.min(root1, root2);
tv2.setText(" " + result);
}
}
};
});
}
XML code:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Type the operands of the quadratic equation you want to solve"
android:id="#+id/textView"
android:layout_marginTop="39dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Root #1"
android:id="#+id/button"
android:layout_marginTop="60dp"
android:layout_below="#+id/editText3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Root #2"
android:id="#+id/button2"
android:layout_marginTop="69dp"
android:layout_alignTop="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView2"
android:layout_alignBottom="#+id/button"
android:layout_alignTop="#+id/button"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|numberSigned"
android:ems="10"
android:id="#+id/editText"
android:layout_marginTop="32dp"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|numberSigned"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|numberSigned"
android:ems="10"
android:id="#+id/editText3"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x^2"
android:id="#+id/textView4"
android:layout_alignTop="#+id/editText"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x"
android:id="#+id/textView5"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/textView4"
android:layout_alignStart="#+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="=0"
android:id="#+id/textView6"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
</RelativeLayout>
You are comparing double values with ==.
The problem is that it will likely not be equal 0, because of the minor error it has when storing double values. For example, it will be something like 0.000000001 or -0.00000001. Instead, compare like this:
if (Math.abs(a) < 0.0000001) { ... }
Also you forgot to divide your roots by 2 * a.
So the main problem I see is, that you are assigning your values for a, b and c in the onCreate method of your activity class.
This means, as soon as your activity is created, i.e. before the user had any time to input any data into your EditText fields, you are trying to read said non-existing input - i.e. 0.
This is why in each onClick method, the first if() clause which checks for zero-values, returns true. Because the values are zero.
What you should do is move this part of each variable:
if (!c_temp.getText().toString().equals("")) {
c = Double.parseDouble(c_temp.getText().toString());
}else {
c = Double.valueOf(0);
}
into your OnClickListener classes, i.e. into the onClick methods, to "refresh" the values you are using in your calculations based on the current user input.
I'm making a very simple calculator on Android. It shows no error on eclipse, but when I run it on my Android device, it stops working on clicking the buttons.
Here's my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.simplecalculator.MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="#string/enter_a_no"
android:inputType="numberSigned|numberDecimal"
/>
<requestFocus />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/plus"
android:layout_alignBottom="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:text="#string/minus"
android:onClick="minusClick" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/plus"
android:layout_below="#+id/plus"
android:text="#string/multiply"
android:onClick="multiplyClick"/>
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/multiply"
android:layout_alignBottom="#+id/multiply"
android:layout_alignLeft="#+id/minus"
android:text="#string/divide"
android:onClick="divideClick" />
<Button
android:id="#+id/equals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/divide"
android:layout_alignTop="#+id/minus"
android:layout_toRightOf="#+id/minus"
android:onClick="equalClick"
android:text="#string/equals" />
<Button
android:id="#+id/_1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/plus"
android:layout_below="#+id/editText1"
android:layout_marginTop="14dp"
android:text="#string/_1"
android:onClick="_1click"/>
<Button
android:id="#+id/_2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_1"
android:layout_alignBottom="#+id/_1"
android:layout_toRightOf="#+id/_1"
android:text="#string/_2"
android:onClick="_2click"/>
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/divide"
android:layout_alignBottom="#+id/divide"
android:layout_toRightOf="#+id/divide"
android:onClick="clearClick"
android:text="#string/ce" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/_1"
android:layout_marginLeft="33dp"
android:layout_marginTop="127dp"
android:hint="#string/plus"
android:onClick="plusClick"
android:text="#string/plus" />
<Button
android:id="#+id/_4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_1"
android:layout_toLeftOf="#+id/_2"
android:text="#string/_4"
android:onClick="_4click"/>
<Button
android:id="#+id/_5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_4"
android:layout_alignBottom="#+id/_4"
android:layout_alignLeft="#+id/_2"
android:text="#string/_5"
android:onClick="_5click" />
<Button
android:id="#+id/_6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/_5"
android:layout_alignBottom="#+id/_5"
android:layout_toRightOf="#+id/_5"
android:text="#string/_6"
android:onClick="_6click"/>
<Button
android:id="#+id/_7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_4"
android:layout_toLeftOf="#+id/_5"
android:text="#string/_7"
android:onClick="_7click"/>
<Button
android:id="#+id/_8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_5"
android:layout_toLeftOf="#+id/_6"
android:text="#string/_8"
android:onClick="_8click"/>
<Button
android:id="#+id/_9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_5"
android:layout_toRightOf="#+id/_5"
android:text="#string/_9"
android:onClick="_9click"/>
<Button
android:id="#+id/_3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/_5"
android:layout_toRightOf="#+id/_2"
android:text="#string/_3"
android:onClick="_3click"/>
<Button
android:id="#+id/_0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/_6"
android:layout_toRightOf="#+id/_6"
android:text="#string/_0"
android:onClick="_0click"/>
</RelativeLayout>
Here's my Java code:
package com.example.simplecalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private double vrble;
private double rslt;
private double vrble2;
String text;
EditText etext;
Button plus;
Button minus;
Button multiply;
Button divide;
Button equal;
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
vrble = 0;
rslt = 0;
etext = (EditText) findViewById(R.id.editText1);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal = (Button) findViewById(R.id.equals);
} else {
vrble = 0;
rslt = 0;
vrble2 = 0;
etext = (EditText) findViewById(R.id.editText1);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal = (Button) findViewById(R.id.equals);
}
}
public void _0click(View v) {
text = etext.getText().toString();
etext.setText(text + "0");
}
public void _1click(View v) {
text = etext.getText().toString();
etext.setText(text + "1");
}
public void _2click(View v) {
text = etext.getText().toString();
etext.setText(text + "2");
}
public void _3click(View v) {
text = etext.getText().toString();
etext.setText(text + "3");
}
public void _4click(View v) {
text = etext.getText().toString();
etext.setText(text + "4");
}
public void _5click(View v) {
text = etext.getText().toString();
etext.setText(text + "5");
}
public void _6click(View v) {
text = etext.getText().toString();
etext.setText(text + "6");
}
public void _7click(View v) {
text = etext.getText().toString();
etext.setText(text + "7");
}
public void _8click(View v) {
text = etext.getText().toString();
etext.setText(text + "8");
}
public void _9click(View v) {
text = etext.getText().toString();
etext.setText(text + "9");
}
public void plusClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble + vrble2;
}
public void minusClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble - vrble2;
}
public void multiplyClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble * vrble2;
}
public void divideClick(View v) {
vrble = Double.parseDouble(etext.getText().toString());
etext.setText("");
vrble2 = Double.parseDouble(etext.getText().toString());
rslt = vrble / vrble2;
}
public void clearClick(View v) {
vrble = 0;
rslt = 0;
etext.setText("0");
}
public void equalClick(View v) {
etext.setText("" + rslt);
}#
Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#
Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What should I do?!!?!
One issue that I can se is a NumberFormatException that you might get. See these lines in any of your method like plusClick():
vrble = Double.parseDouble(etext.getText().toString());
etext.setText(""); //setting EditText to empty
vrble2 = Double.parseDouble(etext.getText().toString()); // parsing the empty string to Double
You are setting the eText to "" and then you are parsing it to Double in the next line. This is throwing the error. You might need to clear your login for all the actions like plus minus etc.
For more, your stacktrace will be helpful.
I created a tictactoe android game, and when i try to link the strings.xml file to activity.java I receive an error.
Here is my activity.java
package com.tictactoeoyna.www;
import android.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TicTacToeOynaActivity extends Activity {
private TicTacToeOyun mGame;
private Button mBoardButtons[];
private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;
private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;
private boolean mHumanFirst = true;
private boolean mGameOver = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBoardButtons = new Button[TicTacToeOyun.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);// it should be blue but not
mBoardButtons[1] = (Button) findViewById(R.id.two);//same
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);//until here
mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humanCount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);
mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGame = new TicTacToeOyun();
startNewGame();
}
private void startNewGame()
{
mGame.clearBoard();
for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;
}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
mHumanFirst = true;
}
}
private class ButtonClickListener implements View.OnClickListener
{
int location;
public ButtonClickListener(int location)
{
this.location = location;
}
public void onClick(View view)
{
if (!mGameOver)
{
if (mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);
int winner = mGame.checkForWinner();
if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
winner = mGame.checkForWinner();
}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
}
else if (winner == 2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
}
else
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
}
}
}
}
}
private void setMove(char player, int location)
{
mGame.setMove(player, location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
mBoardButtons[location].setTextColor(Color.RED);
}
}
The error is on one, two, three...nine and information count, human count etc. Here is my fragment file, so why can't I see one, two, three....nine labeled blue? Why is it not recognized? What have I done wrong ? I received the errors after findviewbyid.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/playArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="one"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/one"
android:textSize="70dp" />
<Button
android:id="#+id/two"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/two"
android:textSize="70dp" />
<Button
android:id="#+id/three"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/three"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/four"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/four"
android:textSize="70dp" />
<Button
android:id="#+id/five"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/five"
android:textSize="70dp" />
<Button
android:id="#+id/six"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/six"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/seven"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/seven"
android:textSize="70dp" />
<Button
android:id="#+id/eight"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/eight"
android:textSize="70dp" />
<Button
android:id="#+id/nine"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="#string/nine"
android:textSize="70dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:text="#string/info"
android:textSize="25dp" />
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/human"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/human" />
<TextView
android:id="#+id/humanCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/ties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ties" />
<TextView
android:id="#+id/tiesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/android" />
<TextView
android:id="#+id/androidCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>
import com.tictactoeoyna.www.R;
and removeimport android.R;
When you got this type of error at anywhere and you have import android.R; then just remove import android.R;.