2

I am trying to get a button custom attribute to return true/false using custom attributes defined in styles.xml. My example is trivial with only two buttons but I can't get it to work. My layout looks like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CustomAttr">

    <Button
        android:id="@+id/button_1"
        style="@style/red_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1!" />

    <Button
        android:id="@+id/button_2"
        style="@style/blue_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2!" />

</LinearLayout>
<resources>

styles.xml looks like:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <attr name="is_red" format="boolean"/>
    <style name="red_button" >
        <item name="android:background">#ffff0000</item>
        <item name="is_red">true</item>
    </style>
    <style name="blue_button" >
        <item name="android:background">#ff0000ff</item>
        <item name="is_red">false</item>
    </style>

</resources>

And the code looks like:

public class CustomAttr
        extends AppCompatActivity
        implements View.OnClickListener {
    private static final String TAG = 
         CustomAttr.class.getSimpleName();  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_attr);
        findViewById(R.id.button_1)
            .setOnClickListener(this);
        findViewById(R.id.button_2)
            .setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        int [] searchAttr = {R.attr.is_red};
        TypedArray attrs
            = v.getContext()
               .obtainStyledAttributes(null, searchAttr);
        boolean isRed = attrs.getBoolean(0, false);
        Log.d(TAG, String.format("%s:%b"
            , (id == R.id.button_1) ? "button 1" : "button 2"
            , isRed )
        );
    }
}

Everything compiles fine and the button colors are working and I'm not getting any warnings. But the boolean isRed in the onClick method always returns false.

I've been looking through the net and docs all day and this looks like it should work -- but it doesn't. What am I doing wrong?
Thanks
Steve S.

********* EDIT Fri Sep 21 10:17:01 PDT 2018 *********
As noted below, this is a prototype for an app with about 250 different buttons in a gridview. There are about 250 of basicly 4 different types and I can set almost everything in each button using one of 4 different styles. I was already using the tag and text fields and really needed a way to detect the button's type (1 0f 4). I finally created a custom button view with it's own attribute set. The code for the working prototype custom view button with its attribute on github. Thanks!
Steve S.

1 Answer 1

1

You haven't used the is_red attribute in your xml. Something like this:

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

    xmlns:custom="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".CustomAttr">

    <Button
        android:id="@+id/button_1"

        custom:is_red="true"

        style="@style/red_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1!" />

    <Button
        android:id="@+id/button_2"

        custom:is_red="false"

        style="@style/blue_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2!" />

</LinearLayout>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @nupadhyaya. The actual app has about 250 buttons of 3 different types. My goal is to set a value in each of the buttons depending on its type (style). Here for example I set the background on all the "red" buttons to red. I'd like to be able to test in the code "if (is_red) { do this...}" to determine an action based on the button's style. I may have to do a custom button(s) but I am hoping to avoid that.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.