Pages

Drop Down MenusCSS Drop Down MenuPure CSS 

Dropdown Menu
Back to Menu

1 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.


android:textOn=""

android:textOff=""

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(R.id.color, "white"); // Items which are not clicked yet.

  favt.setChecked(false);
  favt.setOnClickListener(this);

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(R.id.color, "red");   
  }

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


ToggleButton tgbutton;

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

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

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(R.id.toggleButton1);
  String color = (String) view.getTag(R.id.color); //Red = Favorited
  RowData fav = (RowData) getItem(pos);
  if(color.equals("white"))
  {
   addFavorite(fav); 
   favt.setTag(R.id.color, "red");
  }
  else
  {
   removeFavorite(fav); 
   favt.setTag(R.id.color, "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.
How to Create Your Android Favourite Button Using Toggle Rating: 4.5 Diposkan Oleh: Choozurmobile