1

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.

Cesarsk
  • 153
  • 1
  • 2
  • 14

2 Answers2

0

Try to replace the code in Your onClickListner with just

recplay_button.setBackground(ContextCompat.getDrawable(context, R.drawable.Circle));
Mo Hajr
  • 1,253
  • 1
  • 15
  • 31
0

if you use api 23> getColor() is deprecated.Instead there is getColor(int id,Theme theme). Or in your case ContextCompat.getColor(context, R.color.color_name) Also you did not mention if you want the color to change onClick, because if you dont setOnClickListener is not necessary

Final answer even though getColor is deprecated it will works in your code if you just correct this

GradientDrawable background =(GradientDrawable)button1.getBackground().getCurrent();

It works for me

Adeel
  • 2,901
  • 7
  • 24
  • 34
Thanasis Saxanidis
  • 141
  • 1
  • 2
  • 12
  • Yes I want to do it onClick. What I plan is actually making an animation to change the color from, for example, Red to Blue in 300 ms – Cesarsk Mar 16 '17 at 23:16
  • in your example try this GradientDrawable background = (GradientDrawable)button1.getBackground().getCurrent(); background.setColor(Color.BLACK); In setColor you can use the color you want but if you use getColor have in mind that this is the correct use getColor(int id,Theme theme) – Thanasis Saxanidis Mar 16 '17 at 23:55
  • Also i assume that you have declare button previously in your code because if you just use the id from the xml it wont work – Thanasis Saxanidis Mar 16 '17 at 23:59
  • That's true, I already did it. I just wanted to give a clean and a summarized code to let you quickly understand my case. I'll try that and I'll let you know. Thank you for your help :) – Cesarsk Mar 17 '17 at 00:06
  • try what i wrote above it probably solves your issue – Thanasis Saxanidis Mar 17 '17 at 00:10
  • Tried that in my onClick method. It doesn't work. That's what I did. `recplay_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("DEBUG","CLICKED"); GradientDrawable background = (GradientDrawable)recplay_button.getBackground().getCurrent(); background.setColor(Color.BLACK); } });` – Cesarsk Mar 17 '17 at 09:37
  • i used api25 with this simple code and works perfect , if you want to change the color of the circle you made . Code here: final Button button1 = (Button) findViewById(R.id.recplay_button); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GradientDrawable background = (GradientDrawable)button1.getBackground().getCurrent(); background.setColor(getResources().getColor(R.color.colorAccent)); } }); – Thanasis Saxanidis Mar 17 '17 at 13:20
  • check your code before . Fatal eror means you have done wrong something before. I used your xml files as given and does not seam to have an issue , so probably in your java code – Thanasis Saxanidis Mar 17 '17 at 13:22
  • My code before doesn't work because I didn't call `.getCurrent()` on Background. I tried also calling it but still it doesn't work. Now I'm trying to figure it out why the code you sent me in your previous comment it does work for you while it doesn't for me. I actually click on the button but it doesn't change its color, it remains the same as before, though I checked with the debugger and it enters in that method. – Cesarsk Mar 17 '17 at 13:30