Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Star Button in Android

I'd like to put one star-shaped button in my app, just like a rating star. I've already tried to use a one star rating bar, but unfortunately it doesn't work as a button.

I'd like it to work as a button, even better if the background of the selected state is yellow...any suggestions? Thanks.

like image 212
user1012480 Avatar asked Nov 23 '11 14:11

user1012480


People also ask

How do you make a star on Android?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken imageview and added background as background.

What are Android buttons?

In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it.

What is image button?

Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regular Button , with the standard button background that changes color during different button states.


1 Answers

Sure, use a layout resource like this:

<ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/star"
        android:background="#00ffffff"
        android:onClick="onToggleStar"/>

You can pair this with a state list drawable defined like this that is saved as an XML file at res/drawable/star.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

Here are the images that pack with Android itself:

  • Off Off
  • On On
  • Disabled Disabled
  • On Pressed On Pressed
  • Off Pressed Off Pressed
like image 75
Jonathan Schneider Avatar answered Sep 20 '22 10:09

Jonathan Schneider