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.
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 want to convert the selected text from Edit Text to Hyperlink. Basically i know how to convert text in hyperlink, but not able to do in Edit Text.
I have tried Linkify, but not working.
Any Help would be appreciated..
Here is the way how i solved the problem.
text = EditText
int start = text.getSelectionStart();
int end = text.getSelectionEnd();
SpannableStringBuilder spb = new SpannableStringBuilder(text
.getText().toString());
String hyper = text.getText().toString().substring(start, end);
text.setText(hyper);
text.setAutoLinkMask(Linkify.ALL); // Linkify.WEB_URLS
text.setText(spb);
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?
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();