Yog Guru
Yog Guru

Reputation: 2104

Change the icon of key at run time in Custom Keyboard

I'm creating custom keyboard,changing text & icon of key at run time and I'm successfully change the text of key but not find any way to change the icon of key.

any help,hint will be appreciable.

Code for change the Key text is:

Keyboard currentKeyboard = CustomKeyboard.mInputView.getKeyboard();
List<Keyboard.Key> keys = currentKeyboard.getKeys();
CustomKeyboard.mInputView.invalidateKey(changeKey);
keys.get(changeKey).label = "Change Text";

Upvotes: 2

Views: 3289

Answers (2)

highpass
highpass

Reputation: 163

On my way in the Onkey:

case keycode:
    Keyboard currentKeyboard = mInputView.getKeyboard();
    List<Keyboard.Key> keys = currentKeyboard.getKeys();
    mInputView.invalidateKey(*keycode*);
    keys.get(*keycode*).label = null;
    key.get(*keycode*).icon = getResources().getDrawable(R.drawable.image);

keycode is: key code on xml or Keyboard.KEYCODE_...

Upvotes: 0

Bhavana Vadodariya
Bhavana Vadodariya

Reputation: 2287

don't use ".lable" property.
It will take either >lable or .icon (grrrr.... really bad, but still it is obvious, consider top thing on the image, because here icon is not background but its top image.)
Instead you have to use:

android:keyIcon="@drawable/your_top_icon"  
android:text="your_text"(Text will not appear on key but you will get this value in listener.)  

Now the scenario will be also get changed:
instead of onKey() method now onText() method will be called (Sequence will be same)
In this way you can change the individual icon at runtime.

keys.get(changeKey).text = "Change Text";   
keys.get(changeKey).icon = Drawable;

-ve points:
It means if you have 40 (or 400) Keys in your Keyboard, you have to use 40 different images :'(

You can apply some tricks like applying style, but again it will be for whale layout, for individual key style will not work.

I am too searching some another way to replace image for individual, if find anything let you know.

Upvotes: 2

Related Questions