How to add shift key in Android custom keyboard? -
i working on custom keyboard. want add shift key , change xml file when user press it.
lowercase.xml
<!-- copyright (c) 2008-2012 omron software co., ltd. licensed under apache license, version 2.0 (the "license"); may not use file except in compliance license. may obtain copy of license @ http://www.apache.org/licenses/license-2.0 unless required applicable law or agreed in writing, software distributed under license distributed on "as is" basis, without warranties or conditions of kind, either express or implied. see license specific language governing permissions , limitations under license. --> <keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keywidth="10%p" android:horizontalgap="0dip" android:verticalgap="0dip" android:keyheight="@dimen/key_height" > <row> <key android:codes="113" android:keylabel="q" android:keyedgeflags="left"/> <key android:codes="119" android:keylabel="w"/> <key android:codes="101" android:keylabel="e"/> <key android:codes="114" android:keylabel="r"/> <key android:codes="116" android:keylabel="t"/> <key android:codes="121" android:keylabel="y"/> <key android:codes="117" android:keylabel="u"/> <key android:codes="105" android:keylabel="i"/> <key android:codes="111" android:keylabel="o"/> <key android:codes="112" android:keylabel="p" android:keyedgeflags="right"/> </row> <row> <key android:codes="97" android:keylabel="a" android:horizontalgap="5%p" android:keyedgeflags="left"/> <key android:codes="115" android:keylabel="s"/> <key android:codes="100" android:keylabel="d"/> <key android:codes="102" android:keylabel="f"/> <key android:codes="103" android:keylabel="g"/> <key android:codes="104" android:keylabel="h"/> <key android:codes="106" android:keylabel="j"/> <key android:codes="107" android:keylabel="k"/> <key android:codes="108" android:keylabel="l" android:keyedgeflags="right"/> </row> <row> <key android:codes="00" android:keyicon="@drawable/key_qwerty_shift" android:iconpreview="@drawable/key_qwerty_shift_b" android:keywidth="15%p" android:ismodifier="true" android:issticky="true" android:keyedgeflags="left"/> <key android:codes="122" android:keylabel="z"/> <key android:codes="120" android:keylabel="x"/> <key android:codes="99" android:keylabel="c"/> <key android:codes="118" android:keylabel="v"/> <key android:codes="98" android:keylabel="b"/> <key android:codes="110" android:keylabel="n"/> <key android:codes="109" android:keylabel="m"/> <key android:codes="-5" android:keyicon="@drawable/key_del" android:iconpreview="@drawable/key_del_b" android:keywidth="15%p" android:keyedgeflags="right" android:isrepeatable="true"/> </row> <row android:rowedgeflags="bottom"> <key android:codes="00" android:keylabel="123" android:keywidth="15%p" android:popupkeyboard="@xml/default_en_symbols" android:keyedgeflags="left"/> <key android:codes="44" android:keylabel="," android:keywidth="10%p"/> <key android:codes="32" android:keyicon="@drawable/key_qwerty_space" android:iconpreview="@drawable/key_qwerty_space_b" android:keywidth="40%p" android:isrepeatable="true"/> <key android:codes="46" android:keylabel="." android:keywidth="10%p"/> <key android:codes="63" android:keylabel="\?" android:keywidth="10%p"/> <key android:codes="13" android:keyicon="@drawable/key_enter" android:iconpreview="@drawable/key_enter_b" android:keywidth="15%p" android:keyedgeflags="right"/> </row> </keyboard>
and upercase.xml
<keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keywidth="10%p" android:horizontalgap="0px" android:verticalgap="0px" android:keyheight="6%p" > <row android:keywidth="18%p"> <key android:codes="49" android:keylabel="1" android:keyedgeflags="left"/> <key android:codes="50" android:keylabel="2"/> <key android:codes="51" android:keylabel="3"/> <key android:codes="52" android:keylabel="4"/> <key android:codes="53" android:keylabel="5"/> <key android:codes="54" android:keylabel="6"/> <key android:codes="55" android:keylabel="7"/> <key android:codes="56" android:keylabel="8"/> <key android:codes="57" android:keylabel="9"/> <key android:codes="48" android:keylabel="0" android:keyedgeflags="right"/> </row> <row > <key android:codes="113" android:keylabel="q" android:keyedgeflags="left"/> <key android:codes="119" android:keylabel="w"/> <key android:codes="101" android:keylabel="e"/> <key android:codes="114" android:keylabel="r"/> <key android:codes="116" android:keylabel="t"/> <key android:codes="121" android:keylabel="y"/> <key android:codes="117" android:keylabel="u"/> <key android:codes="105" android:keylabel="i"/> <key android:codes="111" android:keylabel="o"/> <key android:codes="112" android:keylabel="p" android:keyedgeflags="right"/> </row> <row > <key android:codes="46" android:keylabel="." android:keyedgeflags="left"/> <key android:codes="97" android:keylabel="a"/> <key android:codes="115" android:keylabel="s"/> <key android:codes="100" android:keylabel="d"/> <key android:codes="102" android:keylabel="f"/> <key android:codes="103" android:keylabel="g"/> <key android:codes="104" android:keylabel="h"/> <key android:codes="106" android:keylabel="j"/> <key android:codes="107" android:keylabel="k"/> <key android:codes="108" android:keylabel="l" android:keyedgeflags="right"/> <key android:codes="-5" android:keyicon="@drawable/sym_keyboard_delete" android:keywidth="15%p" android:keyedgeflags="right" android:isrepeatable="true"/> </row> <row android:keyheight="8%p"> <key android:codes="00" android:keyicon="@drawable/key_qwerty_shift" android:iconpreview="@drawable/key_qwerty_shift_b" android:keywidth="15%p" android:ismodifier="true" android:issticky="true" android:keyedgeflags="left"/> <key android:codes="32" android:keylabel="space" android:keywidth="20%p" android:isrepeatable="true"/> <key android:codes="122" android:keylabel="z"/> <key android:codes="120" android:keylabel="x"/> <key android:codes="99" android:keylabel="c"/> <key android:codes="118" android:keylabel="v"/> <key android:codes="98" android:keylabel="b"/> <key android:codes="110" android:keylabel="n"/> <key android:codes="109" android:keylabel="m"/> <key android:codes="10" android:keyicon="@drawable/sym_keyboard_return" android:keywidth="20%p" android:keyedgeflags="right"/> </row> </keyboard>
customkeyboard.java
class customkeyboard { /** link keyboardview used render customkeyboard. */ private keyboardview mkeyboardview; /** link activity hosts {@link #mkeyboardview}. */ private activity mhostactivity; /** key (code) handler. */ private onkeyboardactionlistener monkeyboardactionlistener = new onkeyboardactionlistener() { public final static int codedelete = -5; // keyboard.keycode_delete public final static int codeprev = 55000; public final static int codenext = 55001; public final static int codedone = 55002; @override public void onkey(int primarycode, int[] keycodes) { // note can '<key android:codes="49,50" ... >' in xml // file; codes come in keycodes, first in list in // primarycode // edittext , 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 key edittext if (primarycode == codedone) { hidecustomkeyboard(); } else if (primarycode == codedelete) { if (editable != null && start > 0) editable.delete(start - 1, start); } else if (primarycode == codeprev) { view focusnew = edittext.focussearch(view.focus_backward); if (focusnew != null) focusnew.requestfocus(); } else if (primarycode == codenext) { view focusnew = edittext.focussearch(view.focus_forward); if (focusnew != null) focusnew.requestfocus(); } 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 custom keyboard, uses keyboardview (with resource id * <var>viewid</var>) of <var>host</var> activity, , load keyboard * layout xml file <var>layoutid</var> (see {@link keyboard} * description). note <var>host</var> activity must have * <var>keyboardview</var> in layout (typically aligned bottom * of activity). note keyboard layout xml file may include key * codes navigation; see constants in class values. * note enable edittext's use custom keyboard, call * {@link #registeredittext(int)}. * * @param host * hosting activity. * @param viewid * id of keyboardview. * @param layoutid * id of xml file containing 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 not show preview // balloons mkeyboardview.setonkeyboardactionlistener(monkeyboardactionlistener); // hide standard keyboard mhostactivity.getwindow().setsoftinputmode( windowmanager.layoutparams.soft_input_state_always_hidden); } /** returns whether customkeyboard visible. */ public boolean iscustomkeyboardvisible() { return mkeyboardview.getvisibility() == view.visible; } /** * make customkeyboard visible, , hide system keyboard 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 customkeyboard invisible. */ public void hidecustomkeyboard() { mkeyboardview.setvisibility(view.gone); mkeyboardview.setenabled(false); } /** * register <var>edittext<var> resource id <var>resid</var> (on * hosting activity) using custom keyboard. * * @param resid * resource id of edittext registers custom * keyboard. */ public void registeredittext(int resid) { // find edittext 'resid' edittext edittext = (edittext) mhostactivity.findviewbyid(resid); // make custom keyboard appear edittext.setonfocuschangelistener(new onfocuschangelistener() { // note setting on focus listener, can show custom // keyboard when edit box gets focus, hide when // edit box loses focus @override public void onfocuschange(view v, boolean hasfocus) { if (hasfocus) showcustomkeyboard(v); else hidecustomkeyboard(); } }); edittext.setonclicklistener(new onclicklistener() { // note setting on click listener, can show custom // keyboard again, tapping on edit box had focus // (but had keyboard hidden). @override public void onclick(view v) { showcustomkeyboard(v); } }); // disable standard keyboard hard way // note there easy way: // 'edittext.setinputtype(inputtype.type_null)' (but not have // cursor, , no 'edittext.setcursorvisible(true)' doesn't work ) edittext.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { edittext edittext = (edittext) v; int intype = edittext.getinputtype(); // backup 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 words android) edittext.setinputtype(edittext.getinputtype() | inputtype.type_text_flag_no_suggestions); } }
please me convert uppercase keyboard lowercase keyboard.
this old question. came across it. here's take on answer future reference if interested. add:
<key android:codes="-1" android:keyicon="@drawable/sym_keyboard_shift" android:keywidth="12.5%p" android:keyedgeflags="left"/>
to add shift key on left side instance. add onkey() method if:
else if (primarycode == -1){ caps = !caps; keyboard.setshifted(caps); mkeyboardview.invalidateallkeys() }
where keyboard instance of keyboard inflated using 1 of xmls. instance:
keyboard = new keyboard(this, r.xml.lowercase);
caps flag determines whether shift upper case or shift lower case. should shift keyboard when key clicked.
p.s should use switch case statement instead of if else statements.
Comments
Post a Comment