How to change background color of key for android soft keyboard?
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"
Comments
-
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 :
Images for all buttons - its bad idea, so I want to find better issue.
-
TharakaNirmana about 10 yearsgreat answer, however the colors of keys in the keyboard are white. how can I change the color?
-
ericharlow about 10 yearsin 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 almost 10 yearsThis 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 over 7 yearshow about i need that the button stays yellow after clicking on it