Issue
I'm developing an Android language dictionary app. I'm thinking of a Favourites button that works in two phases:
- a short click will add the currently viewed word into the Favourites list;
- and a long click will allow user to view the Favourites list (of added words).
I wonder if this is possible, and if yes could you please explain how to do it?
NB: Till now I've only succeeded in adding a Favourites image button to the app, and when short-clicked, it says: "Chosen Word Added to Favourites".
Thank you very much in advance.
Solution
Assuming, say, public class FavViewActivity extends ListActivity
, you just add the OnLongClickListener the same way as you added the OnClickListener:
btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite);
btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Add code here to save the favourite, e.g. in the db.
}
});
btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Open the favourite Activity, which in turn will fetch the saved favourites, to show them.
Intent intent = new Intent(getApplicationContext(), FavViewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
return false;
}
});
In your db, keep a favourite table listing the id:s of the words that are marked as favourites.
For creating a new Activity, like FavViewActivity, there are plenty of guides around.
For more help, please be more specific about what, and also add what you have tried so far. ^_^
Answered By - JOG