Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 877
yuralife Avatar asked Apr 03 '13 14:04

yuralife


Video Answer


2 Answers

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>
like image 120
ericharlow Avatar answered Sep 23 '22 14:09

ericharlow


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

like image 25
Nitin Avatar answered Sep 21 '22 14:09

Nitin