i'm making recipe application , create favourite button user click on grey star button , change yellow star button(i have picture of grey star , yellow star), selected favourites display in list once click on favourites link on home screen
i have following code in layout
<imagebutton android:id="@+id/favouritebtn" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/staroff" android:background="#00ffffff" android:onclick="ontogglestar" android:clickable="true"/>
can me in how write code when click on star turn yellow , add favourite list
update
favouritebtn.java
import android.app.activity; import android.content.context; import android.content.sharedpreferences; import android.os.bundle; import android.view.view; import android.widget.imagebutton; public class favouritebtn extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.recipe); final imagebutton imgbutton =(imagebutton)findviewbyid(r.id.favbtn); imgbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { boolean isfavourite = readstate(); if (isfavourite) { imgbutton.setbackgroundresource(r.drawable.staroff); isfavourite = false; savestate(isfavourite); } else { imgbutton.setbackgroundresource(r.drawable.staron); isfavourite = true; savestate(isfavourite); } } }); } private void savestate(boolean isfavourite) { sharedpreferences asharedpreferences = this.getsharedpreferences( "favourite", context.mode_private); sharedpreferences.editor asharedpreferencesedit = asharedpreferences .edit(); asharedpreferencesedit.putboolean("state", isfavourite); asharedpreferencesedit.commit(); } private boolean readstate() { sharedpreferences asharedpreferences = this.getsharedpreferences( "favourite", context.mode_private); return asharedpreferences.getboolean("state", true); } }
recipe.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/jbsbackground2" android:orientation="vertical" > <imageview android:id="@+id/iv_detail" android:layout_width="fill_parent" android:layout_height="150dp" android:scaletype="centercrop" android:src="@drawable/barbecuedporkribs" /> <button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="10dp" android:layout_below="@+id/iv_detail" android:background="#3d3c3a" /> <scrollview android:id="@+id/scrollview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/button1" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/iv_detail" android:layout_margintop="5dp" android:text="recipe" android:layout_toleftof="@id/favourites" android:textcolor="#000000" android:textappearance="?android:attr/textappearancelarge" android:textstyle="bold" /> <textview android:id="@+id/tvname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview2" android:layout_margintop="2dp" android:text="textview" android:textcolor="#000000" android:textappearance="?android:attr/textappearancemedium" android:textstyle="italic" /> <textview android:id="@+id/tvfavourite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add favourites (click star)" android:textcolor="#000000" android:textappearance="?android:attr/textappearancemedium" android:textstyle="bold" android:layout_above="@+id/tvname" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <imagebutton android:id="@+id/favbtn" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/staroff" android:background="#00ffffff" android:onclick="onclick" android:clickable="true"/> <textview android:id="@+id/tvtd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ingredients" android:textcolor="#000000" android:textappearance="?android:attr/textappearancelarge" android:textstyle="bold" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <textview android:id="@+id/tvingredients" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/tvtd" android:layout_margintop="2dp" android:text="textview" android:textcolor="#000000" android:textappearance="?android:attr/textappearancemedium" android:textstyle="italic" /> <textview android:id="@+id/tvk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/tvingredients" android:layout_margintop="5dp" android:text="preparation" android:textcolor="#000000" android:textappearance="?android:attr/textappearancelarge" android:textstyle="bold" /> <textview android:id="@+id/tvpreparation" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/tvk" android:layout_margintop="2dp" android:text="textview" android:textcolor="#000000" android:textappearance="?android:attr/textappearancemedium" android:textstyle="italic" /> </linearlayout> </scrollview>
use togglebutton in xml:
<togglebutton android:layout_width="180dp" android:layout_height="180dp" android:id="@+id/mytogglebutton" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:texton="" android:textoff=""/>
and in activity:
togglebutton togglebutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); togglebutton = (togglebutton) findviewbyid(r.id.mytogglebutton); togglebutton.setchecked(false); togglebutton.setbackgrounddrawable(contextcompat.getdrawable(getapplicationcontext(), r.drawable.img_star_grey)); togglebutton.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) togglebutton.setbackgrounddrawable(contextcompat.getdrawable(getapplicationcontext(),r.drawable.img_star_yellow)); else togglebutton.setbackgrounddrawable(contextcompat.getdrawable(getapplicationcontext(), r.drawable.img_star_grey)); } }); }
Comments
Post a Comment