Reputation: 125
I want to use my custom keyboard in android application. When I click Edit Text my keyboard popup and show properly, but when I press keyboards button doesn't type anything and I don't know why?
Here is my CustomKeyboard class:
/**
* Created by KHALED on 8/10/2015.
*/
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
class CustomKeyboard {
/** A link to the KeyboardView that is used to render this CustomKeyboard. */
private KeyboardView mKeyboardView;
/** A link to the activity that hosts the {@link #mKeyboardView}. */
private Activity mHostActivity;
private KeyboardView kv;
private Keyboard keyboard;
private boolean caps = false;
/** The key (code) handler. */
private KeyboardView.OnKeyboardActionListener mOnKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {
public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
public final static int CodeCancel = -3; // Keyboard.KEYCODE_CANCEL
public final static int CodePrev = 55000;
public final static int CodeAllLeft = 55001;
public final static int CodeLeft = 55002;
public final static int CodeRight = 55003;
public final static int CodeAllRight = 55004;
public final static int CodeNext = 55005;
public final static int CodeClear = 55006;
public final static int KEYCODE_SHIFT = -1;
public final static int KEYCODE_DONE = -4;
@Override public void onKey(int primaryCode, int[] keyCodes) {
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml file; all codes come in keyCodes, the first in this list in primaryCode
// Get the EditText and its Editable
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if( focusCurrent==null || focusCurrent.getClass()!=EditText.class ) return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
// Apply the key to the edittext
if( primaryCode==CodeCancel ) {
hideCustomKeyboard();
} else if( primaryCode==CodeDelete ) {
if( editable!=null && start>0 ) editable.delete(start - 1, start);
} else if( primaryCode==CodeClear ) {
if( editable!=null ) editable.clear();
} else if( primaryCode==CodeLeft ) {
if( start>0 ) edittext.setSelection(start - 1);
} else if( primaryCode==CodeRight ) {
if (start < edittext.length()) edittext.setSelection(start + 1);
} else if( primaryCode==CodeAllLeft ) {
edittext.setSelection(0);
} else if( primaryCode==CodeAllRight ) {
edittext.setSelection(edittext.length());
}else if( primaryCode==KEYCODE_DONE ) {
hideCustomKeyboard();
}
// else if( primaryCode==KEYCODE_SHIFT ) {
// caps = !caps;
// keyboard.setShifted(caps);
//kv.invalidateAllKeys();
// }
else { // insert character
editable.insert(start, Character.toString((char) primaryCode));
}
}
@Override public void onPress(int arg0) {
}
@Override public void onRelease(int primaryCode) {
}
@Override public void onText(CharSequence text) {
}
@Override public void swipeDown() {
}
@Override public void swipeLeft() {
}
@Override public void swipeRight() {
}
@Override public void swipeUp() {
}
};
/**
* Create a custom keyboard, that uses the KeyboardView (with resource id <var>viewid</var>) of the <var>host</var> activity,
* and load the keyboard layout from xml file <var>layoutid</var> (see {@link Keyboard} for description).
* Note that the <var>host</var> activity must have a <var>KeyboardView</var> in its layout (typically aligned with the bottom of the activity).
* Note that the keyboard layout xml file may include key codes for navigation; see the constants in this class for their values.
* Note that to enable EditText's to use this custom keyboard, call the {@link #registerEditText(int)}.
*
* @param host The hosting activity.
* @param viewid The id of the KeyboardView.
* @param layoutid The id of the xml file containing the keyboard layout.
*/
public CustomKeyboard(Activity host, int viewid, int layoutid) {
mHostActivity= host;
mKeyboardView= (KeyboardView)mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
/** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
/** Make the CustomKeyboard visible, and hide the system keyboard for view v. */
public void showCustomKeyboard( View v ) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if( v!=null ) ((InputMethodManager)mHostActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}
/**
* Register <var>EditText<var> with resource id <var>resid</var> (on the hosting activity) for using this custom keyboard.
*
* @param resid The resource id of the EditText that registers to the custom keyboard.
*/
public void registerEditText(int resid) {
// Find the EditText 'resid'
EditText edittext= (EditText)mHostActivity.findViewById(resid);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
// NOTE By setting the on focus listener, we can show the custom keyboard when the edit box gets focus, but also hide it when the edit box loses focus
@Override public void onFocusChange(View v, boolean hasFocus) {
if( hasFocus ) showCustomKeyboard(v); else hideCustomKeyboard();
}
});
edittext.setOnClickListener(new View.OnClickListener() {
// NOTE By setting the on click listener, we can show the custom keyboard again, by tapping on an edit box that already had focus (but that had the keyboard hidden).
@Override public void onClick(View v) {
showCustomKeyboard(v);
}
});
// Disable standard keyboard hard way
// NOTE There is also an easy way: 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
edittext.setOnTouchListener(new View.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
}
});
// Disable spell check (hex strings look like words to Android)
edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
}
// NOTE How can we change the background color of some keys (like the shift/ctrl/alt)?
// NOTE What does android:keyEdgeFlags do/mean
And this is keyboard design(kurdkbr.xml):
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="50dp"
>
<Row>
<Key android:codes="1633" android:keyLabel="١" android:keyEdgeFlags="left"/>
<Key android:codes="1634" android:keyLabel="٢"/>
<Key android:codes="1635" android:keyLabel="٣"/>
<Key android:codes="1636" android:keyLabel="٤"/>
<Key android:codes="1637" android:keyLabel="٥"/>
<Key android:codes="1638" android:keyLabel="٦"/>
<Key android:codes="1639" android:keyLabel="٧"/>
<Key android:codes="1640" android:keyLabel="٨"/>
<Key android:codes="1641" android:keyLabel="٩"/>
<Key android:codes="1632" android:keyLabel="٠" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="1602" android:keyLabel="ق" android:keyEdgeFlags="left"/>
<Key android:codes="1700" android:keyLabel="ڤ"/>
<Key android:codes="1601" android:keyLabel="ف"/>
<Key android:codes="1594" android:keyLabel="غ"/>
<Key android:codes="1593" android:keyLabel="ع"/>
<Key android:codes="1607" android:keyLabel="ه"/>
<Key android:codes="1607" android:keyLabel="ه"/>
<Key android:codes="1578" android:keyLabel="ت" android:popupKeyboard="@xml/popup" android:popupCharacters="تط"/>
<Key android:codes="1581" android:keyLabel="ح"/>
<Key android:codes="1582" android:keyLabel="خ" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="1588" android:keyLabel="ش" android:keyEdgeFlags="left"/>
<Key android:codes="1587" android:keyLabel="س"/>
<Key android:codes="1610" android:keyLabel="ی"/>
<Key android:codes="1576" android:keyLabel="ب"/>
<Key android:codes="1604" android:keyLabel="ل"/>
<Key android:codes="1717" android:keyLabel="ڵ"/>
<Key android:codes="1575" android:keyLabel="ا"/>
<Key android:codes="1606" android:keyLabel="ن"/>
<Key android:codes="1580" android:keyLabel="ج"/>
<Key android:codes="1670" android:keyLabel="چ" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="1586" android:keyLabel="ز" android:keyEdgeFlags="left"/>
<Key android:codes="1585" android:keyLabel="ر"/>
<Key android:codes="1685" android:keyLabel="ڕ"/>
<Key android:codes="1583" android:keyLabel="د"/>
<Key android:codes="1608" android:keyLabel="و"/>
<Key android:codes="1734" android:keyLabel="ۆ"/>
<Key android:codes="1603" android:keyLabel="ك"/>
<Key android:codes="1711" android:keyLabel="گ"/>
<Key android:codes="1574" android:keyLabel="ئ"/>
<Key android:codes="63,33,58,44" android:keyLabel="\? ! : " android:keyEdgeFlags="right"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="44" android:keyLabel="," android:keyWidth="10%p" android:keyEdgeFlags="left"/>
<Key android:codes="47" android:keyLabel="/" android:keyWidth="10%p" />
<Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true"/>
<Key android:codes="-5" android:keyLabel="DEL" android:keyWidth="20%p" android:isRepeatable="true"/>
<Key android:codes="-4" android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
And keyboard view layout:
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/samplekeybackground"
android:keyTextColor="@color/blue_steel"
android:keyTextSize="20dp"
android:iconPreview="@drawable/samplekeybackground"
android:visibility="gone" />
And finally in main class:
public class NewPost extends ActionBarActivity {
CustomKeyboard mCustomKeyboard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_post);
mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.kurdkbr );
Keyboard mKeyboard= new Keyboard(NewPost.this,R.xml.kurdkbr);
mCustomKeyboard.registerEditText(R.id.title_neew_post);
// mCustomKeyboard.registerEditText(R.id.content_new_post);
KeyboardView mKeyboardView= (KeyboardView)findViewById(R.id.keyboardview);
// Attach the keyboard to the view
mKeyboardView.setKeyboard(mKeyboard);
// Do not show the preview balloons
mKeyboardView.setPreviewEnabled(false);
}
}
Can anybody tell me what I forgot to handle? As I say keyboard show properly but don't type?
Upvotes: 1
Views: 1096
Reputation: 455
I am getting same error. Instead of extending ActionBarActivity ,try to extend Activity. Your above code works in lollipop devices but not below.
Upvotes: 1