8

I am implementing custom keyboard on Android. I have just read documentation on "developer.android.com" and seen sample with soft keyboard. All I can - it`s to change keyboard background, change placement of buttons, set keyIcon instead of keyLabel to key.

But I still can not change key`s background and color.

Please write some sample code of XML or source. Thanks!

My sample where I change background:

    public class GBInput extends InputMethodService implements KeyboardView.OnKeyboardActionListener{
    ...
    private GBKeyboardView mInputView;
    @Override
        public View onCreateInputView() {
            mInputView = (GBKeyboardView) getLayoutInflater().inflate(R.layout.input, null);
            mInputView.setOnKeyboardActionListener(this);
            mInputView.setKeyboard(mQwertyKeyboard);
            mInputView.setBackgroundResource(R.color.keyboard_background);
            return mInputView;
        }
    ...
    }

And I need something like that : enter image description here

Images for all buttons - its bad idea, so I want to find better issue.

yuralife
  • 1,545
  • 2
  • 21
  • 35

2 Answers2

24

Add this line of code to your input.xml

android:keyBackground="@drawable/samplekeybackground"

So your input.xml should end up looking similar to this.

<com.example.keyboard.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:keyPreviewLayout="@layout/input_key_preview"
    android:keyBackground="@drawable/samplekeybackground"
    />

If you already have a drawable to use great otherwise here is a sample key background drawable that will produce results like your example image.

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item
        android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/normal" />
    <!-- Pressed state -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/pressed" /></selector>

If you want everything in xml you can use shape xml's like the following. drawable/normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <stroke android:width="4dp" android:color="#000000" /> 
  <solid android:color="#FFFFFF"/> 
</shape> 

and drawable/pressed.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
      <stroke android:width="4dp" android:color="#A3D9F3" /> 
      <solid android:color="#4ABCE8"/> 
    </shape>
ericharlow
  • 2,045
  • 3
  • 21
  • 26
  • great answer, however the colors of keys in the keyboard are white. how can I change the color? – TharakaNirmana Mar 18 '14 at 09:51
  • 1
    in your drawable/normal.xml find the > `` change the #FFFFFF to the color you want. see [ShapeDrawable](http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape) for detailed info. – ericharlow Mar 18 '14 at 20:30
  • 6
    This only works at the KeyboardView level, not if your trying to set a different colour for specific keys. Any ideas on how to implement within in your keyboard xml? – Master Zangetsu Jun 19 '14 at 10:06
  • how about i need that the button stays yellow after clicking on it –  Oct 07 '16 at 12:25
4
<com.example.keyboard.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:keyPreviewLayout="@layout/input_key_preview"
    android:keyBackground="@drawable/samplekeybackground"
    android:keyTextColor="#000000"
    />

just add this line to your keyboardView to change text color to black

android:keyTextColor="#000000"

Nitin
  • 111
  • 1
  • 3