Android - get selection of text from EditText - android

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();

Related

How Do I Create a Calculator To Add User Inputs From Same EditText?

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);

Saving the text styling applied to text using setSpan()

HI everyone,
I have a bunch of text in an Edit text that I have set up to be styled(strike through only for the moment) using the setSpan method in Android. This seems to work fine.
The trouble I am having is that all the styling seem to get cancelled once I close that activity. That is when I load up the activity again , it just has plain text and none of the styling I had applied using the setSpan().
Note: All of my text get stored in a Database.
I have attached all the code for the styling, let me know if you need to see any more bits of code.
private void doStrick() {
int selectionStart = mBodyText.getSelectionStart();
styleStart = selectionStart;
int selectionEnd = mBodyText.getSelectionEnd();
// check for boo-boo's
if (selectionStart > selectionEnd){
int temp = selectionEnd;
selectionEnd = selectionStart;
selectionStart = temp;
}
Spannable str = mBodyText.getText();
str.setSpan(new StrikethroughSpan(),selectionStart, selectionEnd, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
Is that some bit of code I need to be adding to save the styling?
EDIT based on answer:
Calendar cal=Calendar.getInstance();
String date_time=String.format("%1$te %1$tB %1$tY,%1$tI:%1$tM:%1$tS %1$Tp",cal);
float Textsize = mBodyText.getTextSize();
String title = mTitleText.getText().toString();
String body = Html.toHtml(mBodyText.getText());
if (mRowId == null) {
long id = mDbHelper.createNote(title, body, date_time, Textsize);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateNote(mRowId, title, body, date_time, Textsize);
Log.d("MYTAG", "updateing note");
updateWidget();
And where I populate the fields:
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
mBodyText.setText(Html.fromHtml(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY))));
mBodyText = (EditText) findViewById(R.id.body);
float size = note.getFloat(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TEXT_SIZE));
mBodyText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
Is that some bit of code I need to be adding to save the styling?
Yes.
Presumably, right now, you are just saving thisIsYourEditText.getText().toString() to your database, then using thisIsYourEditText.setText(stringThatYouLoadBackOutOfYourDatabase) to populate the EditText.
Instead, you need to use Html.toHtml(thisIsYourEditText.getText()) to try to convert your styled text into HTML, and then use thisIsYourEditText.setText(Html.fromHtml(stringThatYouLoadBackOutOfYourDatabase)) to convert that HTML back into styled text.
Note that toHtml() and fromHtml() do not handle all possible CharacterStyles, nor are they guaranteed to do all of the styling correctly round-trip (i.e., the string generated by toHtml() may not match the string you started with before the fromHtml() call).

separate numeric input android

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.

How to get the last input only in EditText? (Android)

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));

Android - Getting User Input As An Integer

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?

Categories

Resources