Android Custom List View Creation

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyActivity extends Activity {

 public ArrayList<String> oneList = new ArrayList<String>();
 public ArrayList<String> twoList = new ArrayList<String>();
 private oneAdapter oneAdapter;
 private ListView oneListView;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.yourXml);
 }

 @Override
 protected void onResume() {
  super.onResume();
  setContentView(R.layout.yourXml);
  listCreation();
  oneListView = (ListView) findViewById(R.id.list_medication);
  oneAdapter = new oneAdapter(this);
  oneListView.setAdapter(oneAdapter);

 }

 /**
  * 
  * Adapter for Custom list view
  * 
  * 
  */
 private class oneAdapter extends BaseAdapter {
  private LayoutInflater mInflater;

  public oneAdapter(Context context) {
   mInflater = LayoutInflater.from(context);

  }

  public int getCount() {
   return oneList.size();
  }

  public Object getItem(int position) {
   return position;
  }

  public long getItemId(int position) {
   return position;
  }

  public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
   if (convertView == null) {
    convertView = mInflater.inflate(R.layout.yourCustomList, null);
    holder = new ViewHolder();
    holder.textViewOne = (TextView) convertView
    .findViewById(R.id.text_value_one);
    holder.textViewTwo = (TextView) convertView
    .findViewById(R.id.text_value_two);
    convertView.setTag(holder);
    } else {
    holder = (ViewHolder) convertView.getTag();
    }
    holder.textViewOne.setText(oneList.get(position));
    holder.textViewTwo.setText(twoList.get(position));
    return convertView;
  }

  class ViewHolder {
   TextView textViewOne;
   TextView textViewTwo;
  }
 }

 private void listCreation() {
  oneList.add("valueOne");
  oneList.add("valueOne");
  oneList.add("valueOne");
  oneList.add("valueOne");
  oneList.add("valueOne");
  oneList.add("valueOne");
  oneList.add("valueOne");

  twoList.add("valueTwo");
  twoList.add("valueTwo");
  twoList.add("valueTwo");
  twoList.add("valueTwo");
  twoList.add("valueTwo");
  twoList.add("valueTwo");
  twoList.add("valueTwo");

 }

}


yourcustomlist.xml


This xml is used to design your custom list. You can design your xml as per your requirement. You have to create a separate xml for your List view.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="wrap_content">

 <RelativeLayout android:layout_width="fill_parent"
  android:background="#184C7C" android:id="@+id/list_layout"
  android:layout_height="50dip">

   <TextView android:id="@+id/text_value_one"
  android:layout_marginLeft="5dip" android:textStyle="bold"
  android:textColor="#FFFFFF" android:layout_marginTop="5dip"
  android:layout_height="fill_parent" android:textSize="15dip"
  android:layout_width="fill_parent" 
  android:layout_toLeftOf="@+id/text_value_two" 
  android:layout_marginRight="5dip"/>

   <TextView android:id="@+id/text_value_two"
  android:textStyle="bold"
  android:textColor="#FFFFFF" android:textSize="15dip"
  android:layout_alignParentRight="true" 
  android:layout_width="80dip" 
  android:layout_height="wrap_content" 
  android:layout_centerVertical="true"/>

 </RelativeLayout>

</LinearLayout>

Comments

Popular posts from this blog

Input filter (space not allowed) for EditText

Android Versions details

English - Formal and Informal words