1

I'm adopting the concept of borderless button to an application I'm developing. I could edit my buttons perfectly so far with the help of this questions:

How to create standard Borderless buttons (like in the design guidline mentioned)?

Android Drawing Separator/Divider Line in Layout?

But now I need to show a different background image or something to emphasize when the button is not enabled due to some obligatory field the user hasn't filled.

My code so far:

some_activity.xml

<RelativeLayout
    android:id="@+id/layoutId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:background="@android:color/darker_gray"/>

    <Button
        android:id="@+id/buttonId"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="?android:attr/selectableItemBackground"
        android:onClick="onButtonClicked"
        android:text="@string/someText"
        android:textColor="@color/someColor"
        android:textSize="25sp"
        android:textStyle="bold" />

</RelativeLayout>

Now, I'm intended to put a striped image in the background of the button to show when it's disabled. By reading this question below, I could customize my button when it wasn't borderless.

Standard Android Button with a different color

I tried to use this idea to customize my borderless button like the following code:

drawable/borderless_button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_enabled="true" 
        android:drawable="?android:attr/selectableItemBackground" >
    </item>

    <item android:state_enabled="false"
        android:drawable="@drawable/stripes"
        android:tileMode="repeat" >
    </item>
</selector>

But it seems impossible, once ?android:attr/selectableItemBackground in the borderless_button_bg.xml makes the program to crash.

LOGCAT

05-28 17:44:54.139: W/dalvikvm(31664): threadid=1: thread exiting with uncaught exception (group=0x40bc61f8)
05-28 17:44:54.154: E/AndroidRuntime(31664): FATAL EXCEPTION: main
05-28 17:44:54.154: E/AndroidRuntime(31664): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Company.productname/com.Company.productname.myActivity}: android.view.InflateException: Binary XML file line #310: Error inflating class <unknown>
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.os.Looper.loop(Looper.java:137)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.ActivityThread.main(ActivityThread.java:4514)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at java.lang.reflect.Method.invokeNative(Native Method)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at java.lang.reflect.Method.invoke(Method.java:511)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at dalvik.system.NativeStart.main(Native Method)
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: android.view.InflateException: Binary XML file line #310: Error inflating class <unknown>
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.Activity.setContentView(Activity.java:1835)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at com.Company.productname.myActivity.onCreate(TelaCaminhao.java:41)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.Activity.performCreate(Activity.java:4465)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-28 17:44:54.154: E/AndroidRuntime(31664):    ... 11 more
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: java.lang.reflect.InvocationTargetException
05-28 17:44:54.154: E/AndroidRuntime(31664):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
05-28 17:44:54.154: E/AndroidRuntime(31664):    ... 25 more
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-ldpi/borderless_button_bg.xml from drawable resource ID #0x7f020000
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.content.res.Resources.loadDrawable(Resources.java:1958)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.view.View.<init>(View.java:2832)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.widget.TextView.<init>(TextView.java:558)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.widget.Button.<init>(Button.java:108)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.widget.Button.<init>(Button.java:104)
05-28 17:44:54.154: E/AndroidRuntime(31664):    ... 28 more
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: <item> tag requires a 'drawable' attribute or child tag defining a drawable
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
05-28 17:44:54.154: E/AndroidRuntime(31664):    at android.content.res.Resources.loadDrawable(Resources.java:1955)
05-28 17:44:54.154: E/AndroidRuntime(31664):    ... 33 more

So, my question is: I need to use borderless buttons AND I need to make clear when the button is disabled. What alternatives do I have?

Community
  • 1
  • 1
Math
  • 3,334
  • 4
  • 36
  • 51
  • This is the root cause: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: tag requires a 'drawable' attribute or child tag defining a drawable. It seems to not like the – Torp May 29 '13 at 13:16
  • Yes, I can set the `?android:attr` in the `android:background` property, but I can't set it in the `android:drawable`. This is killing my idea to set a custom background image to a borderless button when it is disabled. – Math May 29 '13 at 13:23

1 Answers1

0

This was how I managed to solve my problem:

public class MyScreen extends Activity {
    private Button btnNext;
    private Drawable background;

    btnNext = (Button) this.findViewById(R.id.btnNext);
    background = btnNext.getBackground(); //stores the original Drawable characteristic

    ...

    //disable button
    if (!canGoNext) {
        btnNext.setEnabled(false);
        btnNext.setBackgroundResource(R.drawable.gray); //make it gray
    }
    //enable button
    else {
        btnNext.setEnabled(true);
        btnNext.setBackgroundDrawable(background); //make it selectableItemBackground again
    }
}
Math
  • 3,334
  • 4
  • 36
  • 51