In Android Operating System (OS) based on mobile phone or devices, the UI screen that appears first after the user presses the ‘Power on’ button is called the “Android Launcher Screen”. It is possible in Android to construct a custom launcher application, which can be used as a replacement for the default launcher application that comes along with Android phones. Unlike the iPhone, this is one of Android’s best features which lets you design your phone’s interface.
Android Launcher concept
The Launcher falls into one of two categories: ‘design’ or ‘smart’.
Design launchers focus on User Interface. You can change the entire layout of your Android phone’s home screen, application icon shape, icon drag and drop concepts, image gallery etc. You can customize everything it has to offer, including the themes, animations, layout, shortcuts and colors. Some of its interesting features include unlimited scrolling, customize app drawer layout, download-able themes and widget customization.
Smart Launchers learn the most relevant information about you which can be your wake up time, head out on your commute, work at the office, most favorite spot of city etc. You also can add custom gestures to your phone such as double-tapping the screen to open a specific application, web-page or specific setting.
Requirement
You need to have the following installed and configured on your development machine:
- Android SDK and platform tools
- Android Studio
- An emulator or Android device running Android 2.2 or higher
Project Manifest.xml File Modification
In manifest.xml file add two activities. First activity displays the home screen and the second activity needs to display the applications that are installed on user’s device. It is also responsible for launching the installed applications. We don’t need any special configuration for this Activity.
Add the following two categories in Manifest.xml file to the intent-filter group. This intent-filter is associated with the Activity.
<category android_name="android.intent.category.HOME" /> <category android_name="android.intent.category.DEFAULT" />
We also need to set the android:launchMode to “singleTask” to make sure that only one instance of this Activity is held by the system at any time. To show the user’s wallpaper, set the theme to
android:theme=”@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen”
<activity android_name="ah.hathi.simplelauncher.HomeScreenActivity" android_label="Simple Launcher Home" android_theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" android_launchMode="singleTask" android_stateNotNeeded="true"> <intent-filter> <action android_name="android.intent.action.MAIN" /> <category android_name="android.intent.category.HOME" /> <category android_name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Add one more activity in Manifest.xml file for displaying the applications that are installed on user’s device.
<activity android_name="ah.hathi.simplelauncher.AppsListActivity" android_theme="@android:style/Theme.NoTitleBar.Fullscreen" > </activity>
Launcher Main Activity Layouts
Add UI design code in Activity_HomeScreen.xml file. Here layout has a single Button that responds to click events. Clicking the button takes the user from home screen to the list of applications.
<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android" xmlns_tools="http://schemas.android.com/tools" android_layout_width="match_parent" android_layout_height="match_parent" tools_context=".HomeActivity" >
<Button
android:id=”@+id/home_screen_apps_button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:layout_alignParentTop=”true”
android:layout_marginRight=”10dp”
android:layout_marginTop=”10dp”
android:text=”Show Apps”
android:onClick=”showApps”
/>
</RelativeLayout>
Next is to create an XML file for the AppListActivity class in the project’s res/layout folder and name it as activity_apps_list.xml. The layout contains a ListView that takes up the entire screen.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns_android="http://schemas.android.com/apk/res/android" android_layout_width="match_parent" android_layout_height="match_parent" android_orientation="vertical" >
<ListView
android:id=”@+id/apps_list”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
>
</ListView>
</LinearLayout>
Finally, create a third XML file in the same location and name it as list_item.xml. This file defines the layout of an item in the ListView
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android" android_layout_width="match_parent" android_layout_height="match_parent" android_padding="10dp" >
<ImageView
android:id=”@+id/item_app_icon”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true”
/>
<TextView
android:id=”@+id/item_app_label”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_toRightOf=”@+id/item_app_icon”
android:paddingLeft=”10dp”
/>
<TextView
android:id=”@+id/item_app_name”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/item_app_label”
android:layout_toRightOf=”@+id/item_app_icon”
android:paddingLeft=”10dp”
/>
</RelativeLayout>
Implementing the HomeScreenActivity Classes
With the layouts of the application created, it is time to create two Activity classes. When creating the two classes, make sure the name of each class matches the one you specified in the project manifest file earlier.
Create a new class named HomeScreenActivity and set android.app.Activity as its superclass.
Add this code in your HomeScreenActivity class or your main Activity class in onCreate method.
selectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
Intent i = new Intent(this, AppsListActivity.class);
startActivity(i);
}
});
We create an Intent for the AppsListActivity class and start it.
AppsListActivity Class
Create another Activity class named AppsListActivity and set android.app.Activity as its superclass. In the class’s onCreate method, we invoke setContentView, passing in the activity_apps_list layout we created earlier.
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View;
public class AppsListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apps_list);
}
}
AppDetail.java class file
Create a class named AppDetail that will contain the details of an application, its package name, label, and application icon. The interface is pretty basic as you can see below.
public class AppDetail { CharSequence label; CharSequence name; Drawable icon; }
Fetching installed Applications
In the LoadApplication method of the AppListActivity class, we use the queryIntentActivities method of the PackageMangaer class to fetch all the Intents that have a category of Intent.CATEGORY_LAUNCHER. The query returns a list of applications that can be launched by a launcher. We loop through the results of the query and add each item to a list named Apps.
private PackageManager manager; private List<AppDetail> apps;
private void loadApplication(){
manager = getPackageManager();
apps = new ArrayList<AppDetail>();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i, 0);
for(ResolveInfo ri:availableActivities){
AppDetail app = new AppDetail();
app.label = ri.loadLabel(manager);
app.name = ri.activityInfo.packageName;
app.icon = ri.activityInfo.loadIcon(manager);
apps.add(app);
}
}
List of Applications
The Apps variable containing all the details we need, we can show the list of applications using ListView class. We create a simple ArrayAdapter and override its getView method to render the list items. We then associate the ListView with the adapter.
private ListView list; private void loadListView(){ list = (ListView)findViewById(R.id.apps_list);
ArrayAdapter<AppDetail> adapter = new ArrayAdapter<AppDetail>(this,
R.layout.list_item,
apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.list_item, null);
}
ImageView appIcons = (ImageView)convertView.findViewById(R.id.item_app_icon);
appIcons.setImageDrawable(apps.get(position).icon);
TextView appLabels = (TextView)convertView.findViewById(R.id.item_app_label);
appLabels.setText(apps.get(position).label);
TextView appNames = (TextView)convertView.findViewById(R.id.item_app_name);
appNames.setText(apps.get(position).name);
return convertView;
}
};
list.setAdapter(adapter);
}
ListView OnClickListener Method for List item
When the user clicks an item in the ListView, corresponding application will be launched by our launcher. We use the getLaunchIntentForPackage method of the PackageManager class to create an Intent with which we start the application.
private void addonClickListener(){ list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> av, View v, int pos ,long id) { Intent i = manager.getLaunchIntentForPackage(apps.get(pos).name.toString()); AppsListActivity.this.startActivity(i); } });<br > }
Putting It All Together
We need to invoke loadApplication, loadListView and AddClickListener in the onCreate method of AppsListActivity class as shown below.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_apps_list);
loadApplication();
loadListView();
addClickListener();
}
Conclusion
Build and run your application once more to see the result. You should now be able to see the applications that can be launched when you click a button on the home screen of our launcher. Click on an item to launch the corresponding applications. That’s it! Now, try to mimic the UI look and feel of iPhone in your launcher application as you have got complete control over everything!
Here’s a quick read to help you update Android apps outside of Google Play.