Advertisement

New MenuItem for EditText selection

New MenuItem for EditText selection This video shows how to add new MenuItem 'Capitals' for the selection in an EditText field, and how to convert the selected text toUpperCase, in Sketchware android project.

In onCreate use following code:
edittext1.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.add(0,0,0,"Capitals");
return true;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case 0:
String text = edittext1.getText().toString();
int min = 0;
int max = edittext1.getText().length();
if (edittext1.isFocused()) {
final int selStart = edittext1.getSelectionStart();
final int selEnd = edittext1.getSelectionEnd();

min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
final String selectedText = text.subSequence(min, max).toString();
edittext1.setText("");
edittext1.append(text.subSequence(0, min)+selectedText.toUpperCase()+ text.subSequence(max, text.length()));
edittext1.setSelection(max);
return true;
default:
break;
}
return false;
}
});

selection

Post a Comment

0 Comments