Adding a Splash Activity

Discussion in 'Tutorials' started by Vas, Sep 3, 2013.

  1. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    A splash activity is really useful for a live wallpaper. A lot of the times, users that download your wallpapers have difficulties locating them after installation. Of course, the wallpaper will not appear in the app drawer, as it is only supposed to appear in the live wallpaper picker. However, the users will still likely have issues and may end up giving a bad review for the experience. To make it a bit more user friendly you can add a splash activity along with a shortcut icon to the app drawer. It's not difficult at all.

    Below you will find all the source code for the files that need to be added/changed. I have explained the entire process and all the important lines in comments :)

    Here is the source code for the splash activity:
    Code (java):
    package Test.cardslwp;
     
    import android.app.Activity;
    import android.app.WallpaperManager;
    import android.content.Intent;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class Splash extends Activity{
     
        //Make a button that will take the user to the live wallpaper picker
        private Button lwpInstallBtn;
        //Make a simple textview for the title of your wallpaper that we can display in the splash activity
        private TextView title;
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //activities need to refer to an xml layout file, where you have laid out all of the participating components. xml layout files are stored in res/layout. ours is called splash.xml
            setContentView(R.layout.splash);
     
            //this is optional, but you can add your own fonts for your text components for more variety :) if you do, make sure you put it in your assets folder. in this example i have a fonts subfolder inside my assets.
            Typeface font = Typeface.createFromAsset(getAssets(), "fonts/SHOWG.TTF");
     
            //link the textview with the xml element using the ID that is given to it in the layout file
            title = (TextView) findViewById(R.id.title);
            //now you can apply the custom font, if you want to
            title.setTypeface(font);
     
            //now we link the button to the xml in similar fashion
            lwpInstallBtn = (Button) findViewById(R.id.installWallpaper);
            //apply the custom font again
            lwpInstallBtn.setTypeface(font);
            //add an on click listener to the button that will perform the action
            lwpInstallBtn.setOnClickListener(new ImageButton.OnClickListener() {
     
                public void onClick(View v) {
                    //this is where we customize the listener. basically we will outlay a set of instructions that will execute once the button is pressed.
         
                    //first, we deal with accessing the wallpaper picker.
                    //we need to use an intent to tell the system that we want to access the live wallpaper picker. create a new intent with this next line
                    Intent intent = new Intent();
                    //tell the intent what to do.
                    intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
                    //send the intent through
                    startActivity(intent);
     
         
                    //second, let's guide the user to select the wallpaper once they are in the wallpaper picker.
                    //we accomplish this by showing a toast message. it will briefly appear with our instructions. this is the line to get it done:
                    Toast.makeText(getApplicationContext(), "Select " + getString(R.string.application_name), Toast.LENGTH_SHORT).show();
         
                    //now that we are done, close the splash activity since we don't need it anymore.
                    finish();
                }
            });
        }
     
        @Override
        protected void onResume() {
            super.onResume();
        }
     
        @Override
        protected void onStop() {
            super.onStop();
        }
     
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    }
     

    Here's the source code for the xml layout file:
    Code (xml):
    <?xml version="1.0" encoding="utf-8"?>
    <!-- We need to use a layout to organize our elements. Liner Layout will do just fine. You can add a background if you want, or play around with orientation -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:background="@drawable/wallpaper_hdpi"
       android:orientation="vertical" >
     
        <!-- Now we start placing our xml elements. First is the title. Each element is given an ID so that it can be linked to the java component. -->
        <TextView
           android:id="@+id/title"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:layout_marginTop="30dp"
           android:gravity="center"
           android:text="@string/application_name"
           android:textColor="#ff5a00"
           android:textSize="20sp" />
     
        <!-- Now we'll add the button. The text for the button is hardcoded for the sake of example, but you are advised to store all your texts in strings.xml -->
        <Button
           android:id="@+id/installWallpaper"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp"
           android:layout_marginTop="30dp"
           android:text="Set Live Wallpaper" />
     
        <!-- You can build various layouts using xml. We have only added 2 elements to our layout: a button and a textview. There are many more that you can use! You can also use the Graphical Layout tab to better visualize what the layout will look like -->
        <!-- There are various element layout properties that help you shape and align each element. I can't start to explain them all, but you should experiment with them and learn for yourself :) -->
     
    </LinearLayout>
    Here is the source for the modified AndroidManifest.xml:
    Code (xml):
    <?xml version="1.0" encoding="utf-8"?>
     
    <manifest
       xmlns:android="http://schemas.android.com/apk/res/android"
       package="Test.cardslwp" android:versionCode="1"
               android:versionName="1.0">
     
        <uses-sdk android:minSdkVersion="7" />
        <uses-feature android:name="android.software.live_wallpaper" />
     
        <!-- We need to request a permission to install a shortcut icon -->
        <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
     
        <application
           android:label="@string/application_name"
           android:icon="@drawable/icon">
     
            <service
               android:label="@string/service_name"
               android:name="com.sbg.lwc.SBLiveWallpaper"
               android:permission="android.permission.BIND_WALLPAPER">
                <intent-filter>
                    <action android:name="android.service.wallpaper.WallpaperService" />
                </intent-filter>
                <meta-data android:name="android.service.wallpaper" android:resource="@xml/lwp_resource" />
            </service>
     
            <activity
               android:label="Settings"
               android:name="com.sbg.lwc.LiveWallpaperSettings"
               android:theme="@android:style/Theme.WallpaperSettings"
               android:exported="true">
            </activity>
     
            <!-- Register the activity in the manifest -->
            <activity
               android:name="com.sbg.lwc.Splash"
               android:theme="@android:style/Theme.Black.NoTitleBar"
               android:screenOrientation="portrait"
               android:icon="@drawable/icon">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
     
        </application>
        <supports-screens
           android:smallScreens="false"
           android:normalScreens="true"
           android:largeScreens="true" />
    </manifest>
     
    NOTE: If you are using a differently named package, make sure you reflect that change in the manifest. The code above is specifically tailored to com.sbg.lwc package.

    That's it. It's a very basic splash activity. You can customize it as much as you want. You can even add banner ads to it :)

    You can find all the files attached at the end of this post as well (except for the font file. find your own font!)

    Enjoy!

    Attached Files:

    Linda likes this.
  2. sebastian Seasoned Vet

    Member Since:
    Feb 16, 2013
    Message Count:
    135
    Likes Received:
    36
    Trophy Points:
    100
    Hi vas, but we can copy and paste this file into our Live Wallpaper? I am not a code developer...so I don t understand well...were and how we can implementa ADS??XD
    Joshua and fatos like this.
  3. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Vas thanks for the tutorial,this will be very helpful...

    But I have two questions:
    1. Where do I have to put the source code for the splash activity ( what should I open on Eclipse,do I have to create a new file and add the splash activity source code or what ? )
    2. Where do I have to put the source code for the xml layout file
  4. Sam Keeps coming back

    Member Since:
    Jun 3, 2013
    Message Count:
    14
    Likes Received:
    5
    Trophy Points:
    10
    Sebastian ..
    1. Put Class file which is a Splash.java into main project under Source Directory .
    2. Put Splash.xml into layout folder
    3. on the manifest only put this portion of code before closing </application> Tag on Manifest File .

    <activity
    android:name=".Splash"
    android:theme="@android:style/Theme.Black.NoTitleBar"
    android:screenOrientation="portrait"
    android:icon="@drawable/icon">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    fatos likes this.
  5. sebastian Seasoned Vet

    Member Since:
    Feb 16, 2013
    Message Count:
    135
    Likes Received:
    36
    Trophy Points:
    100
    THX so much i'll try tomorrow..here it's 01,00
  6. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    After I've put the source code for the splash activity,Im having an errors at Splash.java,the error is on letter R ( I created splash.java like a new class and I named it Splash, do I have to do it like this or not)

    splash.java error.png

    And second error is on splash.xml
    Any idea guys how to fix those 2 errors

    splash.xml....png

    Attached Files:

    sebastian likes this.
  7. virajith Active Member

    Member Since:
    Dec 2, 2012
    Message Count:
    71
    Likes Received:
    10
    Trophy Points:
    50
    those aren't errors

    u need to add a application_name in string.xml file or replace with some valid one

    btw try re importing resource files "R" ...use eclispe suggested one
    fatos likes this.
  8. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Yes I added app name in string.xml and now Splash.xml is ok

    In Splash.java I imported R to my package name and now 3 errors are gone,I have just one error now at getString(R.string.application_name). Im giving my app name just like in string.xml but then gives me more errors. When I hover mouse on application_name it gives me lot of fixes but quick fixes cant fix it :(
    Any ideas guys...

    Splash.java....png
  9. Vas Origin

    Member Since:
    Jan 4, 2012
    Message Count:
    770
    Likes Received:
    175
    Trophy Points:
    500
    R.string.application_name is a reference to the app name that you added to your strings.xml. Change application_name to whatever your entry is called.
    Here's a cool trick for you:
    In R.string.application_name, delete application_name, so that you just have R.string. left. Then make sure the cursor is after the dot and press ctrl+space. This will show you all the options you have available to you from your strings.xml
  10. Friendlyspeaking Active Member

    Member Since:
    Oct 12, 2012
    Message Count:
    86
    Likes Received:
    27
    Trophy Points:
    50
    I am also receiving 3 errors after importing R, on mouseover it is showing

    splash cannot be resolved or not a field
    installWallpaper cannot be resolved or not a field

    Attached Files:

  11. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Thanks Vas
    I have done like you said and I fixed the error. My live wallpaper now appears in the app drawer but when I try to open the screen turns black for 5-6 seconds and a pop up message appears sayng Unfornately Squarer Live Wallpaper has stopped working. Something is missing here.....
    sebastian likes this.
  12. sebastian Seasoned Vet

    Member Since:
    Feb 16, 2013
    Message Count:
    135
    Likes Received:
    36
    Trophy Points:
    100
    <I have the sameproblem...if I click on icon I heve the fatos's problem, if I go to long press on the screen > live wallpaper > and I select the live wallpaper this open normally...but I not understand one think:
    the splash sceen should open only when consumer press on the icon?? or ever??
    fatos likes this.
  13. Sam Keeps coming back

    Member Since:
    Jun 3, 2013
    Message Count:
    14
    Likes Received:
    5
    Trophy Points:
    10
    If you are using eclipse then check log-cat .. run the program and then check what error it shows in the back portion .
  14. sebastian Seasoned Vet

    Member Since:
    Feb 16, 2013
    Message Count:
    135
    Likes Received:
    36
    Trophy Points:
    100
    boys, with Leadbolt is no longer possible to create the notification ads, it is absurd I urilizzo other store and they agree very well ores type of ads
  15. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Sam Eclipse shows me no errors,and I dont have any warnings regarding Splash.java , Splash.xml and AndroidManifest.xml
    But something is missing and I dont know what:confused:
  16. NewKid Seasoned Vet

    Member Since:
    Apr 1, 2012
    Message Count:
    100
    Likes Received:
    20
    Trophy Points:
    100
    Wait for leadbolt and startapp to unveil their new sdk, but there are also other companies we can use with lwp.
  17. wladca Active Member

    Member Since:
    Mar 20, 2013
    Message Count:
    50
    Likes Received:
    4
    Trophy Points:
    50
    Which one for example ? It's easy to implement their sdk ??
  18. NewKid Seasoned Vet

    Member Since:
    Apr 1, 2012
    Message Count:
    100
    Likes Received:
    20
    Trophy Points:
    100
    I've seen three companies, their ads don't seem as good as push ads but it's something because at least their out of app ads are compliant with Google's new policy, i haven't post it yet here as I'm still searching and i haven't try them myself and because I'm also waiting for leadbolt and startapp to say something new.
    fatos likes this.
  19. fatos LWC Major

    Member Since:
    Apr 2, 2013
    Message Count:
    268
    Likes Received:
    73
    Trophy Points:
    200
    Did anyone added successfully splash activity... I have no errors on eclipse and my live wallpaper icon appears in the app drawer,but the screen turns black when i press the icon.....help...I really want to implement splash activity:( If someone can give me a help what is wrong

    Here is my splash.java

    Splash_java.png

    and my splash.xml

    Splash_xml (1).png
    sebastian likes this.
  20. sebastian Seasoned Vet

    Member Since:
    Feb 16, 2013
    Message Count:
    135
    Likes Received:
    36
    Trophy Points:
    100
    pn leadbolt new sdk caming soon....I hope... -18
    wladca and fatos like this.

Share This Page