How to set the favorite star icon to ON when clicked and OFF when again clicked in android

android My Activity that has list of quotes is as follow.

package bank.quotes.famous;
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.CheckBox;

public class QuotesActivity extends FamousQuotesActivity  { 
     @Override 
     public void onCreate(Bundle savedInstanceState)  { 

         super.onCreate(savedInstanceState); 
         setContentView(R.layout.quoteslayout); 

         CheckBox favButton = (CheckBox)findViewById( R.id.favbutton );

I have tried adding your code here with no success. My xml is as below, >

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
        android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="856dp"
        android:background="@android:color/white"
        android:orientation="vertical" >

        <TableRow
            android:id="@+id/TableRow03"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25" >

            <RelativeLayout
                android:id="@+id/RelativeLayout08"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/forquotesgrey" >



                <TextView
                    android:id="@+id/TextView22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="@string/albertquote"
                    android:textAppearance="?android:attr/textAppearanceMedium" />


                <TextView
                    android:id="@+id/TextView21"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:text="@string/alberteinstein"
                    android:textAppearance="?android:attr/textAppearanceMedium" />


                <CheckBox
                    android:id="@+id/favbutton"
                    style="?android:attr/starStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/TextView21"
                    android:layout_alignParentRight="true" />

            </RelativeLayout>
        </TableRow>

        <TableRow
            android:id="@+id/TableRow06"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25" >

            <RelativeLayout
                android:id="@+id/RelativeLayout10"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/forquotesgrey" >


                <TextView
                    android:id="@+id/TextView16"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="@string/stevequote"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@android:color/black" />


                <TextView
                    android:id="@+id/TextView15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:text="@string/stevejob"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@android:color/black" />

                <CheckBox
                    android:id="@+id/CheckBox01"
                    style="?android:attr/starStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true" />

            </RelativeLayout>
        </TableRow>

If a user likes a quote then, it should go to Favorite activity. Also the fav button should always glow if is user favorite. This is how favorite button works, isn't it? This my first time adding a favorite button so I have less idea of doing it. If you could help me I would really appreciate that. Thank You.


If you want to use stateful drawables you'll find it easier to skin a ToggleButton or CheckBox than an ImageButton.

<CheckBox android:id="@+id/star" 
    style="?android:attr/starStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />