Reputation: 749
i work on an app and i have this problem.
Example:
i have 4 buttons in the main page:
APPLE, STRAWBERRY, CHERRY, FAVORITES
if i click on APPLE STRAWBERRY OR CHERRY, it will open another activity with 3 more buttons
APPLE HISTORY, MADE APPLE JUICE, HOW TO GROW APPLE
Each of this buttons when clicked, open another activity with some data and text
I would like when someone made a long press on APPLE, STRAWBERRY or CHERRY to open a windows that ask if i want to save the selected item to FAVORITES, so it will copy the button inside the activity FAVORITES with the option to delete this choice in the future.
Can someone help me?
Thanks
Upvotes: 0
Views: 1304
Reputation: 12933
You can set a listener for a long click to handle it
yourButton.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Inside this listener you can open a Dialog
, where the user can choose if he wants to add it to Favorites or not. Furthermore save the users choice in SharedPreferences with a boolean
or something which indicates that he made his choice. In your Favorite Activity read the SharedPreferences file and handle accordingly.
This could be done by adding Views programatically to have a dynamic setup. Or if the repertory is limited, like your example, set the Visibility
of some Views / Buttons in your XML to gone and change it to Visible by reference to the users input.
Upvotes: 1
Reputation: 39538
You could save all your favorites items in a csv file, in a preference, in a xml file.. There are millions of way to achieve it.
I would personally use a sharedPreference to let the user edit it easily.
Is it possible to add an array or object to SharedPreferences on Android
Upvotes: 0