How to change the shape stroke color android programmatically

To set the shape stroke color programmatically this example uses GradientDrawable method setStroke(int width, int color) which sets the stroke width and change shape's color. Here default orange color of the shape drawable is changes programetically usingb GradientDrawable method setStroke(int width, int color).

GradientDrawable

A GradientDrawable is Drawable with a color gradient for buttons, backgrounds, etc.
It can be defined in an XML file with the <shape> element. For more information, see GradientDrawable.

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

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

    <stroke
        android:width="3dp"
        android:color="@android:color/holo_orange_dark" />

    <corners
    android:bottomRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"/>
</shape>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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"
    android:gravity="center_horizontal"
    tools:context="quizcounter.compete.geeks.vectordrawablecolorchange.MainActivity">

    <Button
        android:layout_marginTop="10dp"
        android:id="@+id/button"
        android:text="Change Stroke Color"
        android:layout_width="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView "
        android:padding="10dp"
        android:textColor="@android:color/black"
        android:background="@drawable/stroke_background"
         />

</LinearLayout>

MainActivity.java
package quizcounter.compete.geeks.shapestrokecolorchange;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView=findViewById(R.id.text);
        button=findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //To change the stroke color
                GradientDrawable myGrad = (GradientDrawable)textView.getBackground();
                myGrad.setStroke(convertDpToPx(3), Color.RED);
            }
        });
    }

    private int convertDpToPx(int dp){
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }
}



2 comments:

  1. android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.GradientDrawable

    ReplyDelete
    Replies
    1. @JoseGrande Can you share your xml drawable file ?

      Delete

Popular Posts