Reputation: 5126
I'm trying to enable my users to rename a file using the app, my problem is more about the design. I want that when renaming, the EditText will include the old name, and it will be selected, not including the file extension.
I've managed to do that but my problem is that even though the text is selected, the keyboard, and the cursor on the text, are not showing. This makes the user click on the editText to rename it, which cancels the selection, so this is why it really bothers me.
Image for reference:
My EditText xml (ignore the visibility attribute):
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/renameEditText"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_marginBottom="8dp"
android:paddingLeft="20dp"
android:visibility="gone"
android:focusable="true"/>
My Code for setting selection:
renameEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String text = renameEdit.getText().toString();
if (!text.isEmpty()) {
int index = text.lastIndexOf('.');
if (index == -1)
renameEdit.selectAll();
else
renameEdit.setSelection(0, index);
}
}
}
});
Any advices?
Upvotes: 2
Views: 1015
Reputation: 41
There are several ways to open up the soft keyboard programmatically.
Even a few ways of doing it declaratively in the manifest; however, that requires opening the keyboard on activity start.
Try this...
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Other possible answers are here - Open soft keyboard programmatically
Upvotes: 4
Reputation: 17851
Once you have set the selectAll()
or setSelection()
, you could just pop up the keyboard.
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(renameEdit.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
Upvotes: 2