Ok guys, this is something very very stupid but I just can't figure it out.
I'm trying to change the color of a Handmade circle.xml class but I just can't, and I can't understand the reason behind.
I'm pretty sure I'm using a wrong method to do it.
Here's some code. It's a onClick() method which should change the color of my circle drawable "button".
Circle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#e42828"/>
<stroke android:color="#3b91d7" android:width="0dp"/>
<size android:width="250dp" android:height="250dp"/>
</shape>
</item>
</selector>
Activity.java
recplay_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GradientDrawable background = (GradientDrawable)recplay_button.getBackground();
background.setColor(getResources().getColor(R.color.accent));
}
});
Ok, here's the exception:
03-16 22:09:46.546 18305-18305/com.example.cesarsk.say_it E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cesarsk.say_it, PID: 18305
java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.GradientDrawable
at com.example.cesarsk.say_it.PlayActivity$3.onClick(PlayActivity.java:160)
at android.view.View.performClick(View.java:5669)
at android.view.View$PerformClick.run(View.java:22539)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Xml where it's defined my Button:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/buttons_bar">
<Button
android:id="@+id/recplay_button"
android:layout_width="96dp"
android:layout_height="96dp"
android:background="@drawable/circle"
android:backgroundTint="@color/primary_dark"
android:layout_centerInParent="false"
/>
</RelativeLayout>
I suppose that the reason why it doesn't work it's related to the fact that I created the Shape manually so I don't have a way to interface myself with it.
At first, I tried to do
`View v = findViewById(R.id.rect4);
v.setBackground(R.drawable.rectangle);
and the color changed but it also changed the Shape of my Shape, so I can't use it, or I should use it in a different way.
Thank you all.