I am writing a simple computing machine which basically shows in the text view the sum of 2 numbers I entered in two edit text. Theoretically, it should be right, but in my case I feel like something went wrong, perhaps about missing components. At first, I thought the problem had come from the edit text but later I changed it and nothing new happened. That makes me doubt about the use of onClick attribute. The program automatically stops when I pressed the button
Specifically, I have my java code
package com.example.computing_machine;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Computing_Machine extends Activity {
private TextView text;
protected void onCreate(Bundle x) {
super.onCreate(x);
setContentView(R.layout.activity_computing_machine);
text = (TextView) findViewById(R.id.textView3);
}
public void sum(View v) {
/* EditText e1= (EditText) findViewById(R.id.editText1);
EditText e2= (EditText) findViewById(R.id.editText2);
int a = Integer.parseInt(e1.getText() + "");
int b = Integer.parseInt(e2.getText() + ""); */
text.setText(5);
}
}
And this is my XML file
<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.computing_machine.Computing_Machine" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/number1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignStart="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="23dp"
android:text="#string/number2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_toRightOf="#+id/textView1"
android:layout_toEndOf="#+id/textView1"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignStart="#+id/editText1"
android:layout_below="#+id/editText1"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_alignEnd="#+id/editText2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:onClick="sum"/>
Try this:
Code:
int a = Integer.parseInt(""+e1.getText().toString());
int b = Integer.parseInt(""+e2.getText().toString());
Try
text.setText("" + 5);
setText takes a string as its argument. You are passing an int.
Related
I am creating a diet app for myself and I do not know how to transfer result from one calculation which was done in a different activity and use it in another one.
To make things more complicated, I want my spinner in MoreLooseWeightDetails.class to have a list of different diet types which have different fat, proteins and carb ratio. Every time the user choose different type it should automatically change the ratio.
This is where the calculation is being done and has to be trnasfered to MoreLooseWeightDetails.class
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class LooseWeight extends AppCompatActivity {
TextView TotalCal;
EditText numb2;
Spinner ActLvl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loose_weight);
numb2 = (EditText) findViewById(R.id.value1);
ActLvl = (Spinner) findViewById(R.id.value2);
TotalCal = (TextView)findViewById(R.id.resultDisplay);
final Spinner ActLvl = (Spinner)findViewById(R.id.value2);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(LooseWeight.this, android.R.layout.simple_expandable_list_item_1,getResources().getStringArray(R.array.lvl));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ActLvl.setAdapter(myAdapter);
Button calcBtn = (Button)findViewById(R.id.result);
calcBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
float numb3 = Float.parseFloat(numb2.getText().toString());
float a = numb3 * 24;
float b = a * Float.parseFloat(ActLvl.getSelectedItem().toString());
float c = b - 500;
TotalCal.setText(Float.toString(c));
}
});
Button extra = (Button)findViewById(R.id.advance);
extra.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),MoreLooseWeightDetails.class);
startActivity(intent);
}
});
}
}
Here is the MoreLooseWeightDetails.class
package com.fitup.fit_up;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MoreLooseWeightDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_loose_weight_details);
}
}
Here is the MoreLooseWeightDetails.xml
<?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:id="#+id/activity_more_loose_weight_details"
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.fitup.fit_up.MoreLooseWeightDetails">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Title3"
tools:text="More"
android:textSize="36sp" />
<TextView
android:text="Select Type of Diet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Title3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:id="#+id/Info3"
android:textSize="30sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Info3"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="#+id/spinner" />
<TextView
android:text="You Should Eat :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/Info4"
android:textSize="30sp"
android:layout_below="#+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Protein (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit1"
android:textSize="24sp"
android:layout_below="#+id/Info4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="78dp" />
<TextView
android:text="Fat (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit3"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/result1"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignLeft="#+id/result2"
android:layout_alignStart="#+id/result2" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/unit2"
android:id="#+id/result2"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/result3"
android:layout_alignStart="#+id/result3" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit2"
android:id="#+id/result3"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/unit2"
android:layout_toEndOf="#+id/unit2" />
<TextView
android:text="Carbohydrates (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/unit2"
android:textSize="24sp"
android:layout_below="#+id/unit1"
android:layout_alignRight="#+id/unit1"
android:layout_alignEnd="#+id/unit1" />
</RelativeLayout>
I am trying to run a code for matching passwords. I do not understand the error message:
variable pass is already defined in method onclickregister(View)
CODE:
package com.example.admin.rent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Register extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void onclickregister(View view) {
if(view.getId()==R.id.register)
{
EditText name = (EditText)findViewById(R.id.name);
EditText sname = (EditText)findViewById(R.id.surname);
EditText email = (EditText)findViewById(R.id.email);
EditText mnum = (EditText)findViewById(R.id.mobileno);
EditText pass = (EditText)findViewById(R.id.pass);
EditText pass1 = (EditText)findViewById(R.id.pass1);
String namestr = name.getText().toString();
String snamestr = sname.getText().toString();
String emailstr = email.getText().toString();
String mnumstr = mnum.getText().toString();
String passstr = pass.getText().toString();
String pass1str = pass1.getText().toString();
if(!passstr.equals(pass1str))
{
//popup msg
Toast pass = Toast.makeText(Register.this , "Passwords don't match!", Toast.LENGTH_SHORT);
pass.show();
}
}
XML:
<?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="wrap_content"
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.admin.rent.Register">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:hint="#string/name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="#string/surname"
android:ems="10"
android:id="#+id/surname"
android:layout_below="#+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/email"
android:layout_below="#+id/mobileno"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="#string/email"
android:layout_marginBottom="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass"
android:layout_below="#+id/email"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="#string/password"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass1"
android:hint="#string/confirm_password"
android:layout_below="#+id/pass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="+91"
android:id="#+id/textView2"
android:layout_below="#+id/surname"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center|clip_vertical"
android:textSize="18dp"
android:layout_alignBottom="#+id/mobileno" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/mobileno"
android:layout_below="#+id/surname"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_alignParentEnd="true"
android:hint="#string/mobile_number"
android:layout_toRightOf="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginLeft="10dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/register2"
android:id="#+id/register"
android:layout_below="#+id/pass1"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:layout_marginTop="25dp"
android:textSize="25sp"
android:typeface="normal"
android:clickable="false"
android:width="170dp"
android:onClick="onclickregister" />
Rename the pass variable in that inner scope:
if(!passstr.equals(pass1str))
{
//popup msg
Toast pass = Toast.makeText(Register.this , "Passwords don't match!", Toast.LENGTH_SHORT);
pass.show();
}
to something else. The problem is that you already have a variable with that name defined in the outer scope:
EditText pass = (EditText)findViewById(R.id.pass);
i am a beginner android application developer. currently trying to build a simple calculator app nd want to show the calculated result by using Toast. but right now i am unable to use toast to display this output. there is my code
MainActivity.java
package com.example.calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
EditText input_field1,input_field2,result_field;
int result_var;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add_numbers(View v){
input_field1= (EditText) findViewById(R.id.field1);
String no1=input_field1.getText().toString();
input_field2= (EditText) findViewById(R.id.field2);
String no2=input_field2.getText().toString();
int field1_int=Integer.parseInt(no1);
int field2_int=Integer.parseInt(no2);
result_field= (EditText) findViewById(R.id.resultfield);
result_var=field1_int+field2_int;
result_field.setText("result "+result_var);
Toast.makeText(getApplicationContext(), result_var, Toast.LENGTH_LONG).show();
}
}
activity_main.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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:text="Substract" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:text="Divide" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="105dp"
android:text="Add"
android:onClick="add_numbers" />
<EditText
android:id="#+id/field1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="22dp"
android:ems="10" />
<EditText
android:id="#+id/resultfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button2"
android:layout_marginTop="16dp"
android:ems="10" />
<EditText
android:id="#+id/field2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button1"
android:ems="10" />
</RelativeLayout>
The problem is you need a string to be displayed in a toast. You are trying to display an int. The simplest thing to do is to wrap it with String using String.valueOf().
Toast.makeText(getApplicationContext(), String.valueOf(result_var), Toast.LENGTH_LONG).show();
As a side note generally in java we wouldn't use underscores _ in variable names. Better to name them like this.
EditText mInputField1,mInputField2,mResultField;
int mResultVar;
m for member variables.
Also better to put findViewById in onCreate.
result_var should be a string
String result_var="";
String Testing="testing";
Toast.makeText(getApplicationContext(), result_var+testing, Toast.LENGTH_LONG).show();
Otherwise use as according to you:
// if you have values of field1_int and field2_int
int result_var=field1_int+field2_int;
Toast.makeText(getApplicationContext(),String.valueOf(result_var), Toast.LENGTH_LONG).show();
Try this Toast message has to be String
Toast.makeText(getApplicationContext(), ""+result_var, Toast.LENGTH_LONG).show();
A advice, as you are beginner. You should read the required arguments accepted by any method you are using.
Read Toast
For example in your case.
Toast.makeText(getApplicationContext(), ""+result_var, Toast.LENGTH_LONG).show();
Or
Toast.makeText(getApplicationContext(),String.valueOf(result_var), Toast.LENGTH_LONG).show();
I am new to app making, and am making a simple calculator.
Whenever I click a button, I get the message "Unfortunately, Calculator has stopped." Any help would be greatly appreciated.
XML File:
<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=".Calculator" >
<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_marginLeft="70dp"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginLeft="70dp"
android:layout_marginTop="26dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Add"
android:onClick="DoAdd" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:text="Subtract"
android:onClick="DoMinus"/>
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/button2"
android:onClick="DoTimes"
android:text="Multiply" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="94dp"
android:text="TextView" />
Java File:
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}
public void doAdd(View v)
{
EditText firstNumber = (EditText)findViewById(R.id.editText1);
EditText secondNumber = (EditText)findViewById(R.id.editText2);
double x = Double.parseDouble(firstNumber.getText().toString());
double y = Double.parseDouble(secondNumber.getText().toString());
double total = x + y;
TextView answerSpace = (TextView)findViewById(R.id.textView1);
answerSpace.setText(Double.toString(total));
}
}
Thankyou.
Change your xml onClick attribute to exactly match the method name in your java code:
<Button
android:id="#+id/button1"
...
android:onClick="doAdd" />
Notice the lower-case d at the beginning.
If those two don't match, the app crashes while Android is trying to invoke the onClick method you specified.
FD_ already pointed out the mistake from your code.
But for better coding approach you can follow this. If you wish.
In your xml file, define single method name for onClick on each button ie for
addition,substration,division,multiplication.
What i mean to say...?
// For Addition....
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Add"
android:onClick="calculate" />
//Define android:onClick="calculate" for remaining 3 buttons
ie say for subtraction
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Subract"
android:onClick="calculate" />
do similarly for division and multiplication
Now, in Java File
1)Define Variables globally
2)Find their ids in onCreateMethod(), ie when the activity created.
How to do this?
public class Calculator extends Activity
{
EditText firstNumber,secondNumber;
onCreate()
{
firstNumber = (EditText)findViewById(R.id.editText1);
secondNumber = (EditText)findViewById(R.id.editText2);
}
public void calculate(View v)
{
switch(v.getId())
{
case R.id.button1:
// Do coding here addition....
break;
case R.id.button2:
// Do coding here substraction....
break;
case R.id.button3:
// Do coding here multiplication....
break;
case R.id.button4:
// Do coding here division....
break;
}
}
}
So like so many others i am new to android. I have looked everywhere for an explanation of how to do this but have found none.
this is the app im trying to make.
http://i.stack.imgur.com/JGhJP.png
Im assuming i have to take the input from the top two edittexts and put them into stings and them multiply them and set that answers equal to the bottom edittext?
Im just confused as to how i would correctly do that.
Here is what ive tried so far.
package com.wattzen.testcalculation;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
public class Main extends Activity {
private static final String total_Made = "total_Made";
private double tmmade;
private int hedittext;
private int etmade;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
tmmade = 0.0;
} else {
tmmade = savedInstanceState.getDouble(total_Made);
}
}
{
{
hedittext = (EditText) findViewById(R.id.ethours);
etmade = (EditText) findViewById(R.id.etmade);
etmade.addTextChangedListener(etmadeWatcher);}
}
private void updateStandard() {
double tmmade = hedittext * etmade;
tmmade.addTextChangedListener(tmmadeWatcher);
double toTal= tmmade;
}
}
and here is the 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" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hours Worked" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/ethours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="Amount made per hour" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/etmade"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/bcalculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="Calculate" />
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Money Made" />
</TableRow>
<EditText
android:id="#+id/tmmade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="number" />
</TableLayout>
I greatly appreciate any help you guys can give me. thanks.
You should be getting a ton of syntax errors that should clue you in on lines that need changing.
Since hedittext and etmade are ints, you cannot set them equal to an EditText, as you do in onCreate().
First you need to get the actual content of the EditTexts. You can do this by calling the edit text's getText() methods.
Then you need to parse the result of getText as an integer. You can do this using the Integer wrapper class' parseInt() method.
It should look more like this:
EditText editTextA = (EditText) findViewById(R.id.ethours);
int hours = Integer.parseInt(editTextA.getText().toString())
Your first step is to assign an onClick listener the calculate button. Once you do this, reference both the editTexts within onCreate. After this, you can get the text from the edit texts using the getText() function.
Here is a basic mockup of the code. Id suggest checking if the input is valid first and then multiplying
Button bcalculate = (Button)findViewById(R.id.bcalculate);
EditText hedittext = (EditText)findViewById(R.id.ethours);
EditText etmade = (EditText)findViewById(R.id.etmade);
EditText tmmade = (EditText)findViewById(R.id.edittext3);
bcalculate.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
try{
double e1 = Double.parseDouble(hedittext.getText().toString());
double e2 = Double.parseDouble(etmade.getText().toString());
tmmade.setText(e1*e2);
} catch (NumberFormatException e) {
}
});