For example, if user types "abc" in the EditText field, I just want to get the last character "c".
String last = yourEditText.getText().toString();
last = last.substring(last.length() - 1);
System.out.println("last character: " + last);
Expanding DonGru's post, with another alternative of getting the last character
EditText et;
et = (EditText) findViewById(R.id.et);
CharSequence s = et.getText();
System.out.println(s.subSequence(s.length()-1, s.length()));
since getText() returns a CharSequence you can also use that for getting the last character:
EditText my;
my = (EditText) findViewById(R.id.editText1);
CharSequence myCharSeq = my.getText();
System.out.println(myCharSeq.charAt(myCharSeq.length() - 1));
Related
I have one EditText and a TextView
and Buttons to perform addition and number Buttons, I want the values from the EditText and perform operations like add, multiply, divide, etc..., so I just need help with retrieving two values from single EditText, my friend suggested me to use split(), but I don't have an idea how to implement it.
How to get text from edittext?
initialize edittext:
EditText editText = findViewById(R.id.edittext);
String[] editTextValues = edittext.getText().toString().split(" ");
now use editTextValues like this
int firstValue = Integer.parse(editTextValues[0]);
int secondValue = Integer.parse(editTextValues[1]);
int sum = firstValue + secondValue;
Writing my first android app. Almost everything works except this function. Even when all the text boxes have some numeric value, the app crashes in this function. Also, is there a better way to retrieve all the values in these text boxes and convert it to int. Note that all the textboxes are input type number on the activity.xml file.
public int updateclks()
{
try{
EditText v1= (EditText) findViewById(R.id.editText1);
EditText v2 = (EditText) findViewById(R.id.EditText01);
EditText v3 = (EditText) findViewById(R.id.EditText02);
EditText v4 = (EditText) findViewById(R.id.EditText03);
EditText c1= (EditText) findViewById(R.id.EditText04);
EditText c2 = (EditText) findViewById(R.id.EditText05);
EditText c3 = (EditText) findViewById(R.id.EditText06);
EditText c4 = (EditText) findViewById(R.id.EditText07);
try{ //error occurs somewhere around this block
vsel[1]= Integer.parseInt(v1.getText().toString());
vsel[2]= Integer.parseInt(v2.getText().toString());
vsel[3]= Integer.parseInt(v3.getText().toString());
vsel[4]= Integer.parseInt(v4.getText().toString());
clk[1]= Integer.parseInt(c1.getText().toString());
clk[2]= Integer.parseInt(c2.getText().toString());
clk[3]= Integer.parseInt(c3.getText().toString());
clk[4]= Integer.parseInt(c4.getText().toString());
}
catch(NumberFormatException e)
{
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "Fields cannot be empty", duration);
toast.show();
return 1;
}
return 0;
}
parseInt() is not crashing your app.Because if any exception occurs for this parseInt it will be caught as you handled it.Since your application is still crashing see vse1 and clk1 array whether you created these correctly or not.
Also notice about array length if you created vse1 array of length 4.Then You may want to start with index 0 rather than 1.Otherwise ArrayIndexOutOfBounds will crash your application.
I also got the same problem and u can solve it by using trim function.
clk[1]= Integer.parseInt(c1.getText().toString().trim());
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.
In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.
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();