Category Archives: Android

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

Advertisements

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

Image Search in the ListView in Android

Note: Extend an activity by ListActivity
package com.rakhi577.listviewimagesearch;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

	private EditText et;
	private String[] listview_names = {"India","Indonesia","Iceland","Ireland",
									   "Netherlands","Bangladesh","Great Britain",
									   "China","Finland"};
	
	private int[] listview_images   = {R.drawable.india,R.drawable.indonesia,
									   R.drawable.iceland,R.drawable.ireland,
									   R.drawable.netherlands,R.drawable.bangladesh,
									   R.drawable.great_britain,R.drawable.china,
									   R.drawable.finland};
	
	private ArrayList<String> array_sort;
	private ArrayList<Integer> image_sort;
	int textlength=0;
	
	private ListView lv;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et 	= 	(EditText) findViewById(R.id.EditText01);
		lv	=	(ListView) findViewById(android.R.id.list);
		
		array_sort=new ArrayList<String> (Arrays.asList(listview_names));
		image_sort=new ArrayList<Integer>();
		for (int index = 0; index < listview_images.length; index++)
	    {
			image_sort.add(listview_images[index]);
	    }
		
		setListAdapter(new bsAdapter(this));


		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();
				array_sort.clear();
				image_sort.clear();
				for (int i = 0; i < listview_names.length; i++)
				{
					if (textlength <= listview_names[i].length())
					{
						/***
						 * If you want to highlight the countries which start with 
						 * entered letters then choose this block. 
						 * And comment the below If condition Block
						 */
						/*if(et.getText().toString().equalsIgnoreCase(
								(String)
								listview_names[i].subSequence(0,
										textlength)))
						{
							array_sort.add(listview_names[i]);
							image_sort.add(listview_images[i]);
						}*/
						
						/***
						 * If you choose the below block then it will act like a 
						 * Like operator in the Mysql
						 */
						
						if(listview_names[i].toLowerCase().contains(
								et.getText().toString().toLowerCase().trim()))
						{
							array_sort.add(listview_names[i]);
							image_sort.add(listview_images[i]);
						}
                      }
				}
				AppendList(array_sort,image_sort);
				}
				});
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0,
			                    View arg1, int position, long arg3)
			{
			   Toast.makeText(getApplicationContext(), array_sort.get(position),
					   Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	public void AppendList(ArrayList<String> str,ArrayList<Integer> img)
	{
		setListAdapter(new bsAdapter(this));
	}
	
	public class bsAdapter extends BaseAdapter
    {
        Activity cntx;
        public bsAdapter(Activity context)
        {
            // TODO Auto-generated constructor stub
            this.cntx=context;

        }

        public int getCount()
        {
            // TODO Auto-generated method stub
            return array_sort.size();
        }

        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return array_sort.get(position);
        }

        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return array_sort.size();
        }

        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View row=null;
            
            LayoutInflater inflater=cntx.getLayoutInflater();
            row=inflater.inflate(R.layout.search_list_item, null);
            
            TextView   tv	=	(TextView)	row.findViewById(R.id.title);
            ImageView im	=	(ImageView)	row.findViewById(R.id.imageview);
            
            tv.setText(array_sort.get(position));
            im.setImageDrawable(getResources().getDrawable(image_sort.get(position)));
            
        return row;
        }
    }
}
 

Xml Layout for this activity

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
	android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    
	<EditText 
	    android:id="@+id/EditText01"
		android:layout_height="40dp"
		android:layout_width="fill_parent"
		android:hint="search"/>
	
	 <ListView android:id="@+id/android:list"
	    android:cacheColorHint="#00000000"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
</LinearLayout>

xml file for the list items

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

	<TextView
		android:id="@+id/title"
		android:textColor="#000"
	    android:layout_width="0dp"
	    android:layout_weight="1"
	    android:layout_margin="10dp"
	    android:layout_height="wrap_content"/>
	    
	<ImageView 
	    android:id="@+id/imageview"
	    android:layout_width="30dp"
	    android:layout_height="30dp"
	    android:src="@drawable/ic_launcher"
	    android:layout_marginRight="10dp"
	    android:contentDescription="@string/app_name"/>

</LinearLayout>

listviewimagesearch

source code

Bar Chart in Android With out any Built in jars

Main Acitvity


package com.rakhi577.chart;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private LinearLayout lay;
	HorizontalListView listview;
	private double highest;
	private int[] grossheight; 
	private int[] netheight;
		private Double[] grossSal= {15000.0,15000.0,15000.25,15000.1,
								15000.0,15000.0,15000.0,15000.0,
								15000.25,15000.1,15000.0,15000.0};
	
	private Double[] netSal = {12000.0,13000.0,14000.25,10000.1,
								10000.0,9000.0,12000.0,13000.0,
								14000.25,10000.1,10000.0,9000.0};

	private String[] datelabel = {"Jan 12","Feb 12","Mar 12",
								  "Apr 12","May 12","Jun 12",
								  "Jul 12","Aug 12","Sep 12",
								  "Oct 12","Nov 12","Dec 12"};
	

	public void onCreate(Bundle savedInstance)
	{
		super.onCreate(savedInstance);
		setContentView(R.layout.main);
		lay = (LinearLayout)findViewById(R.id.linearlay);
		listview = (HorizontalListView) findViewById(R.id.listview);
        
		List<Double> b = Arrays.asList(grossSal);
        highest = (Collections.max(b));

        netheight = new int[netSal.length];
        grossheight= new int[grossSal.length];
    	//updateSizeInfo();
		
    }
	
	public class bsAdapter extends BaseAdapter
    {
        Activity cntx;
        String[] array;
        public bsAdapter(Activity context,String[] arr)
        {
            // TODO Auto-generated constructor stub
            this.cntx=context;
            this.array = arr;

        }

        public int getCount()
        {
            // TODO Auto-generated method stub
            return array.length;
        }

        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return array[position];
        }

        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return array.length;
        }

        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View row=null;
            LayoutInflater inflater=cntx.getLayoutInflater();
            row=inflater.inflate(R.layout.simplerow, null);
            
            DecimalFormat df = new DecimalFormat("#.##");
            final TextView title	=	(TextView)row.findViewById(R.id.title);
            TextView tvcol1	=	(TextView)row.findViewById(R.id.colortext01);
            TextView tvcol2	=	(TextView)row.findViewById(R.id.colortext02);
            
            TextView gt		=	(TextView)row.findViewById(R.id.text01);
            TextView nt		=	(TextView)row.findViewById(R.id.text02);
            
            tvcol1.setHeight(grossheight[position]);
            tvcol2.setHeight(netheight[position]);
            title.setText(datelabel[position]);
            
            gt.setText(df.format(grossSal[position]/1000)+" k");
            nt.setText(df.format(netSal[position]/1000)+" k");
            
            tvcol1.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, "Month/Year: "+title.getText().toString()+"\nGross Sal: "+grossSal[position], Toast.LENGTH_SHORT).show();
				}
			});
            
            tvcol2.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, "Month/Year: "+title.getText().toString()+"\nNet Sal: "+netSal[position], Toast.LENGTH_SHORT).show();
				}
			});
            
        return row;
        }
    }
	
	@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        updateSizeInfo();
    }
	private void updateSizeInfo() {
		
		/** This is onWindowFocusChanged method is used to allow the chart to
		 * update the chart according to the orientation.
		 * Here h is the integer value which can be updated with the orientation
		 */
		int h;
		if(getResources().getConfiguration().orientation == 1)
		{
			h = (int) (lay.getHeight()*0.5);
			if(h == 0)
			{
				h = 200;
			}
		}
		else
		{
			h = (int) (lay.getHeight()*0.3);
			if(h == 0)
			{
				h = 130;
			}
		}
		for(int i=0;i<netSal.length;i++) 
    	{
			netheight[i] = (int)((h*netSal[i])/highest);
    		grossheight[i] = (int)((h*grossSal[i])/highest);
    		System.out.println("net width[i] "+netheight[i]+"gross width[i] "+grossheight[i]);
    	}
    	listview.setAdapter(new bsAdapter(this,datelabel));
	}
	
}

Xml Layout for this activity

<?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="fill_parent"
  android:background="#fff"
  android:id="@+id/linearlay"
  >
  
    <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:padding="10dp"
         android:textSize="16sp"
         android:gravity="center"
         android:layout_gravity="center"
         android:textColor="#000"
    	 android:text="Bar Chart with out any jar"/>
  
    <RelativeLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:padding="15dp"
         android:background="#ddd"
         android:orientation="horizontal">
    
     		<TextView
     		    android:id="@+id/a1"
	            android:layout_width="10dp"
	            android:layout_height="10dp" 
	            android:layout_marginLeft="10dp" 
	            android:textSize="16sp"
	            android:gravity="center_horizontal"
	            android:layout_centerVertical="true"
	            android:layout_gravity="center"
	            android:background="#53933f"
          		android:text=""/>
	     
	     	<TextView
	     	    android:id="@+id/a2"
	     	    android:layout_toRightOf="@+id/a1"
	     	    android:layout_centerVertical="true"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content" 
	            android:layout_marginLeft="10dp"
	            android:textSize="16sp"
	            android:textColor="#000"
	            android:gravity="center_horizontal"
	            android:layout_gravity="center"
          		android:text="Gross sal"/>
	     
	     	<TextView
	     	    android:id="@+id/a3"
	     	    android:layout_toRightOf="@+id/a2"
	     	    android:layout_centerVertical="true"
	            android:layout_width="10dp"
	            android:layout_height="10dp" 
	            android:layout_marginLeft="10dp"
	            android:textSize="16sp"
	            android:gravity="center_horizontal"
	            android:layout_gravity="center"
	            android:background="#FF9A39"
          		android:text=""/>
	     
	    	 <TextView
	    	    android:id="@+id/a4"
	     	    android:layout_toRightOf="@+id/a3"
	     	    android:layout_centerVertical="true"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:layout_marginLeft="10dp" 
	            android:textSize="16sp"
	            android:textColor="#000"
	            android:gravity="center_horizontal"
	            android:layout_gravity="center"
          		android:text="Net sal"/>
     
     </RelativeLayout>
  
  <com.rakhi577.chart.HorizontalListView
  	android:id="@+id/listview"
  	android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	android:background="#ddd"
  />
  
</LinearLayout>


Xml Layout for List Item

<?xml version="1.0" encoding="utf-8" ?> 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dp">
	 
	  <RelativeLayout 
        android:id="@+id/lin02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_above="@+id/title"
        android:layout_gravity="center">
        
   		<TextView
   		    android:layout_alignParentLeft="true" 
   		    android:id="@+id/colortext01" 
            android:layout_width="30dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="bottom"
            android:layout_alignParentBottom="true"
            android:gravity="bottom" 
            android:padding="10dp" 
            android:textSize="16sp"
            android:background="#4CA613"/>   
            
   		<TextView
   		    android:layout_toRightOf="@+id/colortext01" 
   		    android:id="@+id/colortext02" 
            android:layout_width="30dp" 
            android:layout_height="wrap_content"
            android:gravity="bottom" 
            android:layout_gravity="bottom"
            android:layout_alignParentBottom="true"
            android:padding="10dp" 
            android:textSize="16sp"
            android:background="#FF9A39"/>
   		
   		<TextView android:id="@+id/text01" 
            android:layout_width="wrap_content" 
            android:layout_height="15dp"
            android:layout_above="@+id/colortext01"
            android:textColor="#000"
            android:textStyle="bold"
            android:textSize="13sp"/>
   		
   		<TextView android:id="@+id/text02" 
            android:layout_width="wrap_content" 
            android:layout_height="15dp"
            android:layout_above="@+id/colortext02"
            android:layout_toRightOf="@+id/colortext01"
            android:textColor="#000"
            android:textStyle="bold"
            android:textSize="13sp"/>
   		
  	</RelativeLayout>
  	<TextView 
	      android:id="@+id/title"
	      android:layout_width="60dp" 
	      android:layout_height="wrap_content"
	      android:textSize="14sp"
	      android:gravity="center_horizontal"
	      android:layout_alignParentBottom="true"
	      android:textColor="#000"/>
  </RelativeLayout>



Landscape Image

Portrait Image

source code

Love Detector in Android


package com.rakhi577.works;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Flames extends Activity
{
private EditText boy;
private EditText girl;
private Button test;
private Button clear;
private boolean flag;

private int i;
private int z=0;
private int x;
private int[] f={1,7,13,19,25,31};
int[] l={2,8,14,20,26,32};
int[] a={3,9,15,21,27,33};
int[] m={4,10,16,22,28,34};
int[] e={5,11,17,23,29,35};
int[] s={6,12,18,24,30,36};

private String Boyname;
private String Girlname;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
boy    =    (EditText)findViewById(R.id.txtBoy);
girl=    (EditText)findViewById(R.id.txtGirl);
test=    (Button)findViewById(R.id.buttonsubmit);
clear=    (Button)findViewById(R.id.buttonclear);

test.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

Boyname = boy.getText().toString().trim().replaceAll(" ", "");
Girlname= girl.getText().toString().trim().replaceAll(" ", "");
Validate();
//CheckFlames();
}
});

clear.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
boy.setText("");
girl.setText("");
}
});
}catch (Exception e) {
e.printStackTrace();
}

}
private String findDifference() {
String difference = "";
String temName = this.Girlname;
for (int i = 0; i < Boyname.length(); i++) {
String a = Boyname.charAt(i) + "";
if (Girlname.indexOf(a) != -1) {
Girlname = Girlname.replaceFirst(a, "");
}
else {
difference += a;
}
}
difference += Girlname;
this.Girlname = temName;
return difference;
}

public String getFLAMES() {
String flamesResult = "";
int difference = findDifference().length();
if (difference == 0) {
if (Boyname.equals(Girlname)) {
return "You have Given Same Names, So Can't find FLAMES";
}
else {
return "Can't find FLAMES for the Given Names";
}
}
String fResult[] = {
"FRIENDS", "LOVERS", "AFFAIR", "MARRIAGE", "ENEMIES",
"SISTERS/BROTHERS"};
int k = 0;
for (int i = 5; i > 0; i--) {
int j = 1;
while (j < difference) {
k++;
if (k == fResult.length) {
k = 0;
}
j++;
}
String fResult1[] = new String[i];
int m = 0;
for (int kk = 0; kk < fResult.length; kk++) {
if (kk != k) {
fResult1[m++] = fResult[kk];
}
}
fResult = fResult1;
if (k == fResult.length) {
k = 0;
}
flamesResult = fResult[0];
}
return flamesResult;
}

public void CheckFlames()
{
z=0;

char[] boyD = Boyname.toCharArray();
char[] girlD = Girlname.toCharArray();
int glen = girlD.length;
int blen = boyD.length;

for(x=0;x<blen;x++)
{
for(i=0;i<glen;i++)
{
if(boyD[x]==girlD[i])
{
z=z+1;
}
}
}

for(i=0;i<z;i++)
{
if(z==f[i])
{
DisplayToast("Friends");
}
if(z==l[i])
{
DisplayToast("Lovers");
}
if(z==a[i])
{
DisplayToast("Acquaintance");
}
if(z==m[i])
{
DisplayToast("Marriage");
}
if(z==e[i])
{
DisplayToast("Engaged");
}
if(z==s[i])
{
DisplayToast("Sweethearts");
}
}
}

public void DisplayToast(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
public void Validate()
{
if(Boyname.equals(""))
{
DisplayToast("** Enter Boys Name **");
}
else if(Girlname.equals(""))
{
DisplayToast("** Enter Girls Name **");
}

for(int i=0;i<Boyname.length();i++)
{
if((Boyname.codePointAt(i) >= 97 && Boyname.codePointAt(i) <=122) || (Boyname.codePointAt(i) >= 65 && Boyname.codePointAt(i) <=90))
{
flag = true;
}
else
{
flag = false;
}
}

for(int i=0;i<Girlname.length();i++)
{
if((Girlname.codePointAt(i) >= 97 && Girlname.codePointAt(i) <=122) || (Girlname.codePointAt(i) >= 65 && Girlname.codePointAt(i) <=90))
{
flag = true;
}
else
{
flag = false;
}
}

if(flag)
{
DisplayToast(getFLAMES());
}
else
{
DisplayToast("** Pls Enter Alphabets only **");
}
}
}

Xml Layout for this activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/main_layout"
android:orientation="vertical"
android:background="@drawable/img"
android:layout_gravity="top">

<TextView
android:gravity="center"
android:text="Flames"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="15dp"
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textStyle="bold"/>

<LinearLayout
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Boys Name"
android:background="@android:color/transparent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:width="100dp" android:textColor="#ffffff">
</TextView>

<EditText
android:id="@+id/txtBoy"
android:hint="Boys Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="140dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Girls Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:width="100dp" android:textColor="#ffffff">
</TextView>

<EditText
android:id="@+id/txtGirl"
android:hint="Girls Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="140dp">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/buttonsubmit"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:textColor="#000"
android:text="Test"
android:textStyle="bold"/>
<Button
android:id="@+id/buttonclear"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:textColor="#000"
android:text="Clear"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>

Landscape Image

Portrait Image

source code

Listview names search in Android

Note: Extend an activity by ListActivity
package com.rakhi577.listviewnamesearch;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

	private EditText et;
	private ListView lv;
	private String[] listview_names = {"Ram kiran","Ravi","Raju","Rani",
									   "Naren","prasad","krishna",
									   "Ramya","Rakesh"};
	
	private ArrayList<String> array_sort;
	int textlength=0;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et 	= 	(EditText) findViewById(R.id.EditText01);
		lv	=	(ListView) findViewById(android.R.id.list);
		
		array_sort=new ArrayList<String> (Arrays.asList(listview_names));
		setListAdapter(new bsAdapter(this));


		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();
				array_sort.clear();
				for (int i = 0; i < listview_names.length; i++)
				{
					if (textlength <= listview_names[i].length())
					{
						/***
						 * If you want to highlight the countries which start with 
						 * entered letters then choose this block. 
						 * And comment the below If condition Block
						 */
						/*if(et.getText().toString().equalsIgnoreCase(
								(String)
								listview_names[i].subSequence(0,
										textlength)))
						{
							array_sort.add(listview_names[i]);
							image_sort.add(listview_images[i]);
						}*/
						
						/***
						 * If you choose the below block then it will act like a 
						 * Like operator in the Mysql
						 */
						
						if(listview_names[i].toLowerCase().contains(
								et.getText().toString().toLowerCase().trim()))
						{
							array_sort.add(listview_names[i]);
						}
                      }
				}
				AppendList(array_sort);
				}
			});
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0,
			                    View arg1, int position, long arg3)
			{
			   Toast.makeText(getApplicationContext(), array_sort.get(position),
					   Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	public void AppendList(ArrayList<String> str)
	{
		setListAdapter(new bsAdapter(this));
	}
	
	public class bsAdapter extends BaseAdapter
    {
        Activity cntx;
        public bsAdapter(Activity context)
        {
            // TODO Auto-generated constructor stub
            this.cntx=context;

        }

        public int getCount()
        {
            // TODO Auto-generated method stub
            return array_sort.size();
        }

        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return array_sort.get(position);
        }

        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return array_sort.size();
        }

        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View row=null;
            
            LayoutInflater inflater=cntx.getLayoutInflater();
            row=inflater.inflate(R.layout.search_list_item, null);
            
            TextView   tv	=	(TextView)	row.findViewById(R.id.title);
            
            tv.setText(array_sort.get(position));
            
        return row;
        }
    }
}
 

Xml Layout for this activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical">
	
	<EditText 
	    android:id="@+id/EditText01"
		android:layout_height="wrap_content"
		android:layout_width="fill_parent"
		android:hint="search"/>

	<ListView 
	    android:id="@+id/android:list"
		android:cacheColorHint="#00000000"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
	
</LinearLayout>   

xml file for the list items

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

	<TextView
		android:id="@+id/title"
		android:textColor="#000"
	    android:layout_width="0dp"
	    android:layout_weight="1"
	    android:layout_margin="10dp"
	    android:layout_height="wrap_content"/>

</LinearLayout>

listviewnamesearch

You can find Source code here