-1

I am using the following code to make a star button:

<Button
    android:id="@+id/leftButton"

    android:onClick = "Star"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:Color="#FFFF00"   //I tried adding this, but no luck :(
    android:layout_alignParentTop="true"
    android:background="@android:drawable/btn_star" />

The button currently looks white:

enter image description here

Is there anyway I can make it yellow?

I know I can't have two background attributes, but is there anything I can do to change the color of this star? I want to keep the star as a background attribute, rather than a source attribute, because I want to be able to change the size of it.

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

4 Answers4

2

If you want to yellow the star icon, then you can use ColorFiler for this. As follows:

Drawable drawable = getResources().getDrawable(android.R.drawable.star);
drawable.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);

Hope this helps.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
0

Not directly. You can add different colorized images and change them at runtime, but much better way is to use a xml drawable. For icons with xml files take a look at https://materialdesignicons.com (there are much more sites like that outside)

In the xml file you can change the color directly

Marco Rehmer
  • 1,033
  • 2
  • 12
  • 32
0

Your xml file.

 <Button
        android:id="@+id/leftButton"
        android:tint="@color/yellow_tint"
        android:onClick = "Star"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:background="@android:drawable/btn_star" />

colors.xml

<resources>

 <color name="yellow_tint">#FFFF00</color>

</resources>

Try it and let me know. Refer to this: Working with Drawables. I'm afraid android:color won't work in Drawables.

Anirudh Murali
  • 612
  • 10
  • 25
0

(background_star.xml) on drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/star_yellow" <!-- image yellow -->
        android:state_pressed="true" />
    <item android:drawable="@drawable/star_normal" /> <!-- image normal-->
</selector>

on layout file:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancel alarm"
        android:id="@+id/button2"
        android:background="@drawable/background_star" <!-- change this -->
        android:layout_centerHorizontal="true" />
Lucas
  • 431
  • 3
  • 7