Android: How to open keyboard for editing EditText when click button?
Thanks guys for your help :) I used all suggestions that you gave me, searched and tested a lot of other scripts and finally my code is working :)
Here is my final code:
InputEditText = (EditText) findViewById(R.id.InputText);
public void InputData() {
/* Keyboard Button Action */
KeyboardButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "On Keyboard Button click event!");
InputEditText.requestFocus();
InputEditText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);
}
});
}
It may be useful for someone :) Thank you!
The design you've described isn't recommended. You're violating the focusable
attribute's purpose which is not to control whether the user can alter the text in a EditText
component.
If you plan to provide an alternative input method because the use case seems to require this (e.g. you allow only a certain set of symbols in the editable text field) then you should probably disable text editing altogether for the time the user isn't allowed to change the value of the field.
Declare your editable field:
<EditText
android:id="@+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
notice that its focusable
attribute is left with the default value. That's ok, we'll handle that later. Declare a button which will start editing process:
<Button
android:id="@+id/button_show_ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start editing" />
Now, in your Activity
declare:
private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;
And in onCreate()
do this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ime_activity);
// Find out our editable field.
editText2 = (EditText)findViewById(R.id.edit_text_2);
// Save its key listener which makes it editable.
originalKeyListener = editText2.getKeyListener();
// Set it to null - this will make the field non-editable
editText2.setKeyListener(null);
// Find the button which will start editing process.
buttonShowIme = (Button)findViewById(R.id.button_show_ime);
// Attach an on-click listener.
buttonShowIme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Restore key listener - this will make the field editable again.
editText2.setKeyListener(originalKeyListener);
// Focus the field.
editText2.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
}
});
// We also want to disable editing when the user exits the field.
// This will make the button the only non-programmatic way of editing it.
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// If it loses focus...
if (!hasFocus) {
// Hide soft keyboard.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0);
// Make it non-editable again.
editText2.setKeyListener(null);
}
}
});
}
I hope the code with all the comments is self-explanatory. Tested on APIs 8 and 17.
Try this :
final EditText myedit2 = (EditText) findViewById(R.id.myEditText2);
Button btsmall = (Button) findViewById(R.id.BtSmall);
btsmall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
myedit2.requestFocus();
}
});