This program is for highlighting a particular item in a listview when the item is selected.
First make "List_HighLight_Activity.java" :-
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class List_HighLight_Activity extends Activity {
ListView lv;
TextView tv;
IndexListViewAdapter indexlistviewAdapter;
static int counter=0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.TextView_name);
lv = (ListView)findViewById(R.id.list);
String Info[]= new String[] {"Arshad","Parwez","Arshu","Cool","Dude","Smart",
"Attitude","Respect","Love","Peace","Android","Application","Developer"};
showList(Info);
lv.setAdapter(indexlistviewAdapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
counter=position;
}
});
}
void showList(String[] values)
{
indexlistviewAdapter = new IndexListViewAdapter(this, values)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
return row;
}
};
}
}
Then, make a file named "IndexListViewAdapter.java" :-
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class IndexListViewAdapter extends BaseAdapter
{
Activity context;
String index[];
public IndexListViewAdapter(Activity context, String[] index) {
super();
this.context = context;
this.index = index;
}
public int getCount() {
// TODO Auto-generated method stub
return index.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewindex;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.index_list_row, null);
holder = new ViewHolder();
holder.txtViewindex = (TextView) convertView.findViewById(R.id.TextView_name);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewindex.setText(index[position]);
if (position==List_HighLight_Activity.counter) {
convertView.setBackgroundColor(Color.rgb(99,184,255));
} else {
convertView.setBackgroundColor(Color.WHITE);
}
return convertView;
}
}
Then, make a file named "list_bg.xml" in "/res/drawable" folder :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false"
android:drawable="@color/white" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/blue" />
</selector>
Then, make a file named "main.xml" in "/res/layout" folder :-
<?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="@color/white" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#cdc9c9"
android:dividerHeight="2sp"
android:cacheColorHint="#fff"
android:choiceMode="singleChoice" />
</LinearLayout>
Then, make a file named "index_list_row.xml" in "/res/layout" folder (This is the view which I'm inflating in the list view. Here I'm just passing a textview. You can use your own view which you want to show in the listview.) :-
<?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="40sp"
android:background="@drawable/list_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView_name"
android:layout_width="fill_parent"
android:layout_height="30sp"
android:layout_marginLeft="5sp"
android:textStyle="bold"
android:textSize="16sp"
android:gravity="center_vertical"
android:textColor="@android:color/black"
android:singleLine="true" />
</LinearLayout>
Then, make a file named "colors.xml" in "/res/values" folder (Here i'm using blue color for the item which is selected, rest all the items in the listview are all coloured white) :-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#f00f</color>
<color name="white">#ffff</color>
</resources>
You are done now. Please run the code and let me know if you have any problem.
You can download the full project source code from HERE
First make "List_HighLight_Activity.java" :-
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class List_HighLight_Activity extends Activity {
ListView lv;
TextView tv;
IndexListViewAdapter indexlistviewAdapter;
static int counter=0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.TextView_name);
lv = (ListView)findViewById(R.id.list);
String Info[]= new String[] {"Arshad","Parwez","Arshu","Cool","Dude","Smart",
"Attitude","Respect","Love","Peace","Android","Application","Developer"};
showList(Info);
lv.setAdapter(indexlistviewAdapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
counter=position;
}
});
}
void showList(String[] values)
{
indexlistviewAdapter = new IndexListViewAdapter(this, values)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
return row;
}
};
}
}
Then, make a file named "IndexListViewAdapter.java" :-
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class IndexListViewAdapter extends BaseAdapter
{
Activity context;
String index[];
public IndexListViewAdapter(Activity context, String[] index) {
super();
this.context = context;
this.index = index;
}
public int getCount() {
// TODO Auto-generated method stub
return index.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewindex;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.index_list_row, null);
holder = new ViewHolder();
holder.txtViewindex = (TextView) convertView.findViewById(R.id.TextView_name);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewindex.setText(index[position]);
if (position==List_HighLight_Activity.counter) {
convertView.setBackgroundColor(Color.rgb(99,184,255));
} else {
convertView.setBackgroundColor(Color.WHITE);
}
return convertView;
}
}
Then, make a file named "list_bg.xml" in "/res/drawable" folder :-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false"
android:drawable="@color/white" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/blue" />
</selector>
Then, make a file named "main.xml" in "/res/layout" folder :-
<?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="@color/white" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#cdc9c9"
android:dividerHeight="2sp"
android:cacheColorHint="#fff"
android:choiceMode="singleChoice" />
</LinearLayout>
Then, make a file named "index_list_row.xml" in "/res/layout" folder (This is the view which I'm inflating in the list view. Here I'm just passing a textview. You can use your own view which you want to show in the listview.) :-
<?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="40sp"
android:background="@drawable/list_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView_name"
android:layout_width="fill_parent"
android:layout_height="30sp"
android:layout_marginLeft="5sp"
android:textStyle="bold"
android:textSize="16sp"
android:gravity="center_vertical"
android:textColor="@android:color/black"
android:singleLine="true" />
</LinearLayout>
Then, make a file named "colors.xml" in "/res/values" folder (Here i'm using blue color for the item which is selected, rest all the items in the listview are all coloured white) :-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#f00f</color>
<color name="white">#ffff</color>
</resources>
You are done now. Please run the code and let me know if you have any problem.
You can download the full project source code from HERE