How to change or fill color of shape inside layer-list

639 Views Asked by At

I have tried using the below solution but it didn't worked

https://stackoverflow.com/questions/32163918/programmatically-change-color-of-shape-in-layer-list

Here is my code...

Rolling.java

    package com.example.app1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;

public class Rolling extends AppCompatActivity {

    private LayerDrawable layerDrawable;

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

        LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(Rolling.this,R.drawable.dice1);
     /*   GradientDrawable outer = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.drawable.dice1);
        outer.setColor(Color.BLACK);*/
        shape.setTint(Color.BLUE);

    }
}

activity_rolling.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".Rolling">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="44dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/dice1" />
</androidx.constraintlayout.widget.ConstraintLayout>

dice1.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingRight="50dp" android:paddingLeft="50dp">
    <!-- Larger blue circle -->
    <item
        android:id="@+id/dice1"
        android:bottom="214dp"
        android:top="214dp"
        android:left="100dp"
        android:right="100dp"
        android:gravity="center"
        android:tileMode="repeat"
        >
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
            <stroke android:width="2dp" android:color="#ff207d94" />
            <padding android:left="2dp"
                android:top="2dp"
                android:right="2dp"
                android:bottom="2dp" />
            <size android:width="100dp" android:height="100dp"/>
            <corners android:radius="5dp" />
            <solid android:color="#ffffffff" />
        </shape>
    </item>
   
</layer-list>

So I want to change the color of square shape programmatically So how can I achieve that?

3

There are 3 best solutions below

0
snachmsm On

inside Rolling extends AppCompatActivity you are obtaining new LayerDrawable called shape, but this is "new instance" of this image, you have to set it for your ImageView then

ImageView iv = findViewById(R.id.imageView);
iv.setImageDrawable(shape);

resources are "untouchable" - if you obtain some drawable programmaticaly and make some changes (e.g. set color filter or tint) this doesn't mean that resource/drawable changed. any other further getDrawable or app:srcCompat will fetch image as original without your changes

0
Darkman On

I don't have enough reputation to comment so I'll post this as an answer. You could try one of these.

shape.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);

or

shape.setColorFilter(Color.BLUE, PorterDuff.Mode.DST_IN);

Don't forget to invalidate/refresh the drawable.

shape.invalidate()
1
Muhammad Awais On

Below code, to change the solid and stroke color of the drawable.

ImageView imageView = findViewById(R.id.imageView);
GradientDrawable imageViewDrawable = (GradientDrawable) imageView.getBackground();
imageViewDrawable.setStroke(<border_width_int>, <border_color>)
imageViewDrawable.setColor(<btn_color>)