i have 2 edit Text in my application, 1 button to add the input numbers in the edit Text and 1 text view to display the result. I would like to put a toast message if my edit text tab is empty or null when i click the button. I have searched and tried all the solutions..and nothing seems to work. help please!!
here is my code for adding the two numbers.
MainActivity.java
package com.example.mycalc;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.editText1);
final EditText editText2 = (EditText)findViewById(R.id.editText2);
Button button1 = (Button)findViewById(R.id.button1);
final TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(" ");
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
double edit1 = Double.valueOf(editText1.getText().toString());
double edit2 = Double.valueOf(editText2.getText().toString());
double text = edit1 + edit2;
textView1.setText(String.valueOf(text));
}
});
}
#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);
}
}
when user clicks button check:
if (editText1.getText().toString().trim().length() <= 0) {
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
trim it to avoid blank spaces.
To create and show a Toast use this code:
Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
In order to work properly, you should replace this code:
if (editText1.equals("")) {
editText1.setError("please input number");
}
if (editText2.equals("")) {
editText2.setError("please input number");
}
with this:
if (editText1.getText().toString().length() == 0 || editText1.getText().toString().length() == 1) {
Toast.makeText(this, "Please input number", Toast.LENGTH_LONG).show();
}
You can try this two function for string empty, not empty or null. It is simple return true or false. It is very use for all projects.
if(isEmpty(edittext.getText().toString())){
// your Toast message if string is empty
}
if(isNotEmpty(edittext.getText().toString())){
// your Toast message if string is not empty
}
public static boolean isEmpty(String str) {
if (str == null)
return true;
else if (str.toString().trim().length() == 0)
return true;
return false;
}
public static boolean isNotEmpty(String str) {
if (str == null)
return false;
else if (str.toString().trim().length() == 0)
return false;
return true;
}
Add to your click listener check
Toast toast = Toast.makeText(MainActivity.this, "Your Message", Toast.LENGTH_SHORT);
toast.show();
Your code will be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.editText1);
final EditText editText2 = (EditText)findViewById(R.id.editText2);
Button button1 = (Button)findViewById(R.id.button1);
final TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(" ");
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(editText1.getText().toString().trim().length() == 0 || editText2.getText().toString().trim().length() == 0) {
Toast.makeText(this,"Edittext is null",Toast.LENGTH_SHORT).show();
}
else {
double edit1 = Double.valueOf(editText1.getText().toString());
double edit2 = Double.valueOf(editText2.getText().toString());
double text = edit1 + edit2;
textView1.setText(String.valueOf(text));
}
});
}
}
Related
Good day All
I am struggling with this problem, I am not sure if I'm missing something obvious but the case statement/sorting method fails to return the correct result.
In short, the program recieves an input from a EditText (edtInput) and assigns it to
string temp in isValidNumeericSequence(string temp) {...}
where it gets tested for
1. if temp is empty
2. first character is a numeric value
3. the string contains anything except numbers and/or operators (operators specified in arrOperators)
The result from toast always refers to error_message 2, no matter what characters in the string, it always stays by error_message 2
IF I change the (temp == "" ) to (temp != ""), then it changes and sticks to error_message 1...
this might not be the most optimal method of calculating a equation, but its a simple start for me
p.s. If anything seems/looks abnormal, or irregular use of functions, please point them out, I would appreciate it...
I have tried debugging, but I seem to be missing something.
java file:
package com.cyberstudios.interpreter.interpreter;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static android.widget.Toast.*;
public class MainForm extends Activity {
private TextView lblAnswer, showText;
private EditText edtInput;
public Button btnShowHint, btnCalculate, btnClose;
public String [] arrEditStore;
public String arrOperators [] = {"+", "-", "*", "/", "(", ")"} ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_form);
edtInput = (EditText) findViewById(R.id.edtInput) ;
lblAnswer = (TextView) findViewById(R.id.lblAnswer) ;
btnShowHint = (Button) findViewById(R.id.btnHelp);
btnCalculate = (Button) findViewById(R.id.btnShow) ;
}
public void calculate(View vw)
{
String temp = edtInput.toString(), error_message = "";
//--------------------------------------ERROR HANDLER----------------------------------------------------------
switch (isValidNumericSequence(temp)) {
case 0:
sortInput(temp);
break;
case 1:
error_message = "Dumb Shit, enter a sum, want me to read your mind?" ;
break;
case 2:
error_message = "Think I'm an idiot, I need a number to start with, not a sign or a letter";
break;
case 3:
error_message = "Moron!!! Are we doing word puzzles or character drawing, enter numbers and operators, not letters and punctuation, its not English class!" ;
break;
}
makeText(this, error_message, LENGTH_SHORT).show();
//-------------------------------------------------------------------------------------------------------------
arrEditStore = new String[edtInput.length()] ;
lblAnswer.setText("No Answer");
// applyBODMAS();
/* double answer = CalcFromArray();
lblAnswer.setText(String.valueOf(answer));*/
}
/* private double CalcFromArray() {
return 0;
}*/
/*private void applyBODMAS()
{
for (int k = 1; k <= arrEditStore.length; k++)
{
arrEditStore[k] = "0";
}
}*/
private int isValidNumericSequence(String temp)//checks if string contains any digits
{
// Works with error codes, error code will be updated to be more user friendly
// by reporting the position of errors:
// Code 0 : No error
// Code 1 : Empty String
// Code 2 : First character must be of numeric type
// Code 3 : There is an invalid character in the sequence
makeText(this, "this is edtInput text - " + edtInput.toString(), LENGTH_SHORT).show(); ;
makeText(this, "this is temp text - " + temp, LENGTH_SHORT).show(); ;
if (temp == "") {
return 1;
}else {
if (!Character.isDigit(temp.charAt(0)))
return 2;
for (int x = 1; x <= temp.length(); x++) {
for (int k = 1; k <= arrOperators.length; k++) {
if (temp.charAt(x) != arrOperators.toString().charAt(k) || temp.charAt(x) >= 9 || temp.charAt(x) <= 0)
return 3;
}
}
}
return 0 ;
}
private void sortInput(String temp)
{
int x,y, count = 0;
for (x=1; x<temp.length()+1; x=x+1)
{
for (y=0;y<= arrOperators.length;y=y+1)
{
if(temp.substring(x,x+1).equals(arrOperators[y]))
{
arrEditStore[count++] = temp.substring(0, x-1);
arrEditStore[count++] = arrOperators[y] ;
}
}
}
}
public void _showHint(View vw)
{
final Dialog showHintDialog = new Dialog(this);
showHintDialog.setContentView(R.layout.custom_dialog);
showHintDialog.setTitle("How to enter data");
showHintDialog.show();
btnClose = (Button) showHintDialog.findViewById(R.id.button) ;
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
showHintDialog.dismiss();
}
});
}
#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_form, 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);
}
}
I dont believe the logcat or any xml files are nessassary, but if it needs be, I can post them
Thanx in advance
Thank you too #oguzhand and #Chris Stratton for the comments
edtInput.toString() is totally wrong!!! you need to use
edtInput.getText().toString() You are using edittext totally wrong...
Read this:
developer.android.com/reference/android/widget/EditText.html –
oguzhand
and
Indeed #oguzhand seems to have identified a primary issue, you are
using what is basically a "handy name" of the editText object rather
than the contents of its text field. – Chris Stratton
I simply replaced any edtInput.toString() with edtInput.getText().toString() and it worked perfectly
Thank you
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 hope this isn't a stupid question. I'm having some trouble clearing a TextView. I've looked around and everyone keeps saying use: textView.setText(""); in onCreate but doesn't seem to work for some reason. Basically, my app just accepts a number from an editText then runs the Fibonacci sequence (when a button is clicked) and displays the result in a textView. Well, the sequence displays fine but I want the textview to clear every time I click the button - so far it just keeps adding more text to what's already there.
Am I placing textView.setText(""); in the wrong location? Or am I just missing some other concept? (I also tried placing it from my OnClick - that didn't work either).
Here is my code:
public class MainActivity extends Activity {
// primary widgets
private EditText editText;
private TextView textView;
private Button button1;
static ArrayList<Integer> fibList = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
//Attempt to clear TextView
textView.setText("");
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String input = editText.getText().toString();
int number = Integer.parseInt(input);
int tmp = 0;
// confirm input
if (number < 20) {
Toast.makeText(getApplicationContext(),
"You entered: " + number, Toast.LENGTH_LONG).show();
for (int i = 0; i <= number; i++) {
fibList.add(fib(i));
// sum even numbers
if (fib(i) % 2 == 0) {
tmp += fib(i);
}
}
} else {
Toast.makeText(getApplicationContext(),
"Number is too Large: " + number, Toast.LENGTH_LONG)
.show();
}
String array = fibList.toString();
textView.setText(array);
}
});
}
// run fibonacci sequence
public static int fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
#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;
}
}
If you want the TextView to clear on each button click then the .setText must go in you onClick. The reason you would put the .setText in your onCreate is to clear the text as soon as your activity is created, but you do not have anything to clear just yet since your button has not yet been pushed so setText will do nothing. Also, since your onCreate will only run once for your activity, it will never go back to the setText again. Try the following:
public class MainActivity extends Activity {
// primary widgets
private EditText editText;
private TextView textView;
private Button button1;
static ArrayList<Integer> fibList = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(""); //Clear the TextView
fibList.clear(); //Clear your array list before adding new elements
String input = editText.getText().toString();
int number = Integer.parseInt(input);
int tmp = 0;
// confirm input
if (number < 20) {
Toast.makeText(getApplicationContext(),
"You entered: " + number, Toast.LENGTH_LONG).show();
for (int i = 0; i <= number; i++) {
fibList.add(fib(i));
// sum even numbers
if (fib(i) % 2 == 0) {
tmp += fib(i);
}
}
} else {
Toast.makeText(getApplicationContext(),
"Number is too Large: " + number, Toast.LENGTH_LONG)
.show();
}
String array = fibList.toString();
textView.setText(array);
}
});
}
// run fibonacci sequence
public static int fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
#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;
}
}
you will need to clear textView on Button click event before adding new results to it.do it as:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(""); //<<<<<< clear TextView on Button Click
.....
The problem is more likely in fibList that is not being cleared
how do i hide the context of the text field and show it when i click
the button
i do not want to hide the whole text filed just the context i wrote inside it
and show ot when i click a button
*
this is a small code*
package com.example.nonachan;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
char a;
char b;
char c;
int i = 0;
char buf;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText t =(EditText)findViewById(R.id.t1);
ImageButton n = (ImageButton)findViewById(R.id.b1);
n.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
a = 'a';
t.setText(t.getText().toString() + a);
// t.setVisibility(View.INVISIBLE);
}
});
ImageButton a = (ImageButton)findViewById(R.id.b2);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
b = 'b' ;
t.setText(t.getText().toString() + b);
// t.setVisibility(View.INVISIBLE);
i++;
}
});
ImageButton m = (ImageButton)findViewById(R.id.b4);
m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
c = 'c' ;
t.setText(t.getText().toString() + c);
//t.setVisibility(View.INVISIBLE);
i++;
}
});
Button l = (Button)findViewById(R.id.b3);
l.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// t.setVisibility(View.VISIBLE);
}
});
}
#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;
}
}
Assuming you have misspelled contents as context in your question (looks very likely)
instead of trying to hide the contents of the EditText, just save it to a variable and set the text of the EditText to empty. Then in your button click just set the text back to the contents in your local variable
Eg
String hiddenText = null;
EditText text = (EditText)findViewById(R.id.t1);
ImageButton hide = (ImageButton)findViewById(R.id.b1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// save and hide
hiddenText = text.getText();
text.setText("");
}
});
ImageButton unhide = (ImageButton)findViewById(R.id.b2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// unhide the text and 'clear' hiddenText
if (hiddenText != null) {
text.setText(hiddenText);
hiddenText = null;
}
}
});
There are several ways of doing it. It depends how you want to do it. These are just some suggestions off the top of my head, you can even use them all together.
You can put any entered text in a separate string and then if invisible set the TextView to show an empty string, if visible set the TextView to the saved string.
You can use the visibility tag.
You can set the text color to the background color.
You can create/remove the TextView on visible/invisible states.
You can replace the text with symbols for secure inputs if you want (for each character in edittext display an asterisk or something) and then change that back to plain text.
There are a hundred and one ways of doing it, if you search around on previously answered questions you will see several different methods.
I have a project for an Android class, so I'm still learning and this should be a basic question. We were given a tip calculator and already made some modifications, now we have to add a menu.
When it starts up, it will be in multi-person mode. Gives a text box and Text Field for how many people you want the bill split into. When you hit menu, it should show a Single person mode which eliminates a text box and text field. The menu then changes to show a multi-person mode button in the menu.
I've got everything to work except it's showing both buttons, I cannot figure out how to hide a button temporarily. The main error is:
Cannot invoke setVisibility(int) on the primitive type int
on the statement:
multiple_button.setVisibility(View.GONE);
I've tried every combination of hiding the button I can think of, and think that the above line is correct, but unsure of how make it work.
one_person_button = View.VISIBLE;
multiple_button = View.GONE;
I have this in the code, but it's not doing anything either.
Any help would be greatly appreciated.
edit: code. I've read through the link, but considering I don't have a OnPrepareOptions section, I need to re-read it
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class tipcalc extends Activity
{
public static int one_person_button = Menu.FIRST;
private int multiple_button = Menu.FIRST +1;
static final private int reset_button = Menu.FIRST +2;
private static final int MENU_ITEM = 0;
private EditText txtbillamount;
private EditText txtpeople;
private EditText txtpercentage;
private TextView txtperperson;
private TextView txttipamount;
private TextView txttotal;
private Button btncalculate;
private Button btnreset;
private double billamount = 0;
private double percentage = 0;
private double numofpeople=0;
private double tipamount = 0;
private double totaltopay = 0;
private double perperson = 0;
private View view;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem itemOne = menu.add(0, one_person_button, Menu.NONE,
R.string.one_person);
MenuItem itemMultiple = menu.add(1, multiple_button, Menu.NONE,
R.string.multiple);
MenuItem itemReset = menu.add(2, reset_button, Menu.NONE,
R.string.reset);
itemOne.setIcon(R.drawable.ic_menu_invite);
itemMultiple.setIcon(R.drawable.ic_menu_allfriends);
itemReset.setIcon(R.drawable.ic_menu_refresh);
one_person_button.setGroupVisible(0, true);
multiple_button.setVisibility(View.GONE);
one_person_button = View.VISIBLE;
multiple_button = View.GONE;
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (one_person_button == View.VISIBLE) {
((TextView)findViewById(R.id.txtpeople)).setVisibility(View.INVISIBLE) ;
((TextView)findViewById(R.id.widget30)).setVisibility(View.INVISIBLE) ;
multiple_button = View.VISIBLE;
one_person_button = View.GONE;
numofpeople = 1; }
else if (multiple_button == View.VISIBLE) {
((TextView)findViewById(R.id.txtpeople)).setVisibility(View.VISIBLE) ;
((TextView)findViewById(R.id.widget30)).setVisibility(View.VISIBLE) ;
multiple_button = View.GONE;
one_person_button = View.VISIBLE;
}
return false;
}
private void initControls()
{
txtbillamount = (EditText)findViewById(R.id.txtbillamount);
txtpeople = (EditText)findViewById(R.id.txtpeople);
txtperperson=(TextView)findViewById(R.id.txtperperson);
txttipamount=(TextView)findViewById(R.id.txttipamount);
txttotal=(TextView)findViewById(R.id.txttotal);
btncalculate = (Button)findViewById(R.id.btncalculate);
btnreset = (Button)findViewById(R.id.btnreset);
btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }});
}
private void calculate()
{
billamount=Double.parseDouble(txtbillamount.getText().toString());
numofpeople=Double.parseDouble(txtpeople.getText().toString());
RadioButton poorButton = (RadioButton) findViewById(R.id.radioButton1);
RadioButton goodButton = (RadioButton) findViewById(R.id.radioButton2);
RadioButton excellentButton = (RadioButton) findViewById(R.id.radioButton3);
if (poorButton.isChecked()){
percentage = Double.parseDouble(poorButton.getText().toString());
} else if (goodButton.isChecked()){
percentage = Double.parseDouble(goodButton.getText().toString());
} else if (excellentButton.isChecked()){
percentage = Double.parseDouble(excellentButton.getText().toString());
}
tipamount=(billamount*percentage)/100;
totaltopay=billamount+tipamount;
perperson=totaltopay/numofpeople;
txttipamount.setText(Double.toString(tipamount));
txttotal.setText(Double.toString(totaltopay));
txtperperson.setText(Double.toString(perperson));
}
private void reset()
{
txtbillamount.setText("");
txtpeople.setText("");
txtperperson.setText("");
txttipamount.setText("");
txttotal.setText("");
}
}
Post all of your relavent source code. Without it, we cannot give you specific advice about what is going wrong.
I can tell you though you'll be needing to override onPrepareOptionsMenu() and inside there you'll want to check which mode your in and make the proper button be visible. But you need to call setVisibility(View.VISIBLE); on a reference to the button widget, not on an int.
This page holds the answer to your questions.
try calling setVisibility with 8