Here is a code that i want to make a bmi application..
So i want to make the result display like this 20.30..
How to make the result into 2 decimals places only..
Hope anybody cant help me at this part....
package com.example.bmi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
EditText displayweight;
EditText displayheight;
EditText displayresult;
Button Calculate;
Button Reset;
double bmi=0;
double valueheight=0;
double valueweight=0;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
/*TextView tv = new TextView(this);
tv.setText("BMI Calculator");
setContentView(tv);*/
}
private void initControls() {
displayheight = (EditText)findViewById(R.id.displayheight);
displayweight = (EditText)findViewById(R.id.displayweight);
displayresult = (EditText)findViewById(R.id.displayresult);
Calculate = (Button)findViewById(R.id.calculate);
Reset = (Button)findViewById(R.id.reset);
Calculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
Reset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }
private void reset() {
displayheight.setText("");
displayweight.setText("");
displayresult.setText("");
}});
}
private void calculate() {
valueheight = Double.parseDouble(displayheight.getText().toString());
valueweight = Double.parseDouble(displayweight.getText().toString());
Double valueheightmeters;
valueheightmeters = valueheight / 100;
bmi = (valueweight / (valueheightmeters * valueheightmeters));
//txttipamount.setText(Double.toString(bmi));
if(bmi >= 30)
{
result = "Your BMI of " + Double.toString(bmi) + " is OBESE.";
displayresult.setText(result);
}
else if (bmi >= 25)
{
result = "Your BMI of " + Double.toString(bmi) + " is OVERWEIGHT.";
displayresult.setText(result);
}
else if (bmi >= 18.5)
{
result = "Your BMI of " + Double.toString(bmi) + " is IDEAL.";
displayresult.setText(result);
}
else
{
result = "Your BMI of " + Double.toString(bmi) + " is UNDERWEIGHT.";
displayresult.setText(result);
}
}
#Override
public void onClick(View arg0) {
switch (arg0.getId()){
case R.id.reset:
displayresult.setText("");
break;
default:
break;
}
}
}
this topic is discussed very often. Take a look at Round a double to 2 decimal places and see if it helps you.
Just let me know if you have any further questions.
Related
Showing database rows as ListView/Gridview in the same layout via adapter, is there a option to display those in separate pages like same layout. So I can switch to next page by a button.
Currently am just saving values using sharedprefernce and changes the contents from layout (Just TEXT in my case). I am trying to create app for exam tests with a question and 4 options. currently am saving user score and checked radio button id in sharedpreferences when user click NEXT button.
My code:
package com.sap.quizmaster;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private MyDatabase testdb;
private Cursor mycursor;
private TextView ques;
private RadioGroup options;
private RadioButton op1;
private RadioButton op2;
private RadioButton op3;
private RadioButton op4;
private RadioButton radioButton;
private LinearLayout default_lay;
private LinearLayout default_btn_lay;
private TextView final_text;
private Button final_btn;
private Button next_btn;
private Button prev_btn;
private int checkedid = -1;
private int q_no = 1;
private String answer;
private int score = 0;
private SharedPreferences sp;
private SharedPreferences.Editor sp_edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ques = findViewById(R.id.ques);
op1 = findViewById(R.id.op1);
op2 = findViewById(R.id.op2);
op3 = findViewById(R.id.op3);
op4 = findViewById(R.id.op4);
next_btn = findViewById(R.id.next_btn);
prev_btn = findViewById(R.id.prev_btn);
options = findViewById(R.id.options);
final_btn = findViewById(R.id.final_btn);
final_text = findViewById(R.id.final_text);
default_lay = findViewById(R.id.default_text);
default_btn_lay = findViewById(R.id.default_btn);
//Loading sharedpreferences
sp = getSharedPreferences("QuizMaster", MainActivity.MODE_PRIVATE);
//Clearing all values from sharedpreferences when app re-launching
sp_edit = sp.edit();
sp_edit.clear();
sp_edit.commit();
testdb = new MyDatabase(this);
mycursor = testdb.getQuestion(q_no);
setQuestion();
next_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mycursor.close();
checkedid = options.getCheckedRadioButtonId();
saveInteger(q_no, checkedid);
if (checkedid != -1) {
radioButton = findViewById(checkedid);
if (radioButton.getText().equals(answer)) {
score += 3;
} else {
score -= 1;
}
}
options.clearCheck();
q_no = q_no + 1;
if(q_no>3){
default_btn_lay.setVisibility(View.GONE);
default_lay.setVisibility(View.GONE);
final_btn.setVisibility(View.VISIBLE);
final_text.setVisibility(View.VISIBLE);
final_text.setText("Your Score is "+score);
q_no = 1;
return;
}
checkedid = sp.getInt(q_no + "", -1);
if (checkedid != -1) {
radioButton = findViewById(checkedid);
radioButton.setChecked(true);
}
mycursor = testdb.getQuestion(q_no);
setQuestion();
}
});
prev_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mycursor.close();
if (q_no <= 1) {
Toast.makeText(MainActivity.this, "END", Toast.LENGTH_SHORT).show();
return;
}
q_no = q_no - 1;
mycursor = testdb.getQuestion(q_no);
setQuestion();
checkedid = sp.getInt(q_no + "", -1);
if (checkedid != -1) {
radioButton = findViewById(checkedid);
radioButton.setChecked(true);
if (radioButton.getText().equals(answer)) {
score -= 3;
} else {
score += 1;
}
}
Toast.makeText(MainActivity.this, "Button PREVIOUS pressed", Toast.LENGTH_SHORT).show();
}
});
final_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final_text.setVisibility(View.GONE);
final_btn.setVisibility(View.GONE);
default_lay.setVisibility(View.VISIBLE);
default_btn_lay.setVisibility(View.VISIBLE);
mycursor = testdb.getQuestion(q_no);
setQuestion();
sp_edit = sp.edit();
sp_edit.clear();
sp_edit.commit();
score = 0;
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
testdb.close();
mycursor.close();
}
public void setQuestion() {
answer = mycursor.getString(mycursor.getColumnIndex("ANS"));
ques.setText(mycursor.getString(mycursor.getColumnIndex("QUES")));
op1.setText(mycursor.getString(mycursor.getColumnIndex("OP1")));
op2.setText(mycursor.getString(mycursor.getColumnIndex("OP2")));
op3.setText(mycursor.getString(mycursor.getColumnIndex("OP3")));
op4.setText(mycursor.getString(mycursor.getColumnIndex("OP4")));
}
public void saveInteger(int ques, int value) {
sp_edit = sp.edit();
sp_edit.putInt(ques + "", value);
sp_edit.apply();
}
}
Just get me an link or idea to think in any easy way.
Sounds like you want to use an AdapterViewFlipper
https://developer.android.com/reference/android/widget/AdapterViewFlipper
with a custom adapter
This will generate a view from a template using data from the adapter
e.g. Each question could be on a separate page with a button to move to the next question.
Example https://abhiandroid.com/ui/adapterviewflipper
But you probably don't want to use the autoflipping option and instead link a next button to the showNext() method
package com.example.android.mycalculator;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int qty = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String val = ((EditText)findViewById(R.id.input1)).getText().toString();
int num1 = Integer.valueOf(val);
String val1 = ((EditText)findViewById(R.id.input2)).getText().toString();
int num2 = Integer.valueOf(val1);
}
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/*for getting the input values into some variable**/
/* for adding the input numbers**/
public void add(View view){
qty = num1 + num2;
display(qty);
}
/*for subtracting two input numbs**/
public void subtract(View view){
qty = num1 - num2;
display(qty);
}
public void divide(View view){
qty = num1 / num2;
display(qty);
}
public void multiply(View view){
qty = num1 * num2;
display(qty);
}
}
The code got crashed when I ran it on my device through android studio.
I have a doubt in the code because I'm a beginner in coding. Pls find the error in it.The program takes two input numbers and performs actions like adding,subtracting,dividing and multiplying. It then shows the output as a TextView below.The program ran well in the computer but crashed on the device. No errors were shown while running.
String val = ((EditText)findViewById(R.id.input1)).getText().toString();
you have to move to onCreate
Welcome to StackOverFlow.com!
I suggest that every time that you make a question if you have any exception try to post the messages displayed in your LogCat:
http://developer.android.com/tools/debugging/debugging-log.html
a brief reading to the basics must be required =P
add a method to set the values defined in your EditTexts setValues()
this code will do the job for a calculator in android based on your code:
public class MainActivity extends ActionBarActivity {
/*for getting the input values into some variable**/
String val;
private int num1;
String val1;
private int num2;
int qty = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
public void setValues(){
try{
val = ((EditText)findViewById(R.id.input1)).getText().toString();
num1 = Integer.valueOf(val);
val1 = ((EditText)findViewById(R.id.input2)).getText().toString();
num2 = Integer.valueOf(val1);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Please check your values!", Toast.LENGTH_LONG).show();
}
}
/* for adding the input numbers**/
public void add(View view){
setValues();
qty = num1 + num2;
display(qty);
}
/*for subtracting two input numbs**/
public void substract(View view){
setValues();
qty = num1 - num2;
display(qty);
}
public void divide(View view){
setValues();
qty = num1 / num2;
display(qty);
}
public void multiply(View view){
setValues();
qty = num1 * num2;
display(qty);
}
}
I am trying to make a simple tip calculator and have an EditText text field where you put in the amount of your total bill, then click on the percent button you want to calculate in tip (say, 10%) and then the onClickListener for that % button will calculate the tip and put the result into a different TextView field below itself.
However, I know I had to change my EditText for my total bill from a string to a double, and included code for that, but I still get the error when I try to multiply the total bill times the tip of 10%. Why?
Here is my code:
package com.example.nonitips;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public EditText totalBill;
public double billWithTip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalBill = (EditText)findViewById(R.id.totalBill);
}
public void onTen (View v) {
Toast.makeText(this, "Tipping at 10%", Toast.LENGTH_SHORT).show();
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
bill = (totalBill * 0.10) + totalBill; //Error on this line.
}
});
}
public void onFifteen (View v) {
Toast.makeText(this, "Tipping at 15%", Toast.LENGTH_SHORT).show();
}
public void onTwenty (View v) {
Toast.makeText(this, "Tipping at 20%", Toast.LENGTH_SHORT).show();
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Change your code of EditText click:
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
billWithTip= (bill * 0.10) + billWithTip;
Toast.makeText(this, "Updated billWithTip"+billWithTip, Toast.LENGTH_SHORT).show();
billWithTip.setText(String.valueOf(billWithTip));
}
});
Double to String convertion Example:
double total = 44;
String total2 = String.valueOf(total);
You are trying to multiple EditText with double value. Use following code.
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
billWithTip= (bill * 0.10) + billWithTip;
}
});
try this one..
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// double bill = Double.parseDouble(totalBill.getText().toString());
// bill = (totalBill * 0.10) + totalBill; //Error on this line.
try {
String bills= totalBill.getText().toString();
Double protein = Double.parseDouble(bills); // Make use of autoboxing.It's also easier to read.
} catch (NumberFormatException e) {
// p did not contain a valid double
}
if(protein == null || protein.isEmpty()) {
bill = 0.0;
} else {
bills= Double.parseDouble(noKids.getText().toString());
Double protein = Double.parseDouble(bills);
bill = (protein * 0.10) + protein ;
}
}
});
thank you..
try this
totalBill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double bill = Double.parseDouble(totalBill.getText().toString());
bill = (bill * 0.10) + bill );
}
});
I have a problem where I want to make the comparison the data in button
package lynn.calculate.KaunterKalori;
import lynn.calculate.KaunterKalori.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.EditText;
public class CalculateAll extends Activity implements OnClickListener {
protected int totalKalori1;
protected int totalKalori2;
protected int totalKalori3;
EditText EstimateCalorie;
TextView TotalKaloriSehari;
TextView totalsarapan;
TextView totallunch;
TextView totaldinner;
TextView calorieneeds;
TextView resultDiff;
public CalculateAll() {
}
#Override
public void onCreate(Bundle savedInstancesState) {
super.onCreate(savedInstancesState);
setContentView(R.layout.calculate_all);
View kiraButton = findViewById(R.id.buttonKiraAll);
kiraButton.setOnClickListener(this);
View bezaButton = findViewById(R.id.caloriediff);
bezaButton.setOnClickListener(this);
TotalKaloriSehari = (TextView) findViewById(R.id.JumlahKalori);
totalsarapan = (TextView) findViewById(R.id.sarapantext);
totallunch = (TextView) findViewById(R.id.lunchtext);
totaldinner = (TextView) findViewById(R.id.dinnertext);
EstimateCalorie = (EditText) findViewById(R.id.BMR);
// calorieneeds = (TextView) findViewById(R.id.BMR);
resultDiff = (TextView) findViewById(R.id.result);
Bundle extras = getIntent().getExtras();
if (extras != null) {
totalKalori1 = extras.getInt("totalBreakfast");
totalKalori2 = extras.getInt("totalLunch");
totalKalori3 = extras.getInt("totalDinner");
}
totalsarapan.setText(totalKalori1 + "");
totallunch.setText(totalKalori2 + "");
totaldinner.setText(totalKalori3 + "");
}
public void onClick(View v) {
if (v.getId() == R.id.buttonKiraAll) {
int TotalKalori = calculateTotalKalori(totalKalori1, totalKalori2,
totalKalori3);
TotalKaloriSehari.setText(TotalKalori + "");
}
else if (v.getId()== R.id.caloriediff) {
int nilaikalori = Integer.parseInt(EstimateCalorie.getText()
.toString());
int bmr = calculatebmr(nilaikalori);
String deskripsiKalori = describekalori(bmr);
resultDiff.setText("kalori makanan" + TotalKalori + " calori diperlukan " + bmr + "=" + deskripsiKalori);
// --> this TotalKalori also can't be used
Intent n = new Intent(this, MainActivity.class);
startActivity(n);
}
}
public int calculateTotalKalori(int totalKalori1, int totalKalori2,
int totalKalori3) {
return (int) (totalKalori1 + totalKalori2 + totalKalori3);
}
public int calculatebmr(int nilaikalori) {
return (int) (nilaikalori);
}
private String describekalori(int bmr) {
if (bmr < TotalKalori ) { //<-- this TotalKalori can't be read/used
return "bykkan makan";
} else if (bmr == TotalKalori) {
return "kekalkan jumlah kalori ini";
} else if (bmr > TotalKalori) {
return "kurangkan pengambilan kalori";
} else
return "oiii";
}
}
The first button, buttonKiraAll read int totalKalori, then i want to use the totalKalori data to be compare with 2nd data button calorieDiff, how i want to make the totalKalori can be read by the second button? so i can make the comparison
It's a scoping problem, you are declaring int TotalKalori locally in the onClick event.
Declare it like you have done with totalKalori1 and it should work
Or you can use getTag(); setTag(); to store the variables on the buttons...
Use setTag to the data you want
myButton.setTag("fun Stuff");
Then on the listener just get the tag (you will need to do a type cast):
OnClickListener myListner = new OnClickListener() {
public void onClick(View view) {
doSomethingAwesome(view, (String) view.getTag());
}
};
Declare int TotalKalori only once globally (in the section you declared protected int totalKalori1; protected int totalKalori2;... ) and simply use it for both buttons like: in button1 TotalKalori = calculateTotalKalori(totalKalori1, totalKalori2,totalKalori3); this value is global so button2 already can acess it by simply using TotalKalori where you need and also the comparison portion.
I'm trying to teach myself how to write android apps and I'm having trouble registering a button click and taking actions based on which radio button is selected at the time. This is a simple tip calculator:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.view.View;
public class TipCalc extends Activity implements RadioGroup.OnCheckedChangeListener,View.OnClickListener
{
TextView result;
RadioGroup radiogroup1;
RadioButton r1,r2,r3;
Button calculate;
EditText bill, resulttotal;
private int radioCheckedId = -1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radiogroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
Button calculate = (Button) findViewById(R.id.calculate);
RadioButton r1 = (RadioButton) findViewById(R.id.poor);
RadioButton r2 = (RadioButton) findViewById(R.id.average);
RadioButton r3 = (RadioButton) findViewById(R.id.excellent);
EditText bill = new EditText(this);
EditText resulttotal = new EditText(this);
radiogroup1.setOnCheckedChangeListener(this);
calculate.setOnClickListener(this);
//bill.setText("0");
//resulttotal.setText("0");
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioCheckedId = checkedId;
}
public void onClick(View v)
{
if (v == calculate)
{
String billtotal;
double total = 0;
billtotal = bill.getText().toString();
final int aInt = Integer.parseInt(billtotal);
if (radioCheckedId == 1)
{
total = aInt * 1.1;
final String aString = Double.toString(total);
resulttotal.setText(aString);
}
if (radioCheckedId == 2)
{
total = aInt * 1.15;
final String aString = Double.toString(total);
resulttotal.setText(aString);
}
if (radioCheckedId == 3)
{
total = aInt * 1.2;
final String aString = Double.toString(total);
resulttotal.setText(aString);
}
}
}
}
Everything loads just fine, but nothing happens when I press the calculate button in the virtual phone.
The problem is where you're comparing the RadioGroup's selected id... you'll want to change your onClick() to:
public void onClick(View v) {
if (v == calculate) {
String billtotal;
double total = 0;
billtotal = bill.getText().toString();
final int aInt = Integer.parseInt(billtotal);
if (radioCheckedId == R.id.poor) {
total = aInt * 1.1;
final String aString = Double.toString(total);
resulttotal.setText(aString);
}
if (radioCheckedId == R.id.average) {
total = aInt * 1.15;
final String aString = Double.toString(total);
resulttotal.setText(aString);
}
if (radioCheckedId == R.id.excellent) {
total = aInt * 1.2;
final String aString = Double.toString(total);
resulttotal.setText(aString);
}
}
}
onCheckedChanged() gives you will be the R.id for the view and not just a number which tells you which it is in sequence.
A few quick (unrelated) suggestions:
Use a switch statement instead of a bunch of if-statements.
Put something in there to check for -1 (nothing checked) too... just to be sure.
In the onClick() I usually check for which View was clicked by checking the incoming view's id. This just makes it where you don't have to keep everything stored and (IMHO) is a little more clear what you're talking about.
The above suggestions would look something like:
public void onClick(View v) {
if (v.getId() == R.id.calculate) {
String billtotal;
double total = 0;
billtotal = bill.getText().toString();
final int aInt = Integer.parseInt(billtotal);
switch(radioCheckedId) {
case R.id.poor:
total = aInt * 1.1;
final String aString = Double.toString(total);
resulttotal.setText(aString);
break;
case R.id.average:
total = aInt * 1.15;
final String aString = Double.toString(total);
resulttotal.setText(aString);
break;
case R.id.excellent:
total = aInt * 1.2;
final String aString = Double.toString(total);
resulttotal.setText(aString);
break;
default:
// do something for when nothing is selected... maybe throw an error?
break;
}
}
}
Lastly, if all you're doing in onCheckedChanged() is storing the value you could get rid of it all together and just check for it in the onClick(). Something like:
public void onClick(View v) {
int radioCheckedId = radiogroup1.getCheckedRadioButtonId();
if (v == calculate) {
// ...
Unrelated, but another problem I noticed (and someone else mentioned)... if your EditTexts are listed in the XML layout then you'd need to get hooks to them like this (and not create new ones):
EditText bill = (EditText) findViewById(R.id.bill );
EditText resulttotal = (EditText) findViewById(R.id.resulttotal);
Also, you could probably just use a TextView instead of an EditView for the result if yo udon't need it to be editable.
import java.text.NumberFormat;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.view.View;
public class TipCalc extends Activity
{
TextView result;
RadioGroup radiogroup1;
RadioButton r1,r2,r3;
Button calculate;
EditText bill, resulttotal;
Locale currentLocale = Locale.getDefault();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radiogroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
final Button calculate = (Button) findViewById(R.id.calculate);
final RadioButton r1 = (RadioButton) findViewById(R.id.poor);
final RadioButton r2 = (RadioButton) findViewById(R.id.average);
final RadioButton r3 = (RadioButton) findViewById(R.id.excellent);
final EditText bill = (EditText) findViewById(R.id.bill);
final EditText tiptotal = (EditText) findViewById(R.id.tiptotal);
final EditText resulttotal = (EditText) findViewById(R.id.resulttotal);
bill.setText("0.00");
tiptotal.setText("0.00");
resulttotal.setText("0.00");
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) throws NumberFormatException {
if (v == calculate)
{
NumberFormat currencyFormatter;
currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
double atotal = 0;
double btotal = 0;
String billtotal = bill.getText().toString();
Double aDbl = 0.00;
try
{
aDbl = Double.parseDouble(billtotal);
}
catch(NumberFormatException n)
{
aDbl = 0.00;
}
if (r1.isChecked())
{
atotal = aDbl * 1.1;
btotal = aDbl * 0.1;
}
if (r2.isChecked())
{
atotal = aDbl * 1.15;
btotal = aDbl * 0.15;
}
if (r3.isChecked())
{
atotal = aDbl * 1.2;
btotal = aDbl * 0.2;
}
final String bString = currencyFormatter.format(btotal);
tiptotal.setText(bString);
final String aString = currencyFormatter.format(atotal);
resulttotal.setText(aString);
}
}
});
}
}
I have some similar problem. I have a countdown in a radio group activity.
When user clicks the next Button the radio group is checked to see if an option is selected.
I implemented the button pressed at the end of the countdown, now i need to pass a checked radio Id to bypass the default user message of an option not selected.
case R.id.next:
Log.d(" ID BOTAO",((java.lang.String) String).valueOf(rGroup3.getCheckedRadioButtonId()));
if(rGroup3.getCheckedRadioButtonId()==-1){
Context context = getApplicationContext();
CharSequence text = "Please, select an option!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
break;
}
Your problem is that you never add EditText instances to the current layout.
You should add them as children of the main layout.