How to change background color of key for android soft keyboard?

25,631

Solution 1

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>

Solution 2

<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"

Share:
25,631
yuralife
Author by

yuralife

Mobile software developer.

Updated on July 09, 2022

Comments

  • yuralife
    yuralife almost 2 years

    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.

  • TharakaNirmana
    TharakaNirmana about 10 years
    great answer, however the colors of keys in the keyboard are white. how can I change the color?
  • ericharlow
    ericharlow about 10 years
    in your drawable/normal.xml find the > <solid android:color="#FFFFFF"/> change the #FFFFFF to the color you want. see ShapeDrawable for detailed info.
  • Master Zangetsu
    Master Zangetsu almost 10 years
    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 <key> in your keyboard xml?
  • Admin
    Admin over 7 years
    how about i need that the button stays yellow after clicking on it