Design two images for 1. Gradient background for action-bar background, 2. Logo of the application.
( Right Click on res/layout ⇒ New ⇒ Android XML File) and type the following code.
package com.androidhive.dashboard;
public class DashboardLayout extends ViewGroup {
private static final int UNEVEN_GRID_PENALTY_MULTIPLIER = 10;
private int mMaxChildWidth = 0;
private int mMaxChildHeight = 0;
public DashboardLayout(Context context) {
public DashboardLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
public DashboardLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
mMaxChildWidth = Math.max(mMaxChildWidth, child.getMeasuredWidth());
mMaxChildHeight = Math.max(mMaxChildHeight, child.getMeasuredHeight());
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
mMaxChildWidth, MeasureSpec.EXACTLY);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
mMaxChildHeight, MeasureSpec.EXACTLY);
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
resolveSize(mMaxChildWidth, widthMeasureSpec),
resolveSize(mMaxChildHeight, heightMeasureSpec));
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
int bestSpaceDifference = Integer.MAX_VALUE;
rows = (visibleCount - 1) / cols + 1;
hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));
vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));
spaceDifference = Math.abs(vSpace - hSpace);
if (rows * cols != visibleCount) {
spaceDifference *= UNEVEN_GRID_PENALTY_MULTIPLIER;
if (spaceDifference < bestSpaceDifference) {
bestSpaceDifference = spaceDifference;
rows = (visibleCount - 1) / cols + 1;
hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));
vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));
hSpace = Math.max(0, hSpace);
vSpace = Math.max(0, vSpace);
width = (width - hSpace * (cols + 1)) / cols;
height = (height - vSpace * (rows + 1)) / rows;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
row = visibleIndex / cols;
col = visibleIndex % cols;
left = hSpace * (col + 1) + width * col;
top = vSpace * (row + 1) + height * row;
(hSpace == 0 && col == cols - 1) ? r : (left + width),
(vSpace == 0 && row == rows - 1) ? b : (top + height));
6. Now we need to create a layout file dashboard screen. Create a new xml file under src/layouts and name it as fragment_layout.xml
( Right Click on res/layout ⇒ New ⇒ Android XML File)
<com.androidhive.dashboard.DashboardLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#f8f9fe" >
android:id="@+id/btn_news_feed"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_newsfeed"
android:text="News Feed" />
android:id="@+id/btn_friends"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_friends"
android:text="Friends" />
android:id="@+id/btn_messages"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_messages"
android:text="Messages" />
android:id="@+id/btn_places"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_places"
android:id="@+id/btn_events"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_events"
android:id="@+id/btn_photos"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_photos"
</com.androidhive.dashboard.DashboardLayout>
The output of above code will be
⇒ Designing Footer
7. Create a new xml file under res/layout and name it as footer_layout.xml and type the following code.
style="@style/FooterBar" >
<TextView android:text="www.facebook.com"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#606060"
android:paddingTop="10dip"/>
⇒ Merging all layout together
So far we designed Action bar, Dashboard and Footer. Finally we need to merge all layouts in one xml file.
8. Create a new xml file under src/layouts and name it as dashboard_layout.xml and type following code.
( Right Click on res/layout ⇒ New ⇒ Android XML File)
android:id="@+id/home_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/actionbar_layout"/>
<include layout="@layout/fragment_layout"/>
<include layout="@layout/footer_layout"/>
⇒ Switching between dashboard activities.
So far we created a static dashboard which has no functioning at all. So we need to add some functionality like launching separate activity for each icon on the dashboard. In this case i had 6 icons on my dashboard, so i need 6 Activities one for each icon. In my previous article How to switch between Activities in Android i explained switching between activites.
. Open your main activity class file and paste following code.(
package com.androidhive.dashboard;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidhive.dashboard.R;
public class AndroidDashboardDesignActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_layout);
* Creating all buttons instances
Button btn_newsfeed = (Button) findViewById(R.id.btn_news_feed);
Button btn_friends = (Button) findViewById(R.id.btn_friends);
Button btn_messages = (Button) findViewById(R.id.btn_messages);
Button btn_places = (Button) findViewById(R.id.btn_places);
Button btn_events = (Button) findViewById(R.id.btn_events);
Button btn_photos = (Button) findViewById(R.id.btn_photos);
* Handling all button click events
btn_newsfeed.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), NewsFeedActivity.class);
btn_friends.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), FriendsActivity.class);
btn_messages.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), MessagesActivity.class);
btn_places.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), PlacesActivity.class);
btn_events.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), EventsActivity.class);
btn_photos.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), PhotosActivity.class);
. Dont’t forget to add an entry of all activities in your AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below
<?xml version="1.0" encoding="utf-8"?>
package="androidhive.dashboard"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:label="@string/app_name"
android:name="com.androidhive.dashboard.AndroidDashboardDesignActivity" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<activity android:name="com.androidhive.dashboard.NewsFeedActivity" ></activity>
<activity android:name="com.androidhive.dashboard.FriendsActivity" ></activity>
<activity android:name="com.androidhive.dashboard.MessagesActivity" ></activity>
<activity android:name="com.androidhive.dashboard.PlacesActivity" ></activity>
<activity android:name="com.androidhive.dashboard.EventsActivity" ></activity>
<activity android:name="com.androidhive.dashboard.PhotosActivity" ></activity>