I have an Android app with Activity containing a TextView as below.
<EditText
android:id="@+id/textEdit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:lines="4"
android:hint="My hint"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="left"
android:selectAllOnFocus="true"
android:textAppearance="?android:attr/textAppearanceSmall"/>
When the user clicks within this TextView (and only within this TextView), I want to show a custom keyboard. I read the excellent article here and used the CustomKeyboard class developed therein. The class contains the following "hack" to hide the default keyboard and show our custom keyboard.
// edittext is the TextView on which we want to show custom keyboard
// Rest of the code below from CustomKeyboard class
// Disable standard keyboard hard way
edittext.setOnTouchListener(new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
This shows the custom keyboard and does not show the default keyboard. However, this has one nasty problem. It results in the cursor getting stuck on the first character within the TextView. No matter where I click in the TextView, the cursor is still at the first character.
Based on some other suggestions on StackOverflow, I tried re-writing the code as below:
// edittext is the TextView on which we want to show custom keyboard
edittext.setOnTouchListener(new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
float x = event.getX();
float y = event.getY();
int touchPosition = edittext.getOffsetForPosition(x, y);
if (touchPosition>0){
edittext.setSelection(touchPosition);
}
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
This does not have any effect. When I log the value of touchPosition variable, it always shows -1, but I do see x and y changing with reasonable values.
I am clueless now. I have tried several other solutions suggested elsewhere, but not able to get it show only the custom keyboard with proper cursor handling.
Would appreciate any help.
After a lot of trial and error, I found out how to do this.
Earlier, I had placded the keyboard-hiding code in the onTouch(...) handler of the EditText. For whatever reason, that does not work. It still shows both keyboards (default and custom).
I moved that code to the activity as follows:
private OnTouchListener m_onTouchListenerEditText = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (v != m_myEditText)
{
// This is the function which hides the custom keyboard by hiding the keyboard view in the custom keyboard class (download link for this class is in the original question)
m_customKeyboard.hideCustomKeyboard();
((EditText) v).onTouchEvent(event);
}
return true;
}
};
private void hideDefaultKeyboard()
{
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}