How to Create Your Android Favourite Button Using Toggle
Want to create your own favourite button in any app? It's pretty simple and learning. Using toggle button you can do so. With a toggle button, one can easily change its appearance between states. Using setChecked() method of toggle button smartly, you can create your button anywhere on your Android phone. 

The favorite feature is mostly used when one want to shortlist items in a list. But not only this, you can use this tip anywhere on apps. Using android.widget.Button class you can display a normal button.

You may want to keep your toggle button always ON or OFF by default. Use the following toggle as per your need. Understand toggle button working on the android developer site.



Now use getView() method and set the tag to denote not-Favorited mode and then set the setChecked to false. Also set onClickListener to it. 

You can also use CompoundButton.setChecked() or CompoundButton.toggle() method.

favt.setTag(, "white"); // Items which are not clicked yet.


You can learn how to set selected items red using setChecked method in this article (customizing android buttons using selectors)

if(checkFavorite(current))   // Basic method if the item already exists in the database

   favt.setChecked(true);    // To dispaly different icon if item is already shortlisted.
   favt.setTag(, "red");   

If you want to save the state preferences, use these codes

ToggleButton tgbutton;

tgbutton = (ToggleButton) findViewById(;
tgbutton.setOnClickListener(new ToggleButton.OnClickListener() {

    public void onClick(View v) {
        SharedPreferences sharedPreferences = PreferenceManager
        Editor editor = sharedPreferences.edit();
        editor.putBoolean("toggleButton", tgbutton.isChecked());

Now the trick part, if already favoured item is clicked, it must become un-favorite and vice-versa.

public void onClick(View view) {

  // TODO Auto-generated method stub
  ToggleButton favt = (ToggleButton) view.findViewById(;
  String color = (String) view.getTag(; //Red = Favorited
  RowData fav = (RowData) getItem(pos);
   favt.setTag(, "red");
   favt.setTag(, "white");

Now you must have clear idea to create your own android favorite button. If you want to learn how to create android buttons using SQL professionally, see this great article here.

You can also follow this youtube guide to know how to create an android button from start to finish. 

You may also like "Modify Android Lock Screen: Get Rid of Annoying Carrier Logo"

Want an Animated Star like favorite button? See this Github article or android-arsenal site. And if you are an android app developer, you might also like a StackOverflow discussion here.

1 comment:

No more post found.

Choozurmobile - Shop Some of the Best Products Online Copyright © 2020 | Privacy Policy | Disclaimer | TOS