3

I'm trying to set a custom background for my KeyboardView. setting the XML attribute keyBackground seems to be the way to go about it, but I keep getting a NullPointerException slash ResourceNotFoundException in my logcat when it tries to inflate the drawable. I'm not really sure what's wrong -- all the files seem to be in the right location. I've put them in multiple drawable folders, cleaned my project, etc.

main.xml file:

<android.inputmethodservice.KeyboardView
    android:id="@+id/keyboardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:visibility="gone"
    android:keyBackground="@drawable/keybg" />

keybg.xml file:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:state_pressed="true"
        android:drawable="@drawable/keybg_pressed" />
    <item
        android:drawable="@drawable/keybg_notpressed" />
</selector>

keybg_pressed and keybg_notpressed are both png files

(relevant) logcat:

E/AndroidRuntime(17772): FATAL EXCEPTION: main
E/AndroidRuntime(17772): java.lang.RuntimeException: Unable to start activity   
    ComponentInfo{MainActivity}: android.view.InflateException:   
    Binary XML file line #620: Error inflating class 
    android.inputmethodservice.KeyboardView

E/AndroidRuntime(17772): Caused by: android.content.res.Resources$NotFoundException: File   
    res/drawable/keybg.xml from drawable resource ID #0x7f0201a3
E/AndroidRuntime(17772):    at   
    android.content.res.Resources.loadDrawable(Resources.java:1947)
E/AndroidRuntime(17772):    at 
    android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime(17772):    at android.inputmethodservice.KeyboardView.<init>    
    (KeyboardView.java:303)
E/AndroidRuntime(17772):    at android.inputmethodservice.KeyboardView.<init>  
    (KeyboardView.java:279)
E/AndroidRuntime(17772):    ... 28 more
E/AndroidRuntime(17772): Caused by: java.lang.NullPointerException
E/AndroidRuntime(17772):    at  
    android.graphics.drawable.DrawableContainer$DrawableContainerState.
    addChild(DrawableContainer.java:524)
E/AndroidRuntime(17772):    at  
 android.graphics.drawable.StateListDrawable$StateListState.addStateSet(StateListDrawable.java:278)
E/AndroidRuntime(17772):    at   
    android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:186)
E/AndroidRuntime(17772):    at  
    android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
E/AndroidRuntime(17772):    at   
    android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
E/AndroidRuntime(17772):    at  
     android.content.res.Resources.loadDrawable(Resources.java:1944)
E/AndroidRuntime(17772):    ... 31 more
vivianh
  • 155
  • 1
  • 3
  • 12

2 Answers2

1

you say "keybg_pressed and keybg_notpressed are both png files".

They should be XML files ! (I know it's weird).

I use this for my custom keyboard view:

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/keyboardView"
    android:layout_alignParentBottom="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:keyBackground="@drawable/samplekeybackground"
    android:keyTextColor="@android:color/white"
    android:keyPreviewLayout="@layout/input_key_preview" 
    android:background="@drawable/keyboardbackground"
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
0

create a folder in res and make a xml file like:

<!-- Functional keys. -->

<item android:state_single="true" android:state_pressed="true"
      android:drawable="@drawable/btn_keyboard_key_dark_pressed_holo" />
<item android:state_single="true"
      android:drawable="@drawable/btn_keyboard_key_dark_normal_holo" />

<!-- Toggle keys. Use checkable/checked state. -->
<!-- The lock icon is a copy of the 'on' icon, hue shifted by +164 using Gimp's Colors/Hue&Saturation -->

<item android:state_checkable="true" android:state_checked="true" android:state_pressed="true"
      android:drawable="@drawable/btn_keyboard_key_dark_pressed_on_holo" />
<item android:state_checkable="true" android:state_pressed="true"
      android:drawable="@drawable/btn_keyboard_key_dark_pressed_off_holo" />
<item android:state_checkable="true" android:state_checked="true" android:state_active="true"
      android:drawable="@drawable/btn_keyboard_key_dark_normal_lock_holo" />
<item android:state_checkable="true" android:state_checked="true"
      android:drawable="@drawable/btn_keyboard_key_dark_normal_on_holo" />
<item android:state_checkable="true"
      android:drawable="@drawable/btn_keyboard_key_dark_normal_off_holo" />

<!-- Normal keys. -->

<item android:state_pressed="true"
      android:drawable="@drawable/btn_keyboard_key_light_pressed_holo" />
<item android:drawable="@drawable/btn_keyboard_key_light_normal_holo" />

above image exists in drawable-hdi png

Sonu Kumar
  • 969
  • 1
  • 11
  • 36