Varki
Varki

Reputation: 5

Change button image permanently

I have a TableLayout with a lot of buttons in each TableRow. In the XML, every button looks like this:

<Button
        android:id="@+id/a1"
        android:text="Button1" 
        android:textColor="#FFFFFF"
        android:textSize="10sp" 
        android:background="#000000"
        android:drawableTop="@drawable/image1" />

In my app, I've an option in the Activity, which is used to "add to favorites" the button. Question is, how can I change the button image permanently(even if the user leaves the app), until the user presses again the option ( remove from favorites)?. I guess I'll have to use sharedpreferences, but I can not figure out how to do it.

Thank you very much!

Upvotes: 0

Views: 591

Answers (2)

Raj Shah
Raj Shah

Reputation: 322

Shared Preference is to be used for obvious.

You can get tutorial on Shared Preference from here link1,link2,Developer Site

The thing you need to implement is as follow.

Store a string in shared preference. Name it buttonImageData. Using media store class find the _id of all the songs that are stored using MediaStore.Audio.Media._ID.

Store all this id in an integer array. Then when the application is close store _id of all the songs having mark sign in a string with" " (space) as seperator. Simple for loop and string append function will work out.

When the application is started again, get the string in sharedpreference. Use following kind of code to get the _id of all the songs

The string generated will be like buttonImageData="23 43 42 54 12 567 653 234 543" In android, Mediaprovider provides a database in which the song has unique and permament _id all time so keeping the track of _id would help in making app robust even if the name or any attributes of song file is changed.

StringTokenizer st = new StringTokenizer(buttonImageData);
     while (st.hasMoreTokens()) {
         array[i++]=Integer.parseInt(st.nextToken());
     } 

Now simple you have the array of all the _id of all the songs who are marked by user. So update the buttons

I hope you got this. If any query ask. This is most efficient way as per my knowledge.

Upvotes: 0

mwengler
mwengler

Reputation: 2778

SharedPreference is just a file you can save and retrieve values from. Here are the simplest steps to use a SharedPreference in your activity:

You need to make a variable sP which will point at the SharedPreferences file you will use:

    SharedPreferences sP = getSharedPreferences("MyPrefs", MODE_PRIVATE);

You need to make a variable sPeD which will point at the "editor" for that file which will let you put values in to that file:

    SharedPreferences.Editor sPeD = sP.edit();

You can then extract stored values from that file. The values are indexed by a "key" which is just a String that you define:

    String myString = sP.getString("keyTextYouDefine", "Oops!");

If there is no value stored at key "keyTextYouDefine" then getString() will make myString equal to "Oops!".

In order to store a value at for that key, use this:

    sPeD.putString("keyYouDefine","The string I want to save.");
    sPeD.commit();

If you forget to do commit() after you put things in the file, then they are not actually put there.

This should get you well on your way.

ADDED LATER:

You can then use this to determine which image is on the button.

Assuming you have propertly defined your button

Button button = findViewById(R.id.myButton) // or whatever you are actually using

then set image from whatever, if anything was stored in ShardPreference

    int which = sP.getInt("WhichImage", 1); // assuming image1 is the "default"
    switch (which) {
        case 1:
            button.setCompoundDrawables(null, @drawable/image1, null, null);
            break;
        case 2:
            button.setCompoundDrawables(null, @drawable/image2, null, null);
            break;
        default: // no image
    }

Elsewhere in your activity, when you have decided to switch button image to image2:

    if (whatever) {  // condition for changing to image 2
        button.setCompoundDrawables(null, @drawable/image2, null, null);
        sP.edit().putInt("WhichImage", 2).commit();
    }

I have never used setCompoundDrawables() myself, so your mileage may vary.

Upvotes: 1

Related Questions