Welcome to phase two of this custom launcher educational! If you haven’t already learn phase considered one of this sequence, learn it and are available again. To a good better extent than phase one, that is a reasonably complex undertaking. If you aren’t aware of categories, the Android SDK, and java, I like to recommend you additionally perform a little extra background studying first.
Still with me?
Good. If you’ve adopted at the side of phase one, you must now have a launcher that lots whilst you boot your telephone. It must even have a running app drawer. For the instant, that app drawer is a little sluggish and there’s most effective a unmarried web page showing a unmarried app although. In different phrases, we’ve got paintings to do!
First, it’s time to load the icons into the drawer in a separate thread. This will steer clear of busying the primary UI thread, which means that the checklist will load in the background, able to use.
To do that, we will be able to be the use of one thing referred to as ASyncTask.
Speeding up the apps drawer
Here’s the plan.
Make your apps checklist public and create a approach in our radaptor.java elegance to upload new pieces to that checklist:
public RAdapter(Context c)
We don’t want to create our checklist in the constructor anymore, so we’ll simply claim it.
Instead, upload the next subclass to AppsDrawer.java to carry out the similar factor with AsyncTask. This will carry out the similar motion in a separate thread, so the app can nonetheless handle consumer interactions whilst running thru it. The code must glance acquainted:
public elegance myThread extends AsyncTask<Void, Void, String>
Of direction you additionally want to delete the replica code from the adapter elegance. We can then merely cause our ASync elegance in the onCreate() approach of the AppsDawer.java record:
Try operating your launcher and the apps drawer must now spring to existence beautiful seamlessly. The eagle-eyed amongst you’re going to even have spotted I created any other new approach:
public void updateStuff()
Notice the process radaptor.notifiyItemInserted(). This permits the dynamic including of things to the checklist in our recyclers. It will likely be helpful in the longer term for you severe launcher designers, as a result of it may possibly pay attention for newly put in or deleted apps and replace the view accordingly.
This all appears a lot higher however there’s one thing flawed nonetheless. At the instant, we’re calling onCreate() and making a new app drawer each and every time the job is created. To steer clear of this going down, we would like to upload a line to our manifest in the <software> tag for AppsDrawer:
To be further secure, we will be able to additionally override the onBackPressed() approach in our AppsDrawer.java record.
The app drawer has gotten faster, however it could be even higher if it was once created when the app launches, slightly than when the consumer first clicks the app drawer button. That means, it could be able earlier than it was once clicked. We may just bend over backwards to do that, however the most productive resolution is to position our app drawer into a fragment — shelve that for a second, we’ll come again to it.
Fragments are extremely tough for construction dynamic Americaand they are easiest for our launcher!
Fragments additionally supply one of the best ways to create a great sequence of homescreens to swipe thru when opting for our apps!
We’ll be developing fragments after which swiping thru them with ViewPager.
Basically a fragment is an job-lite. It has its personal existence-cycle and will comprise numerous perspectives however multiple fragments will also be visual onscreen without delay (in contrast to an job). Fragments too can behave like items, in that a couple of circumstances of the similar fragment can exist without delay. This once more lends itself neatly to a homepage, as a result of customers may just upload and take away homepages as vital to space numerous other apps and widgets. Fragments are extremely tough for construction dynamic Americaand so they’re easiest for our launcher!
To create a fragment, pass to File > New > Fragment. You’ll then give you the option to create a new fragment, which we will be able to name Homescreen. Untick the manufacturing facility strategies and callbacks bins and click on end. This must generate a new XML record, fragment_homescreen.xml, and a new Java record, Homescreen.java, similar to an job.
For now, upload any other symbol view and position it in the middle of the display screen the use of gravity. Give it the ID “icon” and provides the fragment itself the ID “home.”
To get this to run inside of our fragment, we sadly can’t simply drag and drop the onClick() code from earlier than. Instead, read about the code beneath to see how the entire thing must paintings:
public elegance Homescreen extends Fragment implements View.OnClickListener
It’s a little extra fiddly, however you must be in a position to opposite engineer this to paintings as you require it to. Just override the more than a few onClicks.
Notice that I used to be in a position to use the getActivityIcon from MainJob as a result of I made the process static. Static strategies from different categories are usable with out developing a couple of circumstances of that elegance. You see, there’s approach to my insanity (and my strategies too)!
Add the fragment to your activity_main.xml and prepare it properly above the app drawer button. You’ll now be in a position to see the Chrome icon button simply as earlier than. It’s a lot of code to succeed in the very same result, however that’s programming for you!
Of direction, the true explanation why we went to all of this effort was once as a result of it could permit us to do extra thrilling issues going ahead. Now we will be able to create a couple of fragments the use of the very same Java code and the very same XML.
That lets run two circumstances of the similar display screen and alter the icons that show in accordance with the ID we give to each and every one in the XML!
It will get higher, too.
Using fragments additionally signifies that we will be able to use ViewPager to scroll thru our homescreens, as could be standard in any launcher app. ViewPager additionally offers us the choice to animate the monitors as we transition between them.
Using fragments additionally signifies that we will be able to use ViewPager to scroll thru our homescreens as you are expecting to be in a position to in any launcher app.
You can in finding the legit documentation for the use of ViewPager right here. It’s no longer too tough, fortunately.
First, we’d like to drag and drop our ViewPager into the activity_main.xml, simply as in some other view. Just stick it the place the fragment is lately.
Now we’d like to create any other elegance. This one will likely be referred to as “HomescreenAdapter” and can prolong FragmentStatePageAdapter. This adapter will position our fragments within the ViewPager.
It looks as if this:
non-public elegance HomescreenAdapter extends FragmentStatePagerAdapter }
We want a world variable like static ultimate int NUM_PAGES to outline alternatively many pages you wish to have. You would possibly no longer need it to be a “final” in long run although, as maximum apps permit their customers to upload further homepages.
Set up the adapter in your MainJob’s onCreate() approach:
mPager = (ViewPager) in findingViewById(R.identity.homescreenPager); mPagerAdapter = new HomescreenAdapter(getSupportFragmentSupervisor()); mPager.setAdapter(mPagerAdapter);
Load that up and also you must now have a swipe-in a position portion of the display screen, with each and every one appearing our Chrome icon. The app drawer button must additionally keep proper the place it’s on the backside of the display screen.
In the longer term, chances are you’ll want to adapt this to display other icons on each and every web page. You would do this via passing the place int from getItem() as a package and the use of a transfer commentary to load other icons or layouts.
With that, you presently have a sequence of monitors wherein you’ll swipe, in addition to a fantastically snappy app drawer! This is beginning to feel and look a lot like a actual launcher. At the ground of that legit documentation, you’ll even upload a vary of fancy animations similar to the most productive launchers in the market!
Launchers don’t simply display icons although: they display widgets too.
The very first thing you’ll want to do to get that to paintings is upload this permission to your manifest:
<makes use of-permission android:title="android.permission.BIND_APPWIDGET" />
Ignore the caution about permission most effective being granted to gadget apps. These days you additionally want to grant your app permission at runtime the use of a conversation.
You’re going to use an AppWidgetHost elegance in order to set up and show widgets, which could have its personal ID. This ID is essential and wishes to stay consistent so the widgets know they’re speaking together with your app.
Each widget will likewise be given its personal ID when it’s certain to your host, which is able to occur each and every time the app launcher is loaded. AppWidgetHostView will likely be a container showing the host and widget. You’ll use the choices package to move data to and from widgets, like the scale at which they must be displayed and what data from inside the app they’ll be appearing, amongst different issues.
This is a shockingly concerned procedure, particularly while you get started doing such things as saving which widgets the consumer desires to use and the settings they’ve selected. You’ll want to use a couple of XML information and categories simply to get the fundamentals running. This is just too concerned to undergo step-via-step in this put up.
You can in finding additional info on how to host widgets right here however that is reasonably temporary. You too can in finding running code for a complete launcher right here. The code used in the academic comes from this, so should you learn thru that and raise the snippets from the undertaking, you’ll opposite engineer it to the purpose the place it’ll run.
Reverse engineering and looking for clues could be very ceaselessly the truth of programming on Android, particularly if you end up making an attempt to do one thing this is uncommon and no longer required for nearly all of packages.
I like to recommend you get started out via checking out this in a separate job inside your undertaking (and even a separate undertaking totally) and transfer it to your homepage fragments most effective while you’ve were given the entirety running properly. Reverse engineering and looking for clues could be very ceaselessly the truth of programming on Android, particularly whilst you’re making an attempt to do one thing uncommon, or needless for many packages.
You’ll additionally want to take a look at the phase on the backside of the documentation in order to improve this procedure for Android four.zero and above.
There is quite a bit extra to do!
Like I mentioned, construction a launcher is a giant enterprise. If you’ve controlled to paintings your means throughout the headache of including widgets, there’s nonetheless quite a lot of different issues price including:
- Icon packs
- Handling display screen rotations (if you select to do this!)
- Letting customers drag and drop their icons across the display screen
Plus no matter will make your app distinctive!
That’s no small enterprise, however it may be a specifically a laugh and rewarding task to tackle and the effects will likely be one thing that you just (and any customers) will use each and every unmarried day.
Good good fortune, percentage your ideas at the procedure in the feedback beneath and let me know should you’d like to see the addition of widgets (or anything for that subject) treated in a separate put up!