How to set the app Launcher icon to a pop-up box in Android

Source: Internet
Author: User

Look first.



This is the effect of our own APK click


Below is the layout file

Activity_main.xml Main layout file

<linearlayout 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= ". Mainactivity "
android:orientation= "Vertical" >
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:gravity= "Center"
android:layout_margintop= "15DP"
android:text= "@string/app_name"/>


<gridview
Android:id= "@+id/allapps"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"/>


</LinearLayout>


I use a GridView to make containers


Below is a single item layout file

Application_layout.xml


<?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"
android:gravity= "Center"
>

<imageview
Android:id= "@+id/app_icon"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_margintop= "15DP"/>

<textview
Android:id= "@+id/app_title"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "12SP"
android:gravity= "Center"/>
</LinearLayout>

Below is Androidmanifest.xml, there are several and common apk different place


<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.wind.lancherdemo"
Android:versioncode= "1"
Android:versionname= "1.0" >


<uses-sdk
Android:minsdkversion= "17"
Android:targetsdkversion= "/>"


<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name" >
<activity
Android:name= "Com.wind.lancherdemo.MainActivity"
Android:theme= "@android: Style/theme.dialog" <!--this change app theme to dialog-->
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>


<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>


</manifest>


Below is the source file


Package Com.wind.lancherdemo;


Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;


Import android.app.Activity;
Import Android.content.ComponentName;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.pm.PackageManager;
Import Android.content.pm.ResolveInfo;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.Window;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.BaseAdapter;
Import Android.widget.GridView;
Import Android.widget.ImageView;
Import Android.widget.TextView;


public class Mainactivity extends Activity implements onitemclicklistener{
Private GridView Mgridview;
Private Context Mcontext;
Private Packagemanager Mpackagemanager;
Private list<resolveinfo> Mallapps;
Private list<resolveinfo> Mshowapps = new arraylist<resolveinfo> ();
private static final string[] Mshowapppkgnames = {"Com.android.contacts", "Com.android.mms", "Com.android.browser"}; This place can add the APK package name we need to filter
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);

Setupviews ();
}






private void Setupviews () {
Mcontext = Mainactivity.this;
Mpackagemanager = Getpackagemanager ();
Mgridview = (GridView) Findviewbyid (R.id.allapps);
Bindallapps ();

Mgridview.setadapter (New Griditemadapter (Mcontext, Mshowapps)); This place when set the GridView adapter, do not know can go online search for specific tutorials
Mgridview.setnumcolumns (3);
Mgridview.setonitemclicklistener (this);
}






private void Bindallapps () {
Intent mainintent = new Intent (intent.action_main,null);
Mainintent.addcategory (Intent.category_launcher);
Mallapps = mpackagemanager.queryintentactivities (mainintent, 0);//This place is where we filter out the APK we want based on all the APK we've installed, The purpose of this is that you delete some of the applications we need, and our program is still normal.
for (ResolveInfo App_item:mallapps) {
String pkg = app_item.activityInfo.packageName;
for (int i = 0; i < mshowapppkgnames.length; i++) {
if (mshowapppkgnames[i].equals (pkg)) {
Mshowapps.add (App_item);
}
}
}
Collections.sort (Mshowapps, New Resolveinfo.displaynamecomparator (Mpackagemanager));
}





This is based on our click-through to the specific application
@Override
public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {
ResolveInfo res = mshowapps.get (position);
String pkg = res.activityInfo.packageName;
String cls = Res.activityInfo.name;

ComponentName component = new ComponentName (pkg, CLS);

Intent i = new Intent ();
I.setcomponent (component);
StartActivity (i);
}
This place is where we rewrite our GridView adapter
Private class Griditemadapter extends baseadapter{
Private context context;
Private list<resolveinfo> Resinfo;



Public Griditemadapter (context context, list<resolveinfo> Resinfo) {
This.context = context;
This.resinfo = Resinfo;
}


@Override
public int GetCount () {
TODO auto-generated Method Stub
return Resinfo.size ();
}


@Override
Public Object getItem (int position) {
TODO auto-generated Method Stub
return Resinfo.get (position);
}


@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}

The purpose of this place with Viewholder is to improve the app's speed by not having to reconstruct our convertview and find ImageView and textview every time.
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Viewholder holder = null;

if (Convertview = = null) {
Convertview = Layoutinflater.from (context). Inflate (r.layout.application_layout, NULL);
Holder = new Viewholder ();
Holder.mappicon = (ImageView) Convertview.findviewbyid (R.id.app_icon);
Holder.mapptitle = (TextView) Convertview.findviewbyid (r.id.app_title);
Convertview.settag (holder);
} else {
Holder = (viewholder) convertview.gettag ();
}

ResolveInfo res = resinfo.get (position);
Holder.mAppIcon.setImageDrawable (Res.loadicon (Mpackagemanager));
Holder.mAppTitle.setText (Res.loadlabel (Mpackagemanager). toString ());

return convertview;
}

Private class Viewholder {
ImageView Mappicon;
TextView Mapptitle;
}
}


}



How to set the app Launcher icon to a pop-up box in Android

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.