list_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/grey" />
<item android:state_pressed="true"
android:drawable="@color/blue" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/blue" />
</selector>
That list_bg.xml will be called in the customized list xml as background
list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_margin="2dp" android:background="@color/list_bg">
<TextView android:id="@+id/post" android:gravity="center_vertical"
android:layout_width="wrap_content" android:layout_height="50dp"
android:textSize="20sp" android:textColor="#D0640D"
android:layout_toRightOf="@+id/bite_image" />
</RelativeLayout>
This one is the main xml for the Lister.java
main.xml
<?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">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:smoothScrollbar="true"
android:background="#fff" android:cacheColorHint="#fff"
android:fastScrollEnabled="false" android:clickable="true"
android:layout_marginBottom="36dp" />
</LinearLayout>
The Lister.java will be like this, the main thing is the list_bg.xml only
Lister.java
package com.list.viewer;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class Lister extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] captionArray = { "USA","India","England","Russia","Europe","Canada","Srilanka","Singapore","Thailand","Australia"};
ItemsAdapter ItemsAdapter = new ItemsAdapter(
Lister.this, R.layout.list,
captionArray);
setListAdapter(ItemsAdapter);
}
private class ItemsAdapter extends BaseAdapter {
String[] items;
public ItemsAdapter(Context context, int textViewResourceId,
String[] items) {
// super(context, textViewResourceId, items);
this.items = items;
}
// @Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list, null);
}
TextView post = (TextView) v
.findViewById(R.id.post);
post.setText(items[position]);
return v;
}
public int getCount() {
return items.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
}
}
The full source code
Have a good day.
Good stuff!
ReplyDeleteGet free hard factory reset code info, security setting code, mobile master reset info, android hard reset instruction, iPhone hard reset info, blackbery hard reset instruction and new mobile news and mobile reviews of all brands mobile phone from
Deletehttps://www.resetcode.org
Get free unlock code instructions of all kinds mobile phone from unlockcodesamsung.blogspot.com
Deletehttps://unlockcodesamsung.blogspot.com
Hi, if I change background of view in getView method, is there anyway to have background colors for both normal and highlighted states as shown in your screenshots(normal: white, highlight: blue)?
ReplyDeleteThanks!
Very Good Thanks!
ReplyDeleteWhat can i do if i want to create even click on any list. For example, after i click Europe, there is a Picture appear.
ReplyDeleteThanks before hand for your answers.
It's what I've found!
ReplyDeleteSimple, straight, good!
ReplyDeleteThanks! Best example I've found so far!
Great One... Thanks
ReplyDeleteThanks! best tutorial!
ReplyDeleteThanks and good
ReplyDeleteThanks man, this is great!
ReplyDeleteIamlinkfeeder
ReplyDeleteIamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder
Iamlinkfeeder