0

Problem statement

I'm trying to implement my own virtual keyboard following example "softkeyboard", found in samples folder of android-sdk.

In the onCreateInputView() view is created with layout inflater like this:

@Override
public View onCreateInputView() {
    mContainerView = getLayoutInflater().inflate(R.layout.my_keyboard, null);
    return mContainerView;
}

In my beautiful virtual keyboard gui definition (my_keyboard.xml) I have added some Buttons like this.

<Button
    android:id="@+id/my_button1"
    android:onClick="onMyButton1Pressed"
    android:text="@string/my_button1_text" />

Now If I run the program, Button appears to be working: onMyButton1Pressed() gets called OK. The problem is that Button state drawings (orange highlight when pressed down) do not work.

EDIT 1: Issue is same with ToggleButton: when pressed down, orange highlight won't appear. However green "checked" mark works. If I use these in normal Activity, pressing down does give orange highlight when button is being pressed.

My findings so far, could be useful or not...

I have been trying to google, and read android documentation. Somewhere I read that using inflate with null root element, does not apply themes/styles/background-drawables/whatever (what is the correct term?) to inflated hierarchy. Is this the problem, why button states don't show?

Now when I create virtual keyboard, what is the root element, where keyboard is added?

Also I found an alternative inflate method:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

    resource = ID for an XML layout resource to load (e.g., R.layout.main_page)
    root = Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
    attachToRoot = Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Could I pass this some "dummy" viewgroup from which it can extract layout params?

JoonasS
  • 587
  • 5
  • 14

1 Answers1

0

I tried background drawable + selector xml as mango suggested, and it works. So here is one possible workaround:

1.Add background drawable to button

<Button
    android:id="@+id/my_button1"
    android:onClick="onMyButton1Pressed"
    android:text="@string/my_button1_text" 
    android:background="@drawable/my_button_drawable
/>

2.Created my_button_drawable.xml in /res/drawable/

<?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/btn_default_pressed" />
    <item android:state_enabled="false"
          android:drawable="@drawable/btn_default_transparent_normal" />
    <item android:drawable="@drawable/btn_default_normal" />
</selector>

3.Looked for button background images in: ANDROID_SDK_FOLDER/platforms/android-8/data/res/drawable-hdpi/

  • btn_default_normal.9.png (for normal state)
  • btn_default_pressed.9.png (for pressed state)
  • btn_default_transparent_normal.9.png (for disabled state)

and added those images to drawable folder.

Now if I press button, it gets orange highlight, and disabled state looks ok also. I don't need focused or selected states so I didn't include these in the selector.

JoonasS
  • 587
  • 5
  • 14