intent.putExtra ( "result", (double) kg / (Math.pow (tall, 2)) / 10000);// <SecondActivity>
result = intent.getDoubleExtra double ("result", 0); //<ThirdActivity>
I have tried the following:
tall: 150 cm
kg: 50kg
result: 1.95 .... (original calculations. 19.5)
Even if the value does not appear 10 Multiply.
★ float cast upon the results will not come.
The problem occurs after fertilization in the post.
Android Studio with intent to other activities
(original code)
[SecondActivity]
public class SecondActivity extends Activity {
RadioGroup rg;
ImageButton button4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
rg = (RadioGroup) findViewById(R.id.radioGroup);
button4 = (ImageButton) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioButton rd = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
//String str_Qtype = rd.getText().toString();
EditText et1 = (EditText) findViewById(R.id.EditText02); // tall (inot typing error)
EditText et2 = (EditText) findViewById(R.id.EditText01); // kg (not typing error)
int tall = Integer.parseInt(et1.getText().toString());
int kg = Integer.parseInt(et2.getText().toString());
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtra("tall", Integer.parseInt(et1.getText().toString()));
intent.putExtra("kg", Integer.parseInt(et2.getText().toString()));
intent.putExtra("result",(double)kg/(Math.pow(tall,2))/10000);
startActivity(intent);
}
});
}
}
[ThridActivity]
package com.example.bmi_calculate;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class ThirdActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Intent intent = getIntent();
int tall = intent.getIntExtra("tall", 0);
int kg = intent.getIntExtra("kg", 0);
double result = intent.getDoubleExtra("result",0);
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView)findViewById(R.id.textView3);
tv1.setText("" + tall);
tv2.setText("" + kg);
tv3.setText(""+String.valueOf(result));
}
}
In your second activity you are storing your results in variables tall and kg create a third variable result and pass that variable to the method
#Override
public void onClick(View v) {
RadioButton rd = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
//String str_Qtype = rd.getText().toString();
EditText et1 = (EditText) findViewById(R.id.EditText02); // tall (inot typing error)
EditText et2 = (EditText) findViewById(R.id.EditText01); // kg (not typing error)
int tall = Integer.parseInt(et1.getText().toString());
int kg = Integer.parseInt(et2.getText().toString());
float result = kg/(tall*0.1)*(tall*0.1))
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtra("tall", tall);
intent.putExtra("kg", kg);
intent.putExtra("result",result);
startActivity(intent);
and in third activity
float result = intent.getFloatExtra("result",0);
To save the value, do this:
intent.putExtra("result",(float) (kg/((tall*0.01)*(tall*0.01))) ); // tall in cm unit
You need to get float value or save as int:
float result = intent.getFloatExtra("result", 0f);
[Edit] - fixed code typo
Related
XML LAYOUT SCREENSHOT
Trying to calculate the final amount of selected items in an another activity. Need to create order summary in another activity.
Tried introducing multiplication function but also need to hard code the quantity values to auto calculate the final amount.
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int q = Integer.valueOf(pantqty.getText().toString());
float r = Integer.valueOf(pantrate.getText().toString());
float amount = q * r;
result.setText(Float.toString(amount));
}
});
}
}
Need to create a method in OnCLickListener to calculate the amount from the items selected.can if else case be used on OnclickListerButton?
Thanks for the help.
You can do something like this:
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
EditText shirtQty;
EditText shirtRate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
shirtQty = (EditText) findViewById(R.id.shirtqtyId);
shirtRate = (EditText) findViewById(R.id.shirtRateId);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculateSum();
}
});
}
private void calculateSum(){
int q=0;
String pantQty=pantqty.getText().toString().trim();
if(!pantQty.isEmpty() && pantQty!="") {
q=Integer.valueOf(pantQty);
}
float r = Integer.valueOf(pantrate.getText().toString());
int a = Integer.valueOf(shirtQty.getText().toString());
float b = Integer.valueOf(shirtRate.getText().toString());
float amount = (q * r)+(a*b);
result.setText(Float.toString(amount));
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("total",amount);
startActivity(intent);
}
}
On the secondActivity you get get the total as:
float total=getIntent().getFloatExtra("total",0.0);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.lo_pantqty);
shirtqty = (EditText) findViewById(R.id.lo_shirtqty);
shareesqty = (EditText) findViewById(R.id.lo_shareesqty);
suitqty = (EditText) findViewById(R.id.lo_suitqty);
result = (TextView) findViewById(R.id.et_result);
SUBMIT = (Button) findViewById(R.id.io_enter);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculationSum();
}
});
}
private void calculationSum() {
int q = 0;
String pantQTY = pantqty.getText().toString().trim();
if (!pantQTY.isEmpty()&&pantQTY!=""){
q = Integer.valueOf(pantQTY);
}
int r = 0;
String shirtQTY = shirtqty.getText().toString().trim();
if (!shirtQTY.isEmpty()&&shirtQTY!=""){
r = Integer.valueOf(shirtQTY);
}
int a = 0;
String suitQTY = suitqty.getText().toString().trim();
if (!suitQTY.isEmpty()&&suitQTY!=""){
a = Integer.valueOf(shirtQTY);
}
int b = 0;
String shareesQTY = shareesqty.getText().toString().trim();
if (!shareesQTY.isEmpty()&&shareesQTY!=""){
b = Integer.valueOf(shirtQTY);
}
int amount = (q*30) + (r*35) + (a*220) + (b*60);
result.setText(Integer.toString(amount));
}
}
I'm a beginner, I did a simple task but I do not see the result in TextView. How can I solve my problem?
Code:
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText1, editText2;
TextView textView;
public void Add(View v)
{
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
textView.setText(String.valueOf(sum));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText1 = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
textView = findViewById(R.id.textView);
}
}
The second option I tried was:
textView.setText("SUM = " + String.valueOf(sum));
But then I receive a message:
Do not concatenate text displayed with setText. Use resource string with placeholders.
PS. Of course i added method android:onClick = "Add"
You can try sum.toString() instead of String.valueOf(sum)
This will do your work
textView.setText(Float.toString(sum))
you better make button onclick in java, then display the string value to the textview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleButtonClick();
}
private void handleButtonClick() {
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addAndSetResult();
}
});
}
private void addAndSetResult() {
// inside here do the add logic and set result to textview
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
String sumStr = String.valueOf(sum);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(sumStr);
}
I have been working on a basic Calculator App in Android Studios. I've gotten preety deep into it and I am happy with my progress so far. The problem that I am now facing is that when I am running the Calculator App, whenever I input a number in portrait mode and switch into landscape mode and try to add a number or do any operation on it, the app crashes. I used the onSaveInstanceState method to save the data and the onRestoreInstanceState method to retrieve it. I looked into documentation on how to use these two methods and I am sure that I am implemented them correctly. Any help would be appreciated.
I have pasted the code down below.
package com.mohamedali.calculatorapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText moeresult;
private EditText moenewNumber;
private TextView displayOperation;
//Comment: Variables to hold the operands and type of calculations
private Double operand1 = null;
private String pendingOperation = " = ";
public static final String STATE_PENDING_OPERATION = "Pending Operation";
public static final String STATE_OPERAND1 = "Operand 1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moeresult = (EditText) findViewById(R.id.result);
moenewNumber = (EditText) findViewById(R.id.NewNumber);
displayOperation = (TextView) findViewById(R.id.Operation);
Button button0 = (Button) findViewById(R.id.button0);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button buttonDecimal = (Button) findViewById(R.id.buttonDecimal);
Button buttonMultiply = (Button) findViewById(R.id.buttonMultiply);
Button buttonDivide = (Button) findViewById(R.id.buttonDivide);
Button buttonMinus = (Button) findViewById(R.id.buttonMinus);
Button buttonPlus = (Button) findViewById(R.id.buttonPlus);
Button buttonEqual = (Button) findViewById(R.id.buttonEqual);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
moenewNumber.append(b.getText().toString());
}
};
button0.setOnClickListener(listener);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
button6.setOnClickListener(listener);
button7.setOnClickListener(listener);
button8.setOnClickListener(listener);
button9.setOnClickListener(listener);
buttonDecimal.setOnClickListener(listener);
View.OnClickListener opListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Button b = (Button) view;
String op = b.getText().toString();
String value = moenewNumber.getText().toString();
try {
Double doublevalue = Double.valueOf(value);
performOperation(doublevalue, op);
} catch (NumberFormatException e) {
moenewNumber.setText("");
}
pendingOperation = op;
displayOperation.setText(pendingOperation);
}
};
buttonEqual.setOnClickListener(opListener);
buttonMultiply.setOnClickListener(opListener);
buttonMinus.setOnClickListener(opListener);
buttonDivide.setOnClickListener(opListener);
buttonPlus.setOnClickListener(opListener);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString(STATE_PENDING_OPERATION, pendingOperation);
if (operand1 != null) {
outState.putDouble(STATE_PENDING_OPERATION, operand1);
}
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
pendingOperation = savedInstanceState.getString(STATE_PENDING_OPERATION);
operand1 = savedInstanceState.getDouble(STATE_OPERAND1);
displayOperation.setText(pendingOperation);
}
private void performOperation(Double value, String operation) {
// first thing is that it checks if first value is null
if (null == operand1) {
operand1 = value;
} else {
if (pendingOperation.equals(" = ")) {
pendingOperation = operation;
}
switch (pendingOperation) {
case "=":
operand1 = value;
break;
case "/":
if (value == 0) {
operand1 = 0.0;
} else {
operand1 /= value;
}
break;
case "*":
operand1 *= value;
break;
case "-":
operand1 -= value;
break;
case "+":
operand1 += value;
break;
}
}
// Displays the result
moeresult.setText(operand1.toString());
// It then clears it
moenewNumber.setText("");
}
}
I'm new to android and Java. I wanna pass a variable (ac) from OnClickListener to another. I've tried this way, but i receive this error: cannot resolve symbol 'ac'. Can you help me please?
Button Calculate = (Button) theLayout.findViewById(R.id.button);
Button buttonb = (Button) theLayout.findViewById(R.id.buttonb);
final TextView tvac = (TextView) theLayout.findViewById(R.id.tvac);
final TextView tvh = (TextView) theLayout.findViewById(R.id.tvh);
final EditText eta = (EditText) theLayout.findViewById(R.id.eta);
final EditText etn = (EditText) theLayout.findViewById(R.id.etn);
final EditText etb = (EditText) theLayout.findViewById(R.id.etb);
Calculate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Double a = new Double(eta.getText().toString());
Double n = new Double(etn.getText().toString());
Double ac = a*n;
tvac.setText(getResources().getString(R.string.tvresultados2) + " " + ac);
}
});
buttonb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
double b = new Double(etb.getText().toString());
double h = ac/b; //error: cannot resolve symbol 'ac'
tvh.setVisibility(View.VISIBLE);
tvh.setText("h = " + h);
}
});
The most simple way is to declare global variable. Declare your ac outside of onCreate scope instead of inside of it.
public Double ac; // global variable
#Override
public void onCreate(Bundle savedInstanceState){
Button Calculate = (Button) theLayout.findViewById(R.id.button);
Button buttonb = (Button) theLayout.findViewById(R.id.buttonb);
final TextView tvac = (TextView) theLayout.findViewById(R.id.tvac);
final TextView tvh = (TextView) theLayout.findViewById(R.id.tvh);
final EditText eta = (EditText) theLayout.findViewById(R.id.eta);
final EditText etn = (EditText) theLayout.findViewById(R.id.etn);
final EditText etb = (EditText) theLayout.findViewById(R.id.etb);
Calculate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Double a = new Double(eta.getText().toString());
Double n = new Double(etn.getText().toString());
ac = a*n;
tvac.setText(getResources().getString(R.string.tvresultados2) + " " + ac);
}
});
buttonb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
double b = new Double(etb.getText().toString());
double h = ac; //assign global variable into h
tvh.setVisibility(View.VISIBLE);
tvh.setText("h = " + h);
}
});
}
Can i write some code so that instead of writing a separate onClick listener i can group them all in single piece of code? Presently i have this. Now how can i avoid writing onclicklistener method for each.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DecimalFormat currencyFormatter = (DecimalFormat) NumberFormat.getInstance();
char decimalSeparator = currencyFormatter.getDecimalFormatSymbols().getDecimalSeparator();
mDecimalSeparator = Character.toString(decimalSeparator);
setContentView(R.layout.main);
mInputStack = new Stack<String>();
mOperationStack = new Stack<String>();
inputText = (TextView) findViewById(R.id.InputText);
inputText.setText("0");
resultText = (TextView) findViewById(R.id.ResultText);
resultText.setText("");
One = (Button) findViewById(R.id.button1);
Two = (Button) findViewById(R.id.button2);
Three = (Button) findViewById(R.id.button3);
Four = (Button) findViewById(R.id.button4);
Five = (Button) findViewById(R.id.button5);
Six = (Button) findViewById(R.id.button6);
Seven = (Button) findViewById(R.id.button7);
Eight = (Button) findViewById(R.id.button8);
Nine = (Button) findViewById(R.id.button9);
Zero = (Button) findViewById(R.id.button0);
Add = (Button) findViewById(R.id.buttonAdd);
Subtract = (Button) findViewById(R.id.buttonSubtract);
Multiply = (Button) findViewById(R.id.buttonMultiply);
Divide = (Button) findViewById(R.id.buttonDivide);
Delete = (Button) findViewById(R.id.buttonDel);
Period = (Button) findViewById(R.id.buttonPeriod);
Equal = (Button) findViewById(R.id.buttonEqual);
Sin = (Button) findViewById(R.id.buttonSin);
Cos = (Button) findViewById(R.id.buttonCos);
Tan = (Button) findViewById(R.id.buttonTan);
Cosec = (Button) findViewById(R.id.buttonCosec);
Sec = (Button) findViewById(R.id.buttonSec);
Cot = (Button) findViewById(R.id.buttonCot);
Sinh = (Button) findViewById(R.id.buttonSinh);
Cosh = (Button) findViewById(R.id.buttonCosh);
Tanh = (Button) findViewById(R.id.buttonTanh);
Factorial = (Button) findViewById(R.id.buttonFactorial);
Log = (Button) findViewById(R.id.buttonLog10);
Ln = (Button) findViewById(R.id.buttonLoge);
Reciprocal = (Button) findViewById(R.id.buttonReciprocal);
Square = (Button) findViewById(R.id.buttonSquare);
SquareRoot = (Button) findViewById(R.id.buttonSquareRoot);
Power = (Button) findViewById(R.id.buttonPower);
Percent = (Button) findViewById(R.id.buttonPercent);
One.setOnClickListener(this);
Two.setOnClickListener(this);
Three.setOnClickListener(this);
Four.setOnClickListener(this);
Five.setOnClickListener(this);
Six.setOnClickListener(this);
Seven.setOnClickListener(this);
Eight.setOnClickListener(this);
Nine.setOnClickListener(this);
Zero.setOnClickListener(this);
Add.setOnClickListener(this);
Subtract.setOnClickListener(this);
Divide.setOnClickListener(this);
Multiply.setOnClickListener(this);
Equal.setOnClickListener(this);
Delete.setOnClickListener(this);
Period.setOnClickListener(this);
Sin.setOnClickListener(this);
Cos.setOnClickListener(this);
Tan.setOnClickListener(this);
Cot.setOnClickListener(this);
Sec.setOnClickListener(this);
Cosec.setOnClickListener(this);
Sinh.setOnClickListener(this);
Cosh.setOnClickListener(this);
Tanh.setOnClickListener(this);
Percent.setOnClickListener(this);
Reciprocal.setOnClickListener(this);
Log.setOnClickListener(this);
Ln.setOnClickListener(this);
Power.setOnClickListener(this);
Square.setOnClickListener(this);
SquareRoot.setOnClickListener(this);
Factorial.setOnClickListener(this);
Sign.setOnClickListener(this);
You can set an android:click property in the xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="Go!"
android:onClick="goButtonClicked" android:id="#+id/goButton"></Button>
</LinearLayout>
SimplestButtonActivity.java
package com.botbook.simplestbutton;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class SimplestButtonActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void goButtonClicked(View v) {
tToast("Go button clicked!");
}
private void tToast(String s) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, s, duration);
toast.show();
}
}
You can set an android:click property in the xml. This will allow you to decide what function to call when the button is clicked in the xml. The function must take the same parameters as the normal onClick function and must be part of your activity, but can be named anything you want- Like onePressed, twoPressed, etc.
Try this
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
// do stuff;
break;
case R.id.button2:
// do stuff;
break;
case R.id.button3:
// do stuff;
break;
//....add here add buttons
default:
break;
}
}