Damir
Damir

Reputation: 1

Android : How to change a value into a drawable xml file?

I have this code into a drawable xml file:

 <?xml version="1.0" encoding="utf-8"?>
<!-- Background circle for the magnitude value -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="@color/magnitude1" />
    <size
        android:width="36dp"
        android:height="36dp" />
    <corners android:radius="18dp" />
</shape>

And I wanted to change programmatically the value of the solid-->color field magnitude1 by another one

I tried access to by using

 Resources res = this.getContext().getResources();
    myShape = res.getDrawable(magnitude_circle);

But Can't achieve to change magnitude1 by magnitude2 for example

Any idea ?

Regards,

Dam

Upvotes: 0

Views: 1662

Answers (2)

Mukesh Y.
Mukesh Y.

Reputation: 949

1.First select the layout where you have used

View mainContainer = findViewById(R.id.main_container);

2.Then select the background drawable of that container

GradientDrawable bgDrawable = (GradientDrawable) mainContainer.getBackground();

4. Finally change the color of drawable resource

shape.setColor(ContextCompat.getColor(this, R.color.magnitude2));

Upvotes: 0

Murtadha S.
Murtadha S.

Reputation: 471

You should use R.drawable.magnitude_circle instead of only magnitude_circle in :

 Resources res = this.getContext().getResources();
    myShape = res.getDrawable(magnitude_circle);

Upvotes: 1

Related Questions