Question: I am setting background of a textview dynamically, based on some conditions as 

```
textview.setBackgroundResource(R.drawable.attempted_question_border);

```

OR

```
textview.setBackgroundResource(R.drawable.skipped_question_background);

```

Xml for background is 

```
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid
        android:color="@color/answered_que_bg" >
    </solid>
    <stroke
        android:width="1dp"
        android:color="@color/answered_que_bg" >
    </stroke>
    <corners
        android:radius="2dp">
    </corners>
</shape>

```

It sets background color 'answered\_que\_bg' and border color 'answered\_que\_bg' OR background color 'skipped\_question\_background' and border color 'skipped\_question\_background'. So far so good. Now I need to **change only border color** of this textview **keeping background color same** as it had. I tried with changing background with below xml. 

```
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke
        android:width="1dp"
        android:color="@color/bookmark_color" >
    </stroke>
    <corners
        android:radius="2dp">
    </corners>
</shape>

```

It changes border color as desired but background color is also lost.

Answer: 
> 
> **Take your drawable xml with layer list like that**
> 
> 
> 

```
<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/shape">
    <shape
    android:shape="rectangle" >

    <solid
        android:color="@color/colorPrimary" >
    </solid>

    <stroke
        android:width="1dp"
        android:color="@color/colorPrimary" >
    </stroke>

    <corners
        android:radius="2dp">
    </corners>

</shape>
</item>
    </layer-list>

```

> 
> **code pro-grammatically change stroke also the background**
> 
> 
> 

```
      LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(demo.this,R.drawable.drawtext);
        GradientDrawable gradientDrawable = (GradientDrawable) shape.findDrawableByLayerId(R.id.shape);

        // if you want to change the color of background of textview dynamically
        //gradientDrawable.setColor(ContextCompat.getColor(demo.this,R.color.colorAccent));

       // This is mangage the storke width and it's color by this shape.setStroke(strokeWidth,color);
        gradientDrawable.setStroke(2,ContextCompat.getColor(demo.this,R.color.colorAccent));

        text2.setBackground(shape);

```

> 
> With your code without change color of stroke
> 
> 
> 

[![enter image description here](https://i.stack.imgur.com/kmQXh.png)](https://i.stack.imgur.com/kmQXh.png)

> 
> With My code change color of stroke and also you can change the background progrmatically in comment code
> 
> 
> 

[![enter image description here](https://i.stack.imgur.com/DO4nf.png)](https://i.stack.imgur.com/DO4nf.png)
I have done that by putting the TextView into a LinearLayout, where the TextView would be centered with certain margins on either side, then I'd change the background color of the LinearLayout, without changing the background color of the textview, and then it'd appear as if it's border color has changed.