Wednesday, July 6, 2011

How to create dashboard layout in android ?

Ever wondered how to create a dashboard view in android? There is no out of the box layouts to achieve this. But luckily, you don't have to write one. Thanks to Roman Nurik, he has created a custom layout using which you can create a dashboard effect. For other alternatives, you can refer this stackoverflow http://stackoverflow.com/questions/2873568/android-dashboard-pattern question

In this article, i will explain you how to use this layout.

Steps
  • Get the layout source code from https://gist.github.com/882650.
  • Copy the source file to your package. (Be careful about the license and see if it suits you.)
  • Create a layout resource with DashboardLayout as the root layout. See code below.

Source code:



Output:
This will produce something like this.
Do checkout my next article on how to improvise this.

Android - AlertDialog with buttons

In previous article AlertDialog, I explained how to build a simple alert dialogs in android. Here, we will see how to add buttons like yes,no or open,cancel or something of that sort.

Steps:
  • Create your dialog using AlertDialog.Builder
  • Use API methods like setPositiveButton or setNegativeButton or both to add buttons
  • Define handlers or event listeners to the buttons.
Sample code
public class MyActivity extends Activity {
private AlertDialog confirmBox = null;
/*
* Other parts of code,omitted.
*/
private void createDialog(){
confirmBox = new AlertDialog.Builder(MyActivity.this).setTitle("Confirm").setMessage("Are you sure you want to delete this entry?").
setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/**
* TO:DO add code to handle this action.
*/
MyActivity.this.confirmBox.dismiss();
}
}).setNegativeButton("No",null).show();
}
}
As you can see, setNegativeButton() and setPositiveButton() takes two arguments. The first argument is the label/caption for the button. In our case it is the yes and no . The second argument is the onclickhandler. You can also supply null if you want android to take default action applicable. In the above example, we have supplied a handler for positive button and null for negative button.


Result

AlertDialogs in Android

In this article we will see how to display an alert dialog. Alert dialogs are more useful when you want to display any validation errors, display any messages etc..

Steps:
  • Use AlertDialog.Builder API to create/build your custome alert dialog.
  • Explore the api of AlertDialog.Builder. There are lot of customizations possible. Some listed here
Source Code

if(validationError){ // Just an example.
new AlertDialog.Builder(MyActivity.this)
.setTitle("Error")
.setMessage("Please enter valid amount")
.setNeutralButton("Close", null)
.show();
}

Refer AlertDialog.Builder API for more customizations.

Output:
The above code will give you an alert dialog like this.

Tuesday, July 5, 2011

Android List view with alternating row colors

In this article, we shall see how to apply alternating row colors to a listview.

Steps:
  • Create your own Adapter and override getView method.
  • Use your own logic and set background color inside the getview method.
Source Code


/**
*
*/
package com.blogs.adapters;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;

/**
* @author C'dine
* A simple cursor adapter. Only variation is that it displays alternate rows
* in alternate colors.
*/
public class AlternateRowCursorAdapter extends SimpleCursorAdapter{

private int[] colors = new int[] { Color.parseColor("#F0F0F0"), Color.parseColor("#D2E4FC") };
//private int[] colors = new int[] { R., 0x30808080 };
public AlternateRowCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
/**
* Display rows in alternating colors
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}


Output: