makapaka
makapaka

Reputation: 179

Trying to setBackgroundColor to drawable in Adapter not working as expected

I have a linearlayout background set to a drawable file in order to create that rounded corner look as such:

<LinearLayout
    android:id="@+id/layout_top"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/rectangle_topcorner">

     <TextView android:layout_width="fill_parent"
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:text="Impact"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:layout_marginLeft="10dip"  />
</LinearLayout>

In my adapter, after I inflate the layout item, I am trying to change the backgroundColor to a 'different' drawable, in order to change the background color:

LinearLayout top = (LinearLayout) view.findViewById(R.id.layout_top);
top.setBackgroundColor(R.drawable.rectangle_topcorner_high);

The problem is, after doing that, the rectangle loses its rounded corner look and its just a plain old square.

The 2 drawables: rectangle_topcorner

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:id="@+id/background_shape" >
<corners android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
<solid android:color="#005577"/>  
</shape>

rectangle_topcorner_high

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:id="@+id/background_shape" >
<corners android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
<solid android:color="#83162D"/>  
</shape>

I'm missing something to preserve the rounded corners ?

Upvotes: 0

Views: 783

Answers (2)

makapaka
makapaka

Reputation: 179

Thought I would share my eventual fix - As I was trying variants of

top.setBackgroundColor(R.drawable.rectangle_topcorner_high);

The correct (working) code is:

top.setBackground(context.getResources().getDrawable(R.drawable.rectangle_topcorner_high));

Upvotes: 0

royB
royB

Reputation: 12977

Try using:

top.setBackgroundResource(R.drawable.rectangle_topcorner_high);

setBackgroundColor are for color resources

Upvotes: 1

Related Questions