Android Create your own launcher

Source: Internet
Author: User
Tags home screen

Introduction

In its more basic form, a launcher is an application that does the following:

    • It represents the home screen of a device

    • It lists and launches applications that is installed on the device

In other words, it's the application that shows up when you press the home button. Unless you ' ve already installed a custom launcher and you're currently using the default launcher that comes with your Andro ID installation. A lot of device manufacturers has their own default, custom launchers that conform to their proprietary look and feel, fo R example, Samsung TouchWiz and HTC sense.

In this tutorial, we is going to create a simple launcher with a basic user interface. It would have both screens:

    • A home screen showing the device ' s wallpaper

    • A screen showing the icons and details of the applications installed on the device

1.Requirements

You need to has the following installed and configured on your development machine:

    • Android SDK and Platform tools

    • Eclipse IDE 3.7.2 or higher with the ADT plugin

    • An emulator or Android device running Android 2.2 or higher

You can download the SDK and platform tools the Android Developer Portal.

2.Project Setup

Launch Eclipse and create a new Android application project. I ' m naming the application simplelauncher, but can name it anything you want. Make sure-a unique package. The lowest SDK version our launcher supports are Froyo and the target SDK is Jelly Bean.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4D/29/wKiom1RNHpPQczRhAAJEuuMz2CM531.jpg "title=" S1a.png "alt=" Wkiom1rnhppqczrhaajeuumz2cm531.jpg "/>


Since we don ' t want Activity to the Create an, deselect create Activity. Click Finish to continue.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/29/wKiom1RNHsCwdfiMAAFOF1XCKOc036.jpg "title=" s3.png "alt=" Wkiom1rnhscwdfimaafof1xckoc036.jpg "/>


3.Project Manifest

The next step is modifying, the androidmanifest.xml file by adding and the activities. The first Activity displays the home screen. Let ' s name it as HomeActivity shown below.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/2A/wKioL1RNH5OiNhKAAAG7DV2I9Xk184.jpg "title=" 1.PNG " alt= "Wkiol1rnh5oinhkaaag7dv2i9xk184.jpg"/>


By adding the categories android.intent.category.HOME android.intent.category.DEFAULT intent-filter and to the group, the associated Activity behaves like a launcher and shows up as An option while you press the device ' s home button.

We also need to set the-to-make sure-instance of this was held by the system at any time launchMode singleTask Activity . To show the user's wallpaper, set the theme to Theme.Wallpaper.NoTitleBar.FullScreen .

The second Activity we need to add displays the applications that is installed on the user ' s device. It ' s also responsible for launching applications. We don ' t need any special the configuration for this Activity . Name it AppsListActivity .

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/2A/wKioL1RNH9-Dp5E1AACeOm3TER8170.jpg "title=" 2.PNG " alt= "Wkiol1rnh9-dp5e1aaceom3ter8170.jpg"/>


4.Activity Layouts

Create an XML file for the HomeActivity class in the project ' s res/layout folder and name it activity_home.xml. The layout has a single, Button responds to click events. Clicking the button takes the user from the home screen to the list of applications.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/29/wKiom1RNH7_DpwR2AAH2lzvcJ5U167.jpg "title=" 3.PNG " alt= "Wkiom1rnh7_dpwr2aah2lzvcj5u167.jpg"/>


Next, create an XML file for the AppsListActivity class in the project ' s res/layout folder and name it Activity_apps_lis T.xml. The layout contains a, takes up, the ListView entire screen.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/2A/wKioL1RNIFShoFsOAAFB9mfElhg500.jpg "title=" 4.PNG " alt= "Wkiol1rnifshofsoaafb9mfelhg500.jpg"/>


Finally, create a third XML file in the same location and name it list_item.xml. This file defines the layout of a item in the ListView . Each list view item represents a application installed on the user ' s device. It shows the application ' s icon, label, and package name. We Display the application icon using a instance and instances for the label and package ImageView TextView name.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/29/wKiom1RNIEXRI-pIAAMr3HOsw0Q439.jpg "title=" 5.PNG " alt= "Wkiom1rniexri-piaamr3hosw0q439.jpg"/>


5.Implementing the Activity Classes HomeActivity

With the layouts of the application created, it's time to create the Activity classes. When creating the other classes, make sure the name of each class matches the one you specified in the project ' s manifest fi Le earlier.

Create a new class named and set as its HomeActivity android.app.Activity superclass.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/2A/wKioL1RNINKTVOWEAAGW3wPwjF8746.jpg "title=" 6.PNG " alt= "Wkiol1rninktvoweaagw3wpwjf8746.jpg"/>


In the class ' s onCreate method, we invoke setContentView , passing in the layout we created earlier. Remember that we added a button to the activity_home layout that triggers a method named showApps . We now need to implement this method in the HomeActivity class. The implementation is pretty simple, we create a for the Intent AppsListActivity class and start it.

AppsListActivity

Create another Activity class named and set as its AppsListActivity android.app.Activity superclass. In the class ' s onCreate method, we invoke setContentView , passing in the activity_apps_list layout we created earlier.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/29/wKiom1RNIMKy4_xDAAFeXoMZ5hY484.jpg "title=" 7.PNG " alt= "Wkiom1rnimky4_xdaafexomz5hy484.jpg"/>


Even though our launcher isn ' t finished yet, you can save and run your application at this point. When you press the device's home button, you should see a pop-up asking you which launcher your ' d like to use.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/2A/wKioL1RNIVKSnWSvAAGqoY7X1mA195.jpg "title=" 8.PNG " alt= "Wkiol1rnivksnwsvaagqoy7x1ma195.jpg"/>


If you choose Simple Launcher Home, you should see your new home screens with a single button in the top right cor NER of the screen. You should also see your device's current wallpaper.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4D/29/wKiom1RNISnysZNEAAFkuErCMuE101.jpg "title=" Sh2.jpg "alt=" Wkiom1rnisnyszneaafkuercmue101.jpg "/>


Go back to Eclipse and create a class named AppDetail that would contain the details of an application, the IT package name, label , and application icon. The interface is pretty basic as can see below.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/2A/wKioL1RNIafiCmRLAACfiNVv6W8471.jpg "title=" 9.PNG " alt= "Wkiol1rniaficmrlaacfinvv6w8471.jpg"/>

6.Fetching applications

loadApps AppsListActivity in the method of the class, we use the queryIntentActivities method of the PackageManager class to fetch all the Intents . Have a category of Intent.CATEGORY_LAUNCHER . The query returns a list of the applications that can is launched by a launcher. We Loop through the results of the query and add each item to a list named apps . Take a look at the following code snippet for clarification.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/29/wKiom1RNIY-SAMHOAAH_AG5gDaQ575.jpg "title=" 10.PNG "alt=" Wkiom1rniy-samhoaah_ag5gdaq575.jpg "/>


7.Displaying the List of applications

appswith the variable containing all the details we need, we can show the list of applications using the ListView class. We create a simple and ArrayAdapter override it getView method to render the list ' s items. We then associate the with the ListView adapter.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/2B/wKioL1RNIhaw8UaiAAMlvm5ROus266.jpg "title=" 12.PNG "alt=" Wkiol1rnihaw8uaiaamlvm5rous266.jpg "/>


8.Listening for Clicks

When the user clicks ListView A item in the, the corresponding application should is launched by our launcher. We use the method of the class to create a with getLaunchIntentForPackage PackageManager Intent which we start the application. Take a look at the following code snippet.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/29/wKiom1RNIf6ADE90AAEwG74u9ZI388.jpg "title=" 13.PNG "alt=" Wkiom1rnif6ade90aaewg74u9zi388.jpg "/>


9.Putting It all Together

To make everything work together, we need to invoke loadApps , loadListView , and in the method of the addClickListener onCreate class as AppsListActivity show N below.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/2B/wKioL1RNIo3CMBn-AADNNOD5zJU494.jpg "title=" 14.PNG "alt=" Wkiol1rnio3cmbn-aadnnod5zju494.jpg "/>


Build and run your application once more for see the result. You should now is able to see the applications so can be launched if you click the button on the home screens of our LA Uncher. Click on a item to launch the corresponding application.


Conclusion

You now have your own custom launcher. It's very basic, but can add all the customizations to you want. If you want to dig deeper into custom launchers, I encourage your to take a look at the sample applications on the Android Developer Portal.


Come from a overseas

Android Create your own launcher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.