I'm a new Android developer. As a starting project, I'm trying to create a basic addition calculator. I have an EditText which is supposed to take the input (input is a string) and convert it to int1 when Button1 is pressed. When Button2 is pressed, it is supposed to take the input, convert it to int2, add int1 and int2 together and store the result in the int ans, and set the text of the EditText to ans. However, when I try to use Integer.parseInt(et.getText().toString()) I get an error and the app force closes. Could anyone provide me with the code to properly convert these Strings to integers? Thank you.
static int fn = 0;
static int sn = 0;
static int ans = 0;
static int pro = 0;
//"+" Button Clicked//
if(pro == 0){
fn = Integer.parseInt(entry.getText().toString());
entry.setText("");
pro++;
}else{
//MessageBox Crap//
//"=" Button Clicked//
sn = Integer.parseInt(entry.getText().toString());
ans = fn + sn;
entry.setText(ans);
Shouldn't ans be converted to a string before you set the contents of the EditText?
Related
I want to create a calculator only to add the user digit inputs given in same edittext and show in second edittext. Here is an example.
52 is entered by user in a EditText.
i want to perform addition in these number and show the result in second edittext.
answer should be 5+2=7.
i don't now what to perform
so i am performing this task.
int ans = a+b;
final int[] oil={ans};
final String str = String.valueOf(R.id.editText1);
final int y = Integer.parseInt(str);
final int z = oil[y];
et2.setText(z);
This is what you are expecting i think
String val=et1.getText().toString();
char[] valarry =val.toCharArray();
int result= Integer.parseInt(String.valueOf(valarry[0]))+Integer.parseInt(String.valueOf(valarry[1]));
et2.setText(result);
I am using Double data type for a variable in my Android app.
It simply takes a number and shows whatever percentage increase from that number would be:
for example 1,000,000 plus 1000 % = 1.1E7
The problem is I don't want an exponent display (the E), I want it to be in decimal.
This is a code snippet of the area which when the user clicks a Calculate button the info is displayed in an editText (Textbox)
enter code here
Button calc2 = (Button)findViewById(R.id.button1);
calc2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText number1 = (EditText)findViewById(R.id.editText1);
EditText number2 = (EditText)findViewById(R.id.editText2);
EditText number3 = (EditText)findViewById(R.id.editText3);
double editText1 = Double.parseDouble(number1.getText().toString());
double Pluspercent = Double.parseDouble(number2.getText().toString());
double editText3 = Double.parseDouble(number3.getText().toString());
double Result1 = 0 ;
double Result2 = 0;
Result1 = Pluspercent * 0.01 ;
Result2 = editText1 * Result1;
editText3 = editText1 + Result2 ;
number3.setText(editText3 + "");
}
});
enter code here
The code works but displays with the E. Could you show me what code to use to simply get it to display the result as in decimal. The decimal result should be 11,000,000
No need to worry about commas at the moment.
Some languages use a Decimal data type which would take care of this problem I think. Anyone know why Android do not have this?
I looked at
http://lecturesnippets.com/android-variables-data-types/
which shows a list of the data types, but Double seems to be the biggest container and uses the Exponent thing I don't want.
Thanks for any help.
Al
You're looking for a DecimalFormat object. You want to pass your double into the DecimalFormat.format( ... ) method to get a StringBuffer, and then append the rest of the text you'd like to display to that StringBuffer before you pass it to your EditText.
You'll be particularly interested in this method:
public StringBuffer format (double value, StringBuffer buffer, FieldPosition position)
I tried to follow this question here String array incrementing each string
but it didn't want to work, What im trying to do is when the button is clicked increment what the TextToSpeech voice is going to say by incrementing the string. therefore start at string 0, then 1 to 2 to 3 to 4 ect and loop back around. heres the code
String Array Code
String [] speakLetters = { "Letter A for Ant", "Letter b for Bat", "Letter C for Cat" ....... , "Letter Z for zoo"};
The array is laid out just fine its just not working when trying to increment. it either says its just ANT for the first one and never increments or it freezes if I change the code.
Code for trying to increment the array
mNextBtn.setOnClickListener(new OnClickListener() {
int cIndex = 0;
int stringLength = speakLetters.length;
String speakNow = speakLetters[stringLength];
cIndex = (cIndex++); // I also tried here cIndex = (cIndex + 1) % stringLength;
tts.speak(speakNow, TextToSpeech.QUEUE_FLUSH, null);
mNextBtn.setEnabled(mSCanvas.isUndoable());
}
Yes I am writing this to an scanvas I wanted to include that just incase that could be the problem even though I doubt it is.
What am I doing wrong?
Declare cIndex variable as class member outside your click listener block:
int cIndex = 0;
and then modify click handler code:
mNextBtn.setOnClickListener(new OnClickListener() {
String speakNow = speakLetters[cIndex];
tts.speak(speakNow, TextToSpeech.QUEUE_FLUSH, null);
cIndex++;
cIndex %= speakLetters.length;
mNextBtn.setEnabled(mSCanvas.isUndoable());
});
I need to separate the input of an Edit Text on android, the input is in this format 4589, so I want to send the 45 to a list view, and the 89 to a Edit Text, somebody can help me I will appreciate it. thanks
The question is not clear. But you can try something like this
EditText et = (EditText) findViewById(R.id.editText);
String input = et.getText().toString();
String toEditText = input.substring(0,2); //45
String toListView = input.substring(2); //89
now you have the strings, use setText() to print
int a = Integer.parseInt(editText.getText());
int listNum = a / 100; //45
int editNum = a - listNum*100; //89
Here is a solution using integer division.
I'm trying to implement a copy/paste function. How can I get a selection of text from an EditText?
EditText et=(EditText)findViewById(R.id.title);
blabla onclicklistener on a button:
int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();
Then I'm stuck. Any ideas?
Seems like you've already done the hard part by finding what the selected area is. Now you just need to pull that substring out of the full text.
Try this:
String selectedText = et.getText().substring(startSelection, endSelection);
It's just a basic Java String operation.
You should use a special function from the Editable object:
Editable txt = et.getText();
txt.replace(int st, int en, CharSequence source)
This command replaces the part specified with (st..en) with the String (CharSequence).
you don't need to do all this, just long press on edit text it will show you all relevant options to Copy/Paste/Select etc. If you want to save the text use the method shown by mbaird
String selectedText = et.getText().toString().substring(startSelection, endSelection);
getText() returns an editable. substring needs a String. toString() connects them properly.
You can do it this way to get the selected text from EditText:
EditText editText = (EditText) findViewById(R.id.editText3);
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
final int selStart = editText.getSelectionStart();
final int selEnd = editText.getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
// here is your selected text
final CharSequence selectedText = editText.getText().subSequence(min, max);
String text = selectedText.toString();