Charlie-Blake
Charlie-Blake

Reputation: 11050

StateListDrawable behaving strange

I have this StateListDrawable:

GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {
            firstColor, secondColor });
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_pressed },
            new ColorDrawable(onClickColor));
sld.addState(StateSet.WILD_CARD, gd);

This is my Layout:

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

    <RelativeLayout
        android:id="@+id/actionBar"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:baselineAligned="true" 
        android:descendantFocusability="afterDescendants">

        <ImageButton
            android:id="@+id/actionButtonBack"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />

        <ImageButton
            android:id="@+id/actionButton3"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/actionButton2" />

        <ImageButton
            android:id="@+id/actionButton2"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_toLeftOf="@+id/actionButton1" />

        <ProgressBar
            android:id="@+id/actionButton1"
            android:layout_width="48dip"
            android:layout_height="48dip"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:padding="4dip" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="48dip"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_toLeftOf="@+id/actionButton3"
            android:layout_toRightOf="@+id/actionButtonBack"
            android:gravity="center"
            android:textSize="14dip"
            android:textStyle="bold" />
    </RelativeLayout>

</merge>

And then I set the drawable like this:

tv.setClickable(false);
tv.setTextColor(textViewColor);
actionBack.setBackgroundDrawable(sld);
action2.setBackgroundDrawable(sld);
action3.setBackgroundDrawable(sld);
tv.setBackgroundDrawable(sld);
pb.setBackgroundDrawable(sld);

When I click in the actionBack button, the whole layout becomes the color I have as onClickButton.

I just want it to behave as if I had pressed a regular-xml-defined button drawable.

What am I doing wrong?

Upvotes: 0

Views: 488

Answers (1)

Hassan Jawed
Hassan Jawed

Reputation: 1650

It is because the layout_width of the 'tv' (TextView with id android:id="@+id/tvTitle") is 'wrap_content' so when a color is applied to this view it takes the whole boundary you either need to hardcode the layout_width of TextView or apply color as ShapeDrawable with hardcoded width. Hope this will solve your problem.

Upvotes: 1

Related Questions