3

I am Working on a Custom Keyboard and I need to set different image for each key at runtime or dynamically but I am not getting how to do this.

I am using this code:

<com.keyboard.KeyBoard.LatinKeyboardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.keyboard.KeyBoard"
android:id="@+id/keyboard"
android:keyBackground="@drawable/black_bg" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="true"
android:background="@android:color/black"
android:focusableInTouchMode="true" />

and now i want to get android:keyBackground="@drawable/black_bg" attribute in:

@Override 
public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);   
}

because i have to change image background here for keys dynamically but I am not getting how to do this.

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
user
  • 471
  • 2
  • 16
  • 31
  • I thinkthat a similar answer can be found here - > http://stackoverflow.com/questions/6000543/customize-the-appearance-of-a-key – g00dy Aug 12 '13 at 13:04
  • But i want to set background of keys instead of text i am not getting how to do this plz help me – user Aug 12 '13 at 13:19
  • The "key" here is the for loop: `for(Key key: keys)` - this thing will go through all the keys of the keyboard and you can set the key's icon (see this http://developer.android.com/reference/android/inputmethodservice/Keyboard.Key.html#icon), which is taken from here http://developer.android.com/reference/android/inputmethodservice/Keyboard.Key.html . I think that this is the only way of changing keys individually. Your method will change the background of all the buttons. – g00dy Aug 12 '13 at 13:23
  • for a key its not possible because there is no constant background for the key..but you can set drawable images as a background for keys..for this you have to create different colours of drawables – kalyan pvs Aug 12 '13 at 13:23
  • @kalyanpvs how i can set in drawable actuly i have to set only one image ie. white image firset time when user will select black than all keys have black image in backgrount thats it can u please tell me – user Aug 12 '13 at 13:52
  • Keyboard keyboard = keyboardView.getKeyboard(); List keys = keyboard.getKeys(); HashMap data = new HashMap(); for (Key key : keys) { if (Character.isLetter(key.codes[0])) { key.icon=your drawable image; } } – kalyan pvs Aug 12 '13 at 15:03
  • @Kalyanpvs u mean if (Character.isLetter(key.codes[0])) { key.icon= R.drawable.whitebg; } but i am getting error on key.icon= R.drawable.whitebg; line please tell me – user Aug 13 '13 at 05:01
  • Actually android framework doesn't provide us the ability to change a key background at runtime.If you want to achieve this (change skin, e.g.), you'll have to do most drawing work with your custom view. – suitianshi Feb 13 '14 at 02:58
  • Look my answer [http://stackoverflow.com/a/39091883/4964307](http://stackoverflow.com/a/39091883/4964307) I hope to help you. – M星空 Aug 23 '16 at 03:52

1 Answers1

2

There was a later duplicate question posted at how to change key background of any key in Android soft keyboard . It had some good answers.

Community
  • 1
  • 1
scottt
  • 8,301
  • 1
  • 31
  • 41