How to set the icon of a favorite star to the "ON" position when pressed and turned off when you press the android again

My activity, having a list of quotes, is as follows.

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 tried adding your code here without success. My xml is 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 the user likes the quote, then he should go to "Favorite Activity". Also, the fav button should always light if the user is a favorite. That's how a favorite button works, right? This is my first favorite button addition, so I don’t know how to do it. If you could help me, I would really appreciate it. Thank.

+5
source share
1 answer

, ToggleButton CheckBox, ImageButton.

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

All Articles