You are on page 1of 36

Build An Application Launcher For Android | Taywils.

me

4/9/14 11:28 PM

Build An Application Launcher For Android

Blog

Archive

Projects

About

RSS

To start off create a new Android application project within Eclipse. Whenever I develop Android apps I usually start with the layout of the application and add functionality via Java

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 1 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

classes later. Since our app will display the users installed apps within a ListView widget we have to first declare a layout to hold the ListView itself. Create a new xml layout for the ListView by right clicking the res/layout folder and adding a new android xml file. Title it "applauncher.xml" and then add the code below to it.
<LinearLayout android:id="@+id/applauncher_linearlayout" android:layout_width android:layout_height android:orientation <ListView android:id= android:layout_width android:layout_height <TextView android:id= android:layout_width android:layout_height android:text

xmlns:android="http://schemas.android.com/apk/res/andr

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 2 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

</LinearLayout>

When using ListViews we can setup the overall look of each row by creating an xml layout with a skeleton of the widgets that we want to display in each row of our ListView. Think of the row layout as a "row template". If you look at the screenshots of the app you can tell that we only need two widgets within our row layout, an ImageView to hold the apps icon and a TextView to display the apps package name. Create a new xml layout for the ListView rows by right clicking the res/layout folder and adding a new android xml file. Title it "applauncherrow.xml" and add the code below.
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 3 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

<LinearLayout android:id="@+id/applauncherrow_imagelinearlayout" android:layout_width android:layout_height android:padding="6dip" <ImageView android:id="@+id/applauncherrow_icon" android:layout_width android:layout_height android:layout_marginRight <LinearLayout android:id="@+id/applauncherrow_namelinearlayout" android:orientation android:layout_width android:layout_weight android:layout_height <TextView android:id="@+id/applauncherrow_appname" android:layout_width android:layout_height android:layout_weight android:gravity </LinearLayout> </LinearLayout>

xmlns:android="http://schemas.android.com/apk/res/andr

Next we're going to setup the main activity for the launcher app and register the activity in the manifest. Create a new Java Class
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 4 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

and name it "AppLauncher.java". Next open up the manifest file for your application and make sure it looks like the code below.
<!-- Your package="" name might differ --> <manifest package="org.example.hello" android:versionCode android:versionName

xmlns:android="http://schemas.android.com/apk/res/andr

<!-- Your default android:icon="" name might differ -<application android:icon="@drawable/icon" android:label android:debuggable android:description <activity android:name android:label

<!-- This tag indicates the initial ac <intent-filter> <action <category </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 5 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

</manifest>

Ok so now that the layouts are coded up and the activity is registered within the manifest open up AppLauncher.java and lets begin. Lets start off with importing all the necessary libraries needed for our app.
//Your package name might differ package org.example.hello; import java.util.ArrayList; import java.util.Collections import java.util.List; import android.app.ListActivity import android.app.ProgressDialog import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.pm.ActivityInfo import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.content.pm.ResolveInfo import android.graphics.drawable import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater import android.view.View;

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 6 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

import android.view.ViewGroup import android.widget.ArrayAdapter import android.widget.ImageView import android.widget.ListView import android.widget.TextView

Our app will extend the ListActivity class in order to reduce the boiler plate needed to build a ListView.
public class AppLauncher extends

However, due to the structure of the app we'll build the classes needed and stitch them together so just follow along with a text editor for now. To begin we'll need a class to hold all the useful data we can obtain from a given app. We'll call it AppInfo, its very basic so go ahead and type it into your editor.
public class AppInfo{ private String private String private String private String

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 7 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

private Integer private Drawable public String return } public String return } public String return } public String return } public Integer return } public Drawable return } }

The next class we need is a class that will be used to obtain the application info for the AppInfo class. This class is the bread and butter of our AppLauncher and it together with the
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 8 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

AppInfo class can be ported to any application which needs to access other apps via intents.
public class Applications{ private ArrayList private List private Intent private PackageManager

For the Applications constructor we will pass in the all important PackageManager from the main context.
public Applications(PackageManager packMan packageList activityList this }

Below are some getter methods for the package and activities list. The difference between the two is that the package list contains the info about the apps data and the activity
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 9 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

list contains info about the intents the app responds to.
public ArrayList getPackageList return } public List return }

The next method is the createPackageList method. All it does is simply extract a list of all the installed apps via the PackageManager that we passed into the constructor. Next we iterate over the list and copy the info we need into a AppInfo object and then shove that object into the ArrayList<AppInfo> that will be returned.
private ArrayList createPackageList ArrayList List packs
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 10 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

for(int PackageInfo if((! } AppInfo newInfo getPackageManager newInfo newInfo newInfo newInfo getPackageManager pList } return pList }

Similar to the createPakageList method, createActivityList will return a list with all of the apps installed intent data but unlike createPackageList its only 8 lines of code.
private List createActivityList List
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 11 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

Collections

return }

The next two methods are purely for dubugging purposes and can actually be left out but we'll include them for completeness or in case you use this class in another application.
private void packageDebug(){ if( } for packageList packageList packageList packageList packageList } } private void if( }

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 12 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

for i

} }

The next method is needed only because in order to launch an app you need both its package name and its intent name that connects to the main class of the app. However, if you only use the packageList you'll lack the intent data, so the last method of the Application class adds the class name(intent data) to the packageList making it such that client of the class can have everything bundled up nicely in the packageList instead of having to rely on using both the activity and

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 13 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

packageList in unison. For the method addClassNameToPackageList we'll iterate over the packageList and for each packageName we'll find its match within the activityList by packageName and then extract the className from the activityList.
private void addClassNamesToPackageList if(null } String for(int tempName

j packageList

} } }

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 14 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

Below is the complete Application class


public class Applications{ private ArrayList private List private Intent private PackageManager public Applications packMan packageList activityList this } public ArrayList return } public List return } private ArrayList ArrayList List packs

for(int PackageInfo if((! } AppInfo


http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 15 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

newInfo getPackageManager newInfo newInfo newInfo newInfo getPackageManager pList } return pList } private List List Collections

return } private void if( } for packageList packageList packageList packageList packageList } }

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 16 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

private void if( } for i

} } private void if( } String for tempName

j packageList

} } }

Remember the AppInfo class that we made and the


http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 17 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

applauncherrow.xml layout. Well, now we'll utilize both within the custom ArrayAdapter extension we'll use to populate the data of our List. ArrayAdapters are used to fill in the data fields of a ListView widget by storing a layout(android uses the term inflate) holding data into a single row of the list.
public class ApplicationAdapter private ArrayList

The constructor for our ArrayAdapter extension...


public ApplicationAdapter(Context super(context, textViewResourceId this.items = items; }

Being an ArrayAdapter extension means that we have to override the getView method, this is

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 18 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

where the applauncherrow layout is populated with data and is used to represent each row of the ListView. To start we'll reference the applauncherrow layout and inflate it to become a view.
@Override public View getView ViewGroup View if(

view }

Now that the view is inflated with the applauncherrow.xml layout we can now match the ImageView and TextView with the contents of a AppInfo object in preparation for moving it into the ListView.
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 19 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

AppInfo appInfo = items.get if( R R

appName

appIcon } return } }

Here is the complete ApplicationAdapter class, you should add it to your text file.
public class ApplicationAdapter private ArrayList public ApplicationAdapter super(context, textViewResourceId this.items = items } @Override public View getView ViewGroup

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 20 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

View if(

view } AppInfo if( R R

appName

appIcon } return } }

We are done with the utility classes so the next step is to add some methods to the AppLauncher class itself. The first one we're going to add is a method called getApps() which stores the
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 21 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

pacakageList from an Applications object and passes it off to Java Runnable to be ran on a thread.
private void getApps(){ try{ myApps packageList } catch(Exception Log } this.runOnUiThread }

If you look at the getApps() method the last line before the class ends is a method called this.runOnUiThread(returnRes), now what this means is that the main thread will be locked while we pump AppInfo objects into our ListView via the ArrayAdapter. If you are experienced with concurrent programming
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 22 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

you might be wondering why lock the mainUIThread, the answer is because our app can't do anything until all objects have been added to the list. If we by chance were dynamically loading things from the Internet via HTTP requests then we would opt instead for a lazy loader which spawns a thread for each view object pumped into the ListVIew and the list would slowly populate as each item was fetched live from the web. So anyways here is our Runnable method that checks first that the packageList exists and then uses an object from our ApplicationAdapter to fill up our ListView widget.
private Runnable returnRes public void if(
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 23 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

appAdapter

appAdapter } progressDialog appAdapter } };

We're almost done so now open up your Eclipse IDE to the AppLauncher project and click on the AppLauncher.java class. If you remember from earlier where we left off go ahead and add the data members below to the AppLauncher class; notice the line numbers for reference.
ApplicationAdapter appAdapter ProgressDialog progressDialog Runnable viewApps = ArrayList packageList Applications myApps

For the onCreate() method there are some things you sould notice. First since
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 24 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

AppLauncher is a ListActivity extension it inherits the setListAdapter() method. Second we'll initialize the viewApps Runnable to call our getApps() within its run(). This means that any thread created with the viewApps as it's runnable will load up our packages into the ListView. In addition since getApps() locks the mainUIThread users of this app won't be able to do anything until the thread finishes.
public void onCreate(Bundle super.onCreate this.setContentView packageList appAdapter packageList this.setListAdapter viewApps = public
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 25 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

getApps } }; Thread appLoaderThread appLoaderThread

Now that the thread has started and the app has been officially locked, how do we notify the user? The answer is with the android.app.ProgressDialog widget. Remember those loading screen from popular video games, that's all ProgressDialogs do.
progressDialog = ProgressDialog }

Right now our app can successfully load up a list of Icons and application names but how then do we launch the apps the user touches from our list? The answer is via starting a
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 26 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

new intent by overriding the onListItemClick method. So go open up your text file that contains the utility classes and methods and add the onListItemClick() method to it.
@Override protected void onListItemClick long super.onListItemClick AppInfo rowClicked position Intent startApp ComponentName rowClicked rowClicked startApp.setComponent startApp.setAction startActivity }

Before you run the AppLauncher, be sure to copy and paste the classes and methods into the AppLauncher class so it
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 27 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

resembles the complete file below.


org.example.hello; java.util.ArrayList; java.util.Collections; java.util.List; android.app.ListActivity; android.app.ProgressDialog; android.content.ComponentName android.content.Context; android.content.Intent; android.content.pm.ActivityInfo android.content.pm.PackageInfo android.content.pm.PackageManager android.content.pm.ResolveInfo android.graphics.drawable.Drawable android.os.Bundle; android.util.Log; android.view.LayoutInflater; android.view.View; android.view.ViewGroup; android.widget.ArrayAdapter; android.widget.ImageView; android.widget.ListView; android.widget.TextView; class AppLauncher extends ListActivity ApplicationAdapter appAdapter ProgressDialog progressDialog Runnable viewApps = null; ArrayList packageList = null Applications myApps = null; public void onCreate(Bundle savedInstanceState super.onCreate(savedInstanceState
http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html Page 28 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

this.setContentView( packageList = new ArrayList appAdapter = new ApplicationAdapter packageList this.setListAdapter( viewApps = new Runnable public void run getApps } }; Thread appLoaderThread "AppLoaderThread" appLoaderThread.start progressDialog = ProgressDialog "Hold on..." } public class Applications{ private ArrayList packageList private List activityList private Intent mainIntent private PackageManager public Applications( packMan = packManager packageList activityList this.addClassNamesToPackageList } public ArrayList getPackageList return packageList

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 29 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

} public List getActivityList return activityList } private ArrayList createPackageList ArrayList pList List packs = getPackageManager ).getInstalledPackages for(int i = 0; i PackageInfo packInfo if((!getSysPackages continue } AppInfo newInfo newInfo.appName getPackageManager newInfo.packageName newInfo.versionName newInfo.versionCode newInfo.icon getPackageManager pList.add(newInfo } return pList; } private List createActivityList List aList = Collections.

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 30 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

return aList } private void packageDebug if(null == packageList return } for(int i = Log. packageList packageList packageList packageList packageList } } private void activityDebug if(null == activityList return } for(int i = ActivityInfo i Log.

} } private void addClassNamesToPackageList if(null == activityList return

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 31 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

} String tempName for(int i = tempName for( j packageList j } } } } public class AppInfo{ private String appName private String packageName private String className private String versionName private Integer versionCode private Drawable icon public String getAppName return appName } public String getPackageName return packageName } public String getClassName return className }

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 32 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

public String getVersionName return versionName } public Integer getVersionCode return versionCode } public Drawable getIcon return icon; } } public class ApplicationAdapter private ArrayList items public ApplicationAdapter ArrayList super(context, textViewResourceId this.items = items; } @Override public View getView(int position ViewGroup parent View view = convertView if(view == null LayoutInflater view } AppInfo appInfo if(appInfo != TextView R ImageView

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 33 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

R if(appName appName } if(appIcon appIcon } } return view; } } private void getApps(){ try{ myApps = new packageList } catch(Exception exception Log.e("BACKGROUND PROC:" } this.runOnUiThread(returnRes } private Runnable returnRes = public void run(){ if(packageList appAdapter for( appAdapter } } progressDialog appAdapter.notifyDataSetChanged } };

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 34 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

@Override protected void onListItemClick long id){ super.onListItemClick AppInfo rowClicked = position Intent startApp = new ComponentName component rowClicked rowClicked startApp.setComponent startApp.setAction(Intent startActivity(startApp }

Enjoy your AppLauncher.

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 35 of 36

Build An Application Launcher For Android | Taywils.me

4/9/14 11:28 PM

Comments

Community

Login

Sort by Best

Share

Favorite

Join the discussion


felipetio 13
!

6 months ago
! Reply ! Share

Have a project source? Thx

Paul Marcellana
!

3 months ago

May you please provide a project source. I need it badly. Thanks


4
! Reply ! Share

Sagara S.Dev
!

2 months ago

for PackageInfo packInfo = packs.get(i); i am getting Type Mismatch: can not convert from object to Package info error. any suggetions?
2
! Reply ! Share

ALSO ON TAYWILS.ME

WHAT'S THIS?

Install Python, Numpy and Pandas 1 commenton a year ago Windows 7


sutm

Java Spark Framework 1 comment 3 Tutorial


months ago

Diego Torres

Thanks for

Thanks.

Copyright 2014 Demetrious Taylor Wilson. All rights reserved.

http://www.taywils.me/2011/07/05/buildanapplicationlauncherwithandroid.html

Page 36 of 36

You might also like