Monthly Archives: May 2013

ListView Pagination Ex-2

Main Activity

package com.rakhi577.listviewpagenavigation;

import java.util.ArrayList;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {   
    
	private ListView listview;
	private TextView title;
	
	private ArrayList<String> data;
	ArrayAdapter<String> sd;
	
	
	public int TOTAL_LIST_ITEMS = 1030;
	public int NUM_ITEMS_PAGE   = 100;
	private int noOfBtns;
	private Button[] btns;
	
	public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listview = (ListView)findViewById(R.id.list);
    title	 = (TextView)findViewById(R.id.title);
    
    Btnfooter();
    
    data = new ArrayList<String>();
    
    /**
     * The ArrayList data contains all the list items
     */
    for(int i=0;i<TOTAL_LIST_ITEMS;i++)
    {
    	data.add("This is Item "+(i+1));
    }
    
    loadList(0);
    
    CheckBtnBackGroud(0);

	}
	
	private void Btnfooter()
	{
		int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
		val = val==0?0:1;
		noOfBtns=TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;
		
		LinearLayout ll = (LinearLayout)findViewById(R.id.btnLay);
		
		btns	=new Button[noOfBtns];
		
		for(int i=0;i<noOfBtns;i++)
		{
			btns[i]	=	new Button(this);
			btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
			btns[i].setText(""+(i+1));
			
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			ll.addView(btns[i], lp);
			
			final int j = i;
			btns[j].setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) 
				{
					loadList(j);
					CheckBtnBackGroud(j);
				}
			});
		}
		
	}
	/**
	 * Method for Checking Button Backgrounds
	 */
	private void CheckBtnBackGroud(int index)
	{
		title.setText("Page "+(index+1)+" of "+noOfBtns);
		for(int i=0;i<noOfBtns;i++)
		{
			if(i==index)
			{
				btns[index].setBackgroundDrawable(getResources().getDrawable(R.drawable.box_green));
				btns[i].setTextColor(getResources().getColor(android.R.color.white));
			}
			else
			{
				btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
				btns[i].setTextColor(getResources().getColor(android.R.color.black));
			}
		}
		
	}
	
	/**
	 * Method for loading data in listview
	 * @param number
	 */
	private void loadList(int number)
	{
		ArrayList<String> sort = new ArrayList<String>();
		
		int start = number * NUM_ITEMS_PAGE;
		for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)
		{
			if(i<data.size())
			{
				sort.add(data.get(i));
			}
			else
			{
				break;
			}
		}
		sd = new ArrayAdapter<String>(this,
	            android.R.layout.simple_list_item_1,
	            sort);
		listview.setAdapter(sd);
	}
}
 

Xml Layout for this activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:gravity="center"
        android:textSize="16sp"
        android:background="@android:color/darker_gray"
        android:padding="10dp"/>

    <ListView 
	    android:id="@+id/list"
	    android:divider="#000"
	    android:dividerHeight="1dp"
	    android:cacheColorHint="#00000000"
		android:layout_width="fill_parent"
		android:layout_height="0dp"
		android:layout_weight="1"
		android:fadingEdge="none"/>
    
    <HorizontalScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
	    <LinearLayout 
	        android:id="@+id/btnLay"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:orientation="horizontal" >
		</LinearLayout>
	</HorizontalScrollView>
</LinearLayout>

ListView Page navigation Ex-2

You can find source code here

ListView Pagination Ex-1

Main Activity

package com.rakhi577.listviewpagenavigation;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {   
    
	private ListView listview;
	private TextView title;
	private Button btn_prev;
	private Button btn_next;
	
	private ArrayList<String> data;
	ArrayAdapter<String> sd;
	
	private int pageCount ;
	
	/**
	 * Using this increment value we can move the listview items
	 */
	private int increment = 0;
	
	/**
	 * Here set the values, how the ListView to be display
	 * 
	 * Be sure that you must set like this
	 * 
	 * TOTAL_LIST_ITEMS > NUM_ITEMS_PAGE
	 */
	
	public int TOTAL_LIST_ITEMS = 1030;
	public int NUM_ITEMS_PAGE   = 100;
	
	public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listview = (ListView)findViewById(R.id.list);
    btn_prev	 = (Button)findViewById(R.id.prev);
    btn_next	 = (Button)findViewById(R.id.next);
    title	 = (TextView)findViewById(R.id.title);
    
    btn_prev.setEnabled(false);
    
    data = new ArrayList<String>();
    
    /**
     * this block is for checking the number of pages
     * ====================================================
     */
    
    int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
	val = val==0?0:1;
	pageCount = TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;
	/**
	 * =====================================================
	 */
    
    /**
     * The ArrayList data contains all the list items
     */
    for(int i=0;i<TOTAL_LIST_ITEMS;i++)
    {
    	data.add("This is Item "+(i+1));
    }
    
    loadList(0);
    
    btn_next.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			
			increment++;
			loadList(increment);
			CheckEnable();
		}
	});
    
    btn_prev.setOnClickListener(new OnClickListener() {
		
		public void onClick(View v) {
			
			increment--;
			loadList(increment);
			CheckEnable();
		}
	});

	}
	
	/**
	 * Method for enabling and disabling Buttons
	 */
	private void CheckEnable()
	{
		if(increment+1 == pageCount)
		{
			btn_next.setEnabled(false);
		}
		else if(increment == 0)
		{
			btn_prev.setEnabled(false);
		}
		else
		{
			btn_prev.setEnabled(true);
			btn_next.setEnabled(true);
		}
	}
	
	/**
	 * Method for loading data in listview
	 * @param number
	 */
	private void loadList(int number)
	{
		ArrayList<String> sort = new ArrayList<String>();
		title.setText("Page "+(number+1)+" of "+pageCount);
		
		int start = number * NUM_ITEMS_PAGE;
		for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)
		{
			if(i<data.size())
			{
				sort.add(data.get(i));
			}
			else
			{
				break;
			}
		}
		sd = new ArrayAdapter<String>(this,
	            android.R.layout.simple_list_item_1,
	            sort);
		listview.setAdapter(sd);
	}
}
 

Xml Layout for this activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:gravity="center"
        android:textSize="16sp"
        android:background="@android:color/darker_gray"
        android:padding="10dp"/>

    <ListView 
	    android:id="@+id/list"
	    android:divider="#000"
	    android:dividerHeight="1dp"
	    android:cacheColorHint="#00000000"
		android:layout_width="fill_parent"
		android:layout_height="0dp"
		android:layout_weight="1"
		android:fadingEdge="none"/>
    
    <LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal" >

    <Button
        android:id="@+id/prev"
        android:text="Prev"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
    
    <Button
        android:id="@+id/next"
        android:text="Next"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
</LinearLayout>
</LinearLayout>

ListView Page navigation Ex-1

You can find source code here

ListView Names Search without sorting

Here you no need to go for big logics to sort the listview. Its very simple to use.
It will be possible with just EditText and its addTextChangedListener function

Just follow the simple steps
Step 1:

    Create an Array and initialize array with items you want to display in listview

Step 2:

    Here is the simple Logic.Just perform the loop with Characters from ‘A’ to ‘Z’.
Now its time to add the elements to listview.Here I am using seperate List adapter to display indexing listview

        for(char ch='A';ch<='Z';ch++)
        {
            boolean flag = false;
            mylist = new ArrayList<HashMap<String, String>>();
            Header = Character.toString(ch);
            array_sort.add(Header);
            for(int j=0;j<listview_names.length;j++)
            {
                if(listview_names[j].startsWith(Header))
                {
                    flag = true;
                    map = new HashMap<String, String>();
                    array_sort.add(listview_names[j]);
                    map.put("country", listview_names[j]);
                    mylist.add(map);
                }
            }
            if(flag)
            {
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, mylist, R.layout.search_list_item,
                          new String[]{"country"},new int[]{R.id.title});
                sepadapter.addSection(Header, adapter);
            }
            else
            {
                array_sort.remove(Header);
            } 
        }

Step 3:

    Append adapter to listview

         lv.setAdapter(sepadapter);
        lv.setFastScrollEnabled(true);

Step 4:

    Using EditText you can filter listview as follows.Here no need to sort the listiew.Just populate list item which matches with the entered string in the edittext

        et.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable s)
            {
                  // Abstract Method of TextWatcher Interface.
            }
            public void beforeTextChanged(CharSequence s,
                    int start, int count, int after)
            {
                // Abstract Method of TextWatcher Interface.
            }
            public void onTextChanged(CharSequence s,
                    int start, int before, int count)
            {
                textlength = et.getText().length();
                for (int i = 0; i < array_sort.size(); i++)
                {
                    if (textlength <= array_sort.get(i).length())
                    {
                        if(array_sort.get(i).toLowerCase().contains(
                                et.getText().toString().toLowerCase().trim()))
                        {
                            lv.setSelection(i);//Everything works here
                            break;
                        }
                      }
                }
             }
           });

listviewnamesearch

You can find Source code here