Android: Implement a Dashboard Layout Activity
In this post I will show you the steps to quickly implement a dashboard activity in your app starting with the open source, apache licence DashboardLayout by romannurik from the Google I/O app.
A dashboard is the welcoming screen of your app. It provides a starting point for the user. A dashboard can be static or dynamic, e.g. you can incorporate live wallpapers or changing content such as news items.
On a category dashboard, entrance points to content are displayed in several categories. The categories are represented by an icon and a title, and lie full screen in a grid orientation. This lay-out allows the user to find content faster.
First start by creating a new class DashboardLayout.java within your project and copy and pasting the code from github into it. You’ll need to change package com.google.android.apps.iosched.ui.widget; to the package where it is located in your project and press ctrl+shift+O in Eclipse to re-organise imports.
Next up create a new class, DashboardActivity that extends Activity – in my case it extends GDActivity as I am also using the Greendroid library – for quick implementation of an ActionBar but yours can just extend Activity.
Add this new Activity to your AndroidManifest.xml and make it the main one, (mine is in the .ui package, if yours is in the root or elsewhere then remove .ui and change as appropriate):
<activity android:name=".ui.DashboardActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Create a new xml layout, dashboard.xml. Where mine has the tag <com.samcoles.layout.DashboardLayout> you will need to change the part prefixing DashboardLayout to the package where it is located. You may notice they all use the same @drawable, dashboard_buton_add – you will want to change this per button to something more relevant but for simplicity, here they are all going to use the same graphics.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.samcoles.layout.DashboardLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/dashboard_button_add"
style="@style/DashboardButton"
android:text="@string/activity_add"
android:drawableTop="@drawable/dashboard_button_add" />
<Button android:id="@+id/dashboard_button_viewall"
style="@style/DashboardButton"
android:text="@string/activity_viewall"
android:drawableTop="@drawable/dashboard_button_add" />
<Button android:id="@+id/dashboard_button_manage"
style="@style/DashboardButton"
android:text="@string/activity_manage"
android:drawableTop="@drawable/dashboard_button_add" />
<Button android:id="@+id/dashboard_button_personalbests"
style="@style/DashboardButton"
android:text="@string/activity_personalbests"
android:drawableTop="@drawable/dashboard_button_add" />
</com.samcoles.layout.DashboardLayout>
</LinearLayout>
You’ll notice in the dashboard.xml layout there are a number of @string, @style and @drawable resources referenced. Create or replace the string resources within strings.xml, which is created by default in Eclipse when you create a project. Also in your /res/values folder create a new file style.xml and in here place the style for DashboardButton:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="DashboardButton"> <item name="android:layout_gravity">center_vertical</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:gravity">center_horizontal</item> <item name="android:drawablePadding">2dp</item> <item name="android:textSize">14sp</item> <item name="android:textStyle">bold</item> <item name="android:textColor">#ff0000</item> <item name="android:background">@null</item> </style> </resources>
Next up create a folder /res/drawable and in here create a new file, dashboard_button_add.xml (you will need to create multiple different xml files for each button in here and reference them from the dashboard.xml layout. As I mentioned before, we are using the same one for all four buttons here.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/dashboard_add_button_selected"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="@drawable/dashboard_add_button_selected"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="@drawable/dashboard_add_button_selected" android:state_focused="true" />
<item android:drawable="@drawable/dashboard_add_button_default"
android:state_focused="false"
android:state_pressed="false" />
</selector>
Create and add the graphics referenced above to your /res/drawable-hdpi folder. These are add_button_default.png and add_button_selected.png. I made mine the same size as the ones in the Google I/O app, 144px * 96px but the proportions aren’t fixed using DashboardLayout.
Here they are in all their glory. The best part about them is that I didn’t even manage to get the horizontal line straight. Once these placeholders are in you can update them later with proper graphics as well as create new @drawable xml for each different button on the dashboard.
Next up, head back to your DashboardActivity. Within this class create a new inner class, DashboardClickListener that implements the OnClickListener interface. Implement the method onClick(). Here you can call getId() on the View passed to the handler, to get the Id of which button was clicked – this is specified in your dashboard.xml layout. Put this in a switch block and you can then handle the click event however you like, it’s likely you’ll want to just start a new activity with each, here’s mine:
private class DashboardClickListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent i = null;
switch (v.getId()) {
case R.id.dashboard_button_add:
i = new Intent(DashboardActivity.this, AddCapture.class);
break;
case R.id.dashboard_button_viewall:
i = new Intent(DashboardActivity.this, ViewAll.class);
break;
case R.id.dashboard_button_manage:
i = new Intent(DashboardActivity.this, Manage.class);
break;
case R.id.dashboard_button_personalbests:
i = new Intent(DashboardActivity.this, PersonalBests.class);
break;
default:
break;
}
if(i != null) {
startActivity(i);
}
}
}
Last but certainly not least, in your overridden onCreate() method in DashboardActivity, you need to create an instance of DashboardClickListener and attach this to each button. Below is my full overridden onCreate() method – setActionBarContentView() is just the GreenDroid alternative to setContentView() – you can use the latter instead.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setActionBarContentView(R.layout.dashboard);
//attach event handler to dash buttons
DashboardClickListener dBClickListener = new DashboardClickListener();
findViewById(R.id.dashboard_button_add).setOnClickListener(dBClickListener);
findViewById(R.id.dashboard_button_viewall).setOnClickListener(dBClickListener);
findViewById(R.id.dashboard_button_manage).setOnClickListener(dBClickListener);
findViewById(R.id.dashboard_button_personalbests).setOnClickListener(dBClickListener);
}
If all went well, you should now have a working dashboard! Remember you can have more or less buttons on your dashboard, but 6 options appears to be the standard max across most Android Apps I’ve seen and used. Aside from the Google I/O 2011 app, other examples include LinkedIn and tastecard.
A working example project based on this post can be found on github.




great job. Quite easy to read. I will try today
Very good job. Can we get the source?
Excellent. I’d love to get my hands on the source code as well.
Nice feature. Could you post your project’s source. I have many warnings when it comes to DashboardClickListener : (
Since a few people have asked for the source on this I will set it up on Github. I need to create the project from this tut again as I don’t have the original code (the same project ended up becoming Specimen Hunter) so it will be next week.
That would be great. Really appreciate your efforts.
- Jeremy
Hello Guys,
As suggested by Sam, Dashboard is really interesting concept ! I tried to explore more options on this and got very nice tuts and suggestion.
As asked by one of the reader , here is the link for sample proj with source code , also this app & tut was inspired by Google I/O Scheduler.
https://docs.google.com/leaf?id=0B0wYSnCBkoR6NzJiNTFjM2YtOTQxOC00ZjMzLTkxMjQtM2Q3ZjA4ZjY5NWYw&hl=en
A working Android project based on this tutorial (sans Greendroid) is now here – https://github.com/SamColes/Android-Samples/tree/master/DashboardActivityExample – this is my first attempt at using Github for my own projects so if there’s any problems please let me know!
Thanks a lot! It works great and it is damn easy to implement. Have a good day and keep up the good work mate.
Hi there, I tried using your dashboard layout but when I tested it on WQVGA854 resolution the button’s text shifted to the left. I changed the text into something longer. Do you have any fix for this? Thanks in advance.
I found the fix for the centering of the text here: http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/ui/widget/DashboardLayout.java
Rather than creating a separate xml file for a selector for EVERY button you ever create, consider making a selector for the background, so you can apply the same one to every button.
If your foreground image (src) has alpha transparency, the transparent bits show up as the color indicated by the selector. PNG files can handle transparency.
Plus, you can do cool things such as make a gradient button.
Code once and use many – the less coding I have to do when adding new buttons the better!
Thanks so much for sharing !!
(From France)
Cheers for this guide. I think that the Android dashboard can be improved a lot. At least for my needs.
[...] How to do this : Details [...]
Could you add a working example to add a title bar or action bar to your dashboard example? Thanks
Have you seen this post? http://www.samcoles.co.uk/mobile/android-use-greendroid-to-implement-an-actionbar/
Although I would probably reccomend ActionBarSherlock instead of GreenDroid now… I just started using it yesterday. It’s a little bit of a PITA to setup though so I might write a tutorial for that soon.