0

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

Bombolo
  • 749
  • 2
  • 8
  • 19

2 Answers2

1

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.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • Thanks for the answers. Unfortunatly, this is my first app, so i didn't understand so much :( i tried to search some example on the web but i didn't find what i looking for.. but is something...thanks – Bombolo Aug 10 '13 at 16:03
  • There are three search queries you can google for, where you get really a ton of tutorials to make your App happen: DialogFragment, SharedPreferences and Adding Views Programmatically. And you can always use SO if you stuck. – Steve Benett Aug 10 '13 at 16:05
  • Ops, i did i mistake! what i want is to add by long press, the button APPLE HISTORY inside the FAVORITES activity,so when the user is in FAVORITES, it shows the button APPLE HISTORY. I found something on the web about SharedPreferences, but until now i have just found how to import data, nothing about how to import views. – Bombolo Aug 10 '13 at 16:24
  • You can only save primitive types on the SharedPreferences, but no View or Object. How I said you have to save a boolean or something in the SharedPreferences to indicate the users choice. You have to react on that boolean to show the View. – Steve Benett Aug 10 '13 at 16:42
  • mmmmm, PANIC! so is not possible save the button with his graphic layout? – Bombolo Aug 10 '13 at 16:54
  • Consider to open a new question about how to handle this special situation. Maybe you will get an answer which you can handle better. – Steve Benett Aug 10 '13 at 17:41
0

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

Community
  • 1
  • 1
Waza_Be
  • 39,407
  • 49
  • 186
  • 260