Articles: New How to Create Your Android Favourite Button Using Toggle By Patricbensen
Full Link : New How to Create Your Android Favourite Button Using Toggle By Patricbensen
Artikel Android, Artikel android 2.2, Artikel android 2.3, Artikel android developer, Artikel android operating system, Artikel android sdk, Artikel android update, Artikel android widgets, Artikel best android apps, Artikel droid, Artikel google android, Artikel google play, Artikel latest android, Artikel what is android,
New How to Create Your Android Favourite Button Using Toggle By Patricbensen
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.
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.
Information New How to Create Your Android Favourite Button Using Toggle By Patricbensen has finished we discussed
says the article New How to Create Your Android Favourite Button Using Toggle By Patricbensen, we beharap be useful to you in finding repensi about the latest gadgets and long.
Information on New How to Create Your Android Favourite Button Using Toggle By Patricbensen has finished you read this article and have the link https://patricbensen.blogspot.com/2016/06/new-how-to-create-your-android.html Hopefully the info we deliver is able to address the information needs of the present.
0 Response to "New How to Create Your Android Favourite Button Using Toggle By Patricbensen"
Post a Comment