Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Customised Home Screen

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 323
    Comment on it

    In Android when the phone starts then the screen that appears first is the "Launcher Home Screen". We can write an application to create this home screen and can replace the default home screen with our own created one.

    You can create many attractive home screen according to your requirements, a simple example is given here just to provide the basic idea to develop the Launcher Screen:-


    Part(1): Manifest File:-

    Firstly the main important implementation is your manifest file as given below:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="customizedLauncherScreen"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />
    
        <application
            android:launchMode="singleTask"
            android:clearTaskOnLaunch="true"
            android:stateNotNeeded="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".HomeScreen"
                android:label="@string/app_name"
                android:launchMode="singleTask"
                android:excludeFromRecents="true"
                android:screenOrientation="nosensor">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <!-- main point for home screen-->
                    <category android:name="android.intent.category.HOME" />
                    <category android:name="android.intent.category.DEFAULT" />
    
                </intent-filter>
            </activity>
        </application>
    </manifest>
    


    Part(2): Code Implementation:-

    1. Now we will implement the code to create our launcher. The very first task is to get all the installed application of the device so that we can display it. For this, we will use the "AsyncTaskLoader"

    package customizedLauncherScreen;
    
    import android.content.Context;
    import android.content.pm.ApplicationInfo;
    import android.content.pm.PackageManager;
    import android.support.v4.content.AsyncTaskLoader;
    
    import java.text.Collator;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Comparator;
    
    public class AppsLoader extends AsyncTaskLoader<ArrayList<AppModel>> {
        ArrayList<AppModel> mInstalledApps;
    
        final PackageManager mPm;
        PackageIntentReceiver mPackageObserver;
    
        public AppsLoader(Context context) {
            super(context);
    
            mPm = context.getPackageManager();
        }
    
        @Override
        public ArrayList<AppModel> loadInBackground() {
            // retrieve the list of installed applications
            List<ApplicationInfo> apps = mPm.getInstalledApplications(0);
    
            if (apps == null) {
                apps = new ArrayList<ApplicationInfo>();
            }
    
            final Context context = getContext();
    
            // create corresponding apps and load their labels
            ArrayList<AppModel> items = new ArrayList<AppModel>(apps.size());
            for (int i = 0; i < apps.size(); i++) {
                String pkg = apps.get(i).packageName;
    
                // only apps which are launchable
                if (context.getPackageManager().getLaunchIntentForPackage(pkg) != null) {
                    AppModel app = new AppModel(context, apps.get(i));
                    app.loadLabel(context);
                    items.add(app);
                }
            }
    
            // sort the list
            Collections.sort(items, ALPHA_COMPARATOR);
    
            return items;
        }
    
        @Override
        public void deliverResult(ArrayList<AppModel> apps) {
            if (isReset()) {
                // An async query came in while the loader is stopped.  We
                // don't need the result.
                if (apps != null) {
                    onReleaseResources(apps);
                }
            }
    
            ArrayList<AppModel> oldApps = apps;
            mInstalledApps = apps;
    
            if (isStarted()) {
                // If the Loader is currently started, we can immediately
                // deliver its results.
                super.deliverResult(apps);
            }
    
            // At this point we can release the resources associated with
            // 'oldApps' if needed; now that the new result is delivered we
            // know that it is no longer in use.
            if (oldApps != null) {
                onReleaseResources(oldApps);
            }
        }
    
        @Override
        protected void onStartLoading() {
            if (mInstalledApps != null) {
                // If we currently have a result available, deliver it
                // immediately.
                deliverResult(mInstalledApps);
            }
    
            // watch for changes in app install and uninstall operation
            if (mPackageObserver == null) {
                mPackageObserver = new PackageIntentReceiver(this);
            }
    
            if (takeContentChanged() || mInstalledApps == null ) {
                // If the data has changed since the last time it was loaded
                // or is not currently available, start a load.
                forceLoad();
            }
        }
    
        @Override
        protected void onStopLoading() {
            // Attempt to cancel the current load task if possible.
            cancelLoad();
        }
    
        @Override
        public void onCanceled(ArrayList<AppModel> apps) {
            super.onCanceled(apps);
    
            // At this point we can release the resources associated with 'apps'
            // if needed.
            onReleaseResources(apps);
        }
    
        @Override
        protected void onReset() {
            // Ensure the loader is stopped
            onStopLoading();
    
            // At this point we can release the resources associated with 'apps'
            // if needed.
            if (mInstalledApps != null) {
                onReleaseResources(mInstalledApps);
                mInstalledApps = null;
            }
    
            // Stop monitoring for changes.
            if (mPackageObserver != null) {
                getContext().unregisterReceiver(mPackageObserver);
                mPackageObserver = null;
            }
        }
    
        /**
         * Helper method to do the cleanup work if needed, for example if we're
         * using Cursor, then we should be closing it here
         *
         * @param apps
         */
        protected void onReleaseResources(ArrayList<AppModel> apps) {
            // do nothing
        }
    
    
        /**
         * Perform alphabetical comparison of application entry objects.
         */
        public static final Comparator<AppModel> ALPHA_COMPARATOR = new Comparator<AppModel>() {
            private final Collator sCollator = Collator.getInstance();
            @Override
            public int compare(AppModel object1, AppModel object2) {
                return sCollator.compare(object1.getLabel(), object2.getLabel());
            }
        };
    }
    


    1. The second task is to store the installed application in the model which we had gotten in the first step.
    package customizedLauncherScreen;
    
    import android.content.Context;
    import android.content.pm.ApplicationInfo;
    import android.graphics.drawable.Drawable;
    
    import java.io.File;
    
    
    public class AppModel {
    
        private final Context mContext;
        private final ApplicationInfo mInfo;
    
        private String mAppLabel;
        private Drawable mIcon;
    
        private boolean mMounted;
        private final File mApkFile;
    
        public AppModel(Context context, ApplicationInfo info) {
            mContext = context;
            mInfo = info;
    
            mApkFile = new File(info.sourceDir);
        }
    
        public ApplicationInfo getAppInfo() {
            return mInfo;
        }
    
        public String getApplicationPackageName() {
            return getAppInfo().packageName;
        }
    
        public String getLabel() {
            return mAppLabel;
        }
    
        public Drawable getIcon() {
            if (mIcon == null) {
                if (mApkFile.exists()) {
                    mIcon = mInfo.loadIcon(mContext.getPackageManager());
                    return mIcon;
                } else {
                    mMounted = false;
                }
            } else if (!mMounted) {
                // If the app wasn't mounted but is now mounted, reload
                // its icon.
                if (mApkFile.exists()) {
                    mMounted = true;
                    mIcon = mInfo.loadIcon(mContext.getPackageManager());
                    return mIcon;
                }
            } else {
                return mIcon;
            }
    
            return mContext.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
        }
    
    
        void loadLabel(Context context) {
            if (mAppLabel == null || !mMounted) {
                if (!mApkFile.exists()) {
                    mMounted = false;
                    mAppLabel = mInfo.packageName;
                } else {
                    mMounted = true;
                    CharSequence label = mInfo.loadLabel(context.getPackageManager());
                    mAppLabel = label != null ? label.toString() : mInfo.packageName;
                }
            }
        }
    }
    


    1. The third task is to get a fragment where we can show our stored installed application which we had gotten in the previous step.

    package customizedLauncherScreen;
    
    import android.content.Context;
    import android.content.pm.ApplicationInfo;
    import android.graphics.drawable.Drawable;
    
    import java.io.File;
    
    
    public class AppModel {
    
        private final Context mContext;
        private final ApplicationInfo mInfo;
    
        private String mAppLabel;
        private Drawable mIcon;
    
        private boolean mMounted;
        private final File mApkFile;
    
        public AppModel(Context context, ApplicationInfo info) {
            mContext = context;
            mInfo = info;
    
            mApkFile = new File(info.sourceDir);
        }
    
        public ApplicationInfo getAppInfo() {
            return mInfo;
        }
    
        public String getApplicationPackageName() {
            return getAppInfo().packageName;
        }
    
        public String getLabel() {
            return mAppLabel;
        }
    
        public Drawable getIcon() {
            if (mIcon == null) {
                if (mApkFile.exists()) {
                    mIcon = mInfo.loadIcon(mContext.getPackageManager());
                    return mIcon;
                } else {
                    mMounted = false;
                }
            } else if (!mMounted) {
                // If the app wasn't mounted but is now mounted, reload
                // its icon.
                if (mApkFile.exists()) {
                    mMounted = true;
                    mIcon = mInfo.loadIcon(mContext.getPackageManager());
                    return mIcon;
                }
            } else {
                return mIcon;
            }
    
            return mContext.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
        }
    
    
        void loadLabel(Context context) {
            if (mAppLabel == null || !mMounted) {
                if (!mApkFile.exists()) {
                    mMounted = false;
                    mAppLabel = mInfo.packageName;
                } else {
                    mMounted = true;
                    CharSequence label = mInfo.loadLabel(context.getPackageManager());
                    mAppLabel = label != null ? label.toString() : mInfo.packageName;
                }
            }
        }
    }
    


    1. The next step is to fetch all our application to the grid view by using the adapter as coded below:
    package customizedLauncherScreen;
    
    import android.annotation.TargetApi;
    import android.content.Context;
    import android.os.Build;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.Collection;
    
    import customizedLauncherScreen.R;
    
    
    public class AppListAdapter extends ArrayAdapter<AppModel> {
        private final LayoutInflater mInflater;
    
        public AppListAdapter (Context context) {
            super(context, android.R.layout.simple_list_item_2);
    
            mInflater = LayoutInflater.from(context);
        }
    
        public void setData(ArrayList<AppModel> data) {
            clear();
            if (data != null) {
                addAll(data);
            }
        }
    
        @Override
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public void addAll(Collection<? extends AppModel> items) {
            //If the platform supports it, use addAll, otherwise add in loop
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                super.addAll(items);
            }else{
                for(AppModel item: items){
                    super.add(item);
                }
            }
        }
    
        /**
         * Populate new items in the list.
         */
        @Override public View getView(int position, View convertView, ViewGroup parent) {
            View view;
    
            if (convertView == null) {
                view = mInflater.inflate(R.layout.list_item_icon_text, parent, false);
            } else {
                view = convertView;
            }
    
            AppModel item = getItem(position);
            ((ImageView)view.findViewById(R.id.icon)).setImageDrawable(item.getIcon());
            ((TextView)view.findViewById(R.id.text)).setText(item.getLabel());
    
            return view;
        }
    }
    


    1. Last Step is to implement the Receiver Class so that if new app is installed then uploader can be updated

    package customizedLauncherScreen;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    
    
    public class PackageIntentReceiver extends BroadcastReceiver {
    
        final AppsLoader mLoader;
    
        public PackageIntentReceiver(AppsLoader loader) {
            mLoader = loader;
    
            IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
            filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
            filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
            filter.addDataScheme("package");
            mLoader.getContext().registerReceiver(this, filter);
    
            // Register for events related to sdcard installation.
            IntentFilter sdFilter = new IntentFilter();
            sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
            sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
            mLoader.getContext().registerReceiver(this, sdFilter);
        }
    
        @Override public void onReceive(Context context, Intent intent) {
            // Tell the loader about the change.
            mLoader.onContentChanged();
        }
    
    }
    


    Part(3): Layouts:-

    <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"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".HomeScreen">
    
        <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="customizedLauncherScreen.AppsGridFragment"
                android:id="@+id/apps_grid" />
    
    </RelativeLayout>
    



 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: