Wednesday, July 6, 2011

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:

Monday, June 13, 2011

A simple form with Linear layout



Creating a user input form layout is very easy. I'm presenting one such form for you reference.











The above layout will produce an effect like this.

About this blog

This is a placeholder text.

Hello World!!!!