android shape xml rotated drawable change color programmatically
Asked Answered
R

3

3

This is a xml for triangle shape:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/shape_id">
        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%" >
            <shape android:shape="rectangle" >
                <stroke android:width="10dp"/>
            </shape>
        </rotate>
    </item>
</layer-list>

And this is a background of a textview

<TextView
    android:id="@+id/headlineSelect_TXT2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:background="@drawable/category_triangle_shape1"
    android:visibility="invisible" />

And I want to change color of shape programmatically. I tried this but I am getting null pointer exception

LayerDrawable bgDrawable = (LayerDrawable) getActivity()
    .getResources()
    .getDrawable(R.drawable.category_triangle_shape1);
final GradientDrawable shape = (GradientDrawable) bgDrawable
    .findDrawableByLayerId(R.id.shape_id);
shape.setStroke(10,Color.GREEN);

How can I do that? Thanks for help.

Ramie answered 24/2, 2014 at 23:39 Comment(0)
I
15

This is somewhat tricky and involves a lot of casts:

TextView view = (TextView) findViewById( R.id.my_text_view );

// Get the drawable from the view, if you're using an imageview src
// element you'll go with view.getDrawable()
LayerDrawable layers = (LayerDrawable) view.getBackground();

// Now get your shape by selecting the id
RotateDrawable rotate = (RotateDrawable) layers.findDrawableByLayerId( R.id.shape_id );

// Finally you can access the underlying shape
GradientDrawable drawable = (GradientDrawable) rotate.getDrawable();

// ... and do you fancy things
drawable.setColor( ... );
Incommutable answered 4/8, 2014 at 12:25 Comment(1)
Thanks @Incommutable for your precious help this thing works fine and awesome and i am able to use it in my project github.com/harsh159357/InstaTag/commit/…Jasun
P
0

Forget about XML and do it by code like that:

   TextView txtView = (TextView)findViewById(R.id. headlineSelect_TXT2);
    ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient lg = new LinearGradient(0, 0, 0, txtView.getHeight(),
                new int[] { 
                    Color.LIGHT_GREEN, 
                    Color.WHITE, 
                    Color.MID_GREEN, 
                    Color.DARK_GREEN },  
                new float[] {
                    0, 0.45f, 0.55f, 1 },
                Shader.TileMode.REPEAT);
             return lg;
        }
    };
    PaintDrawable p = new PaintDrawable();
    p.setShape(new RectShape());
    p.setShaderFactory(sf);
    txtView.setBackgroundDrawable((Drawable)p);
Pollinize answered 25/2, 2014 at 0:4 Comment(1)
Thanks your answer but this is a rectangle shape. I need a triangle.Ramie
M
0

If you have RotateDrawable in your xml file inside res/drawable then you could try the below code to change the color of rotate drawable.

LayerDrawable layers = (LayerDrawable) getResources().getDrawable(R.drawable.triangle);
RotateDrawable rotateShape = (RotateDrawable) (layers.findDrawableByLayerId(R.id.user_color));
GradientDrawable shape = (GradientDrawable) rotateShape.getDrawable();
shape.setColor(Color.parseColor("#FF0000"));
TextView NotchTV = (TextView) view.findViewById(R.id.bubble_notch);
NotchTV.setBackgroundDrawable(layers);

`

Martin answered 21/1, 2016 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.