Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the solid color programmatically of a shape in Android?

I'm trying to change the solid color of a shape in my Fragment.

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

I can't seem to access it from my code and I already have tried the following answers on stack overflow:

Set android shape color programmatically

How to change shape color dynamically?

Change shape solid color at runtime inside Drawable xml used as background

None of these worked or were pretty outdated.

Fragment where I try to change the color:

public class GameFragment extends Fragment {
    private Theme currentTheme;
    private Circle currentCircle;
    private int currentOrganisationId;
    private List<Card> circleCards;
    private List<ImageButton> circleButtons;
    private int viewHeight;
    private int viewWidth;
    private int marginCard;
    private int[] tableStarts;
    private RelativeLayout background;
    private ViewTreeObserver vto;

    public GameFragment() {
        circleCards = new ArrayList<>();
        circleButtons = new ArrayList<>();
        tableStarts = new int[9];
    }

    //...

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_game, container, false);
        ...
        background = setBackground(rootView);
        circleCards = currentCircle.getCards();

        return rootView;
    }

    //...

    private void drawCards() {
        circleButtons = new ArrayList<>();
        for (Card card : circleCards) {
            ImageButton myImageButton = new ImageButton(this.getContext()); //generate ImageButton
            myImageButton.setId(card.getCardId()); //Set Id of button
            myImageButton.setBackgroundResource(R.drawable.circle_card_icon);

            //set random color to shape
            ...
        }
    }
}
like image 336
Edward Avatar asked Mar 19 '16 14:03

Edward


People also ask

How to set color of shape in xml android?

In the shape drawable xml-file you can set custom solid color with the attribute <solid android:color="@android:color/holo_purple" /> but to change the shape drawable solid color in runtime/dynamically.

How to change color of drawable shape in android?

To change the background color programatically, try to get the shape drawable and then use SetTint to change the color value. Check the code: //layout.

What is gradient drawable?

android.graphics.drawable.GradientDrawable. A 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 the guide to Drawable Resources.


1 Answers

use this:

GradientDrawable bgShape = (GradientDrawable)view.getBackground().getCurrent();
bgShape.setColor(Color.BLACK);

.getCurrent give the selected drawable layerlist

use this it will not throw java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ShapeDrawable exception.

it works perfectly for me.

like image 136
Manmohan Avatar answered Nov 15 '22 03:11

Manmohan