[2.1] Adding links to your wallpaper settings

Discussion in 'Tutorials' started by Vas, Nov 1, 2012.

  1. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    Here's a short guide on (one of the many ways on) how to add links to your live wallpaper settings. I might do a video later, when I have more time.

    Objective:
    To add a setting to our live wallpaper that, when clicked on, it opens the browser and takes the user to the specified link/URL.

    Files you need to edit:
    LiveWallpaperSettings.java (found in the src folder of your project)
    lwp_settings.xml (found in the xml folder of your project)

    Step 1 - Adding the setting:
    The file lwp_settings.xml organizes all the settings that you see in your live wallpaper projects. They are called preferences in Android. So in order to add another one, we have to modify that file. The following code is just an example. Keep in mind that the placement of this code, in respect to the other stored preferences, impacts its location in the wallpaper settings.

    Code (text):
    <Preference
                android:title="This is an ad supported version"
                android:summary="Click here to buy the ad-free version on Google Play."
                android:key="go_to_ad_free" />
    Title - The title of the setting.
    Summary - Usually a brief description of the setting and what it does.
    Key - A reference point that we will use internally later.

    Step 2 - Adding the java code:
    The file LiveWallpaperSettings.java contains the java code that displays the contents of lwp_settings.xml. So now we need to add some Java code that will open a browser on the user's phone and take them to our link. In our example, I want the user to go to the Google Play page with the ad-free version of our wallpaper, with hopes that they will purchase it :D

    First find the following line in file LiveWallpaperSettings.java:
    Code (text):
    PreferenceCategory main_preference_category = (PreferenceCategory) findPreference("main_preference_category");
    Paste the following code just underneath that line:
    Code (text):
            getPreferenceManager().findPreference("go_to_ad_free")
                    .setOnPreferenceClickListener(
                            new OnPreferenceClickListener() {
                                @Override
                                public boolean onPreferenceClick(
                                        Preference preference) {
                                    Intent intent = new Intent(Intent.ACTION_VIEW);
                                    intent.setData(Uri
                                            .parse("https://play.google.com/store/apps/details?id=SeriousBusinessGroup.adfree.supersaiyanlwp"));
                                    startActivity(intent);
                                    return true;
                                }
                            });
    I won't get into the details of what each line does. Basically, we say find preference with the key of "go_to_ad_free" (remember the key value from before?), and if the user clicks on it open this URL. In our case, the URL leads to the market.

    Now, Eclipse (I used Eclipse as my Java IDE) will probably show you some red squiggly lines. We need to fix them before we finish. Eclipse offers some quick fix solutions, but to avoid confusion, we will fix it ourselves.

    Find the following line in your LiveWallpaperSettings.java:
    Code (text):
    import android.preference.PreferenceCategory;


    Now add the following code below:
    Code (text):
    import android.content.Intent;
    import android.net.Uri;
    import android.preference.Preference.OnPreferenceClickListener;
    That's it! Save your files and test out the changes. I will probably put up my modified wallpaper on Google Play for everyone to see in the coming days. Hopefully, I will have more time to make a video and include some screenshots, but this should get you started!

    If you have any questions, just post here. Good luck!
    MicioVille likes this.
  2. Shyner Keeps coming back

    Member Since:
    Sep 2, 2012
    Message Count:
    18
    Likes Received:
    7
    Trophy Points:
    10
    This is awesome, thank you! I've added the code strait into the LWC files so LWC will create the link when ever it builds a wallpaper. I placed the link at the bottom of settings and have it leading to my play store, I changed the key to "my_google_play_store". It's working great. I've barely had any time to work on anything but hopefully that will be changing!

    I would love a Tut on adding a selection of backgrounds and falling item sets!

    A person can probably repeat the first two steps to add other external links as well hey?

    Thanks again Vas!
    Vas likes this.
  3. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    Perfect! Glad to hear that.
    You can add as many links as you want with this tutorial :)

    Changing backgrounds and falling item sets is a little harder, but it's not rocket science. I'll see if I can get around to it sometime.

    :D
  4. MicioVille In the know

    Member Since:
    Oct 5, 2012
    Message Count:
    25
    Likes Received:
    5
    Trophy Points:
    25
    GRAZIE!!!! GRAZIE!!! GRAZIE!!!
    :):):)
    Vas likes this.
  5. Will New Guy

    Member Since:
    Jan 9, 2013
    Message Count:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi Vas,

    when I have implemented this to the full version app and run it, I get this error message and it shuts down my app.
    Any reason why this is happening?
    error.jpg
  6. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    Hard to say from that error message. It's pretty general.
    It's possible that you missed something. I would say check to make sure you followed all the steps correctly. Even a typo in one place can break functionality.
  7. stollo770 LWC Major

    Member Since:
    Feb 2, 2012
    Message Count:
    236
    Likes Received:
    64
    Trophy Points:
    200
    Hey Vas,

    How do you add more then one link to your settings menu? Every time I try it doesn't work. I can't get the code to work in the livewallpapersettings.java. Duplicating the code with a different key phrase doesn't work. Any suggestions? Thanks
  8. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    Repeat step 1 and 2, changing the android:key and the corresponding call for it in findPreference() to a new matching value. If it doesn't work, you probably did something wrong.
  9. stollo770 LWC Major

    Member Since:
    Feb 2, 2012
    Message Count:
    236
    Likes Received:
    64
    Trophy Points:
    200
    You were right Vas, I did do something wrong. When I went to repeat the process I hit enter on my keyboard to leave a black line in between the end of the first getpreferencemanager () lines and the second getpreferencemanager lines. This is what the code looked like when it was not working:

    getPreferenceManager().findPreference("go_to_ad_free")
    .setOnPreferenceClickListener(
    new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(
    Preference preference) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri
    .parse("https://play.google.com/store/apps/details?id=SeriousBusinessGroup.adfree.supersaiyanlwp"));
    startActivity(intent);
    return true;
    }
    });


    getPreferenceManager().findPreference("go_to_ad_free")
    .setOnPreferenceClickListener(
    new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(
    Preference preference) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri
    .parse("https://play.google.com/store/apps/details?id=SeriousBusinessGroup.adfree.supersaiyanlwp"));
    startActivity(intent);
    return true;
    }
    });

    and this is what it looks like now:

    getPreferenceManager().findPreference("go_to_ad_free")
    .setOnPreferenceClickListener(
    new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(
    Preference preference) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri
    .parse("https://play.google.com/store/apps/details?id=SeriousBusinessGroup.adfree.supersaiyanlwp"));
    startActivity(intent);
    return true;
    }
    });
    getPreferenceManager().findPreference("go_to_ad_free")
    .setOnPreferenceClickListener(
    new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(
    Preference preference) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri
    .parse("https://play.google.com/store/apps/details?id=SeriousBusinessGroup.adfree.supersaiyanlwp"));
    startActivity(intent);
    return true;
    }
    });


    With no spaces between the two pieces, it works just fine. Thanks for the help and reassurance that I was on the right track,
    I don't think I would have tried this otherwise.
    Shyner likes this.

Share This Page