Android Tutorial : Custom adapter for ListView to create bubble shaped list rows


Hi.
I am seeing many people interested in writing their own adapters for various reasons. the primary reason being their need to implement their custom layouts in the rows.
In this tutorial, I am going to explain how you can set custom layouts to your lists and their rows.
Although, here's an in-depth article (An excerpt from his awesome book) about custom adapters from the great book of 'CommonsGuy' Mark Murphy at his website, but this tutorial is a quick way of doing that.

For this example, I have created a ListView which has a custom background, and custom rows representing a conversation in bubble format.

Here's the screenshot to make things clear:



Find the whole source code at this link.

The whole concept works like this-
  • You create two layouts:
    • One for Even rows
    • One for Odd rows
  • These layouts are designed keeping in mind that they'll be used as layouts of a row.
  • Your code has an array containing the data that will be displayed in the list view.
  • Inside your code, write a simple class extending BaseAdapter.
    • In this class we implement following methods of Android.Widget.Adapter super class, in order to get things working:
      • getCount() - returns the total number of elements in your data array
      • getIterm() - returns the data item associated with the specified position in the data set. Not implemented at this point.
      • getItemId() - returns the row id associated with the specified position in the list. Not implemented at this point.
      • getView() - returns the row that has to be drawn. This is the method we're interested in the most. It will be called every time the ListView draws a new row.
        Here, you can control what gets drawn in a particular row, by selecting a layout and setting data into it.
        In our example, we are checking whether the position of the row is even or odd, and according to that, we are setting the row's layout.
  • I have also set a background to the layout which holds the list.
  • A list has a transparent background in normal conditions which makes you see the image that you set as the layout background, but when the List is scrolled, it doesn't remain transparent any more.
  • If you want you background image to be visible while scrolling too, set cacheColorHint of your ListView.

Refer to the project for more, and revert for feedback.

Download the source code here.
Happy Coding!

Show Splash Screen for fixed amount of time in Android Apps


You'll be into situations where you have to present a user with a splash screen that stays for sometime and then loads the main application and goes away.

This post demonstrates a code that displays a splash screen for a fixed amount of time and then loads the main screen.

This code isn't for the situation where you have to display a splash screen until you finish doing some work or load data from the web.
It will be covered in a different post.

Read the code, and understand the things from the comments

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;

public class SplashScreen extends Activity {

private final int DISPLAY_LENGTH = 1000;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash_screen);

/* New Handler to start the Menu-Activity
* and close this Splash-Screen after DISPLAY_LENGTH/1000 seconds.*/
new Handler().postDelayed(new Runnable(){

@Override
public void run() {
/* Create an Intent that will start the Main-Activity. */
Intent mainIntent = new Intent(SplashScreen.this,Main.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
}}, DISPLAY_LENGTH);
}
}