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

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>

Leave a Comment