Set Background image, if the Custom Listview item is pressed - android

I am using custom list view using base adapter
<ListView
android:id="#+id/lstHome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:drawSelectorOnTop="false"
android:cacheColorHint="#3d4241"
android:clickable="true"
android:listSelector="#drawable/listbackground">
</ListView>
and listbackground.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/transparent" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="#drawable/list_background" android:state_pressed="true"/>
<item android:drawable="#drawable/list_background" android:state_pressed="false" android:state_selected="true"/>
</selector>
when i click on item the image background is just flashing but i want it should be activated
and show the background.
i used android:attr/activatedBackgroundIndicator in custom listview but it does not work for api below level 11.

Instead of listselector, you can try this:
Create bg_key.xml:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#color/pressed_color"/>
<item
android:drawable="#color/default_color" />
</selector>
Then include this background to your listview:
android:background="#drawable/bg_key"
And then, in your activity, create an onclick listner for your listview:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
}
}
The color code in bg_key, is up to you..

You will use this custom adapter in you listview
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public int[] i ;
public int start=0;
private LinearLayout layout;
private View view;
private View mLastView;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
start=0;
this.context = context;
i = new int[this.mlist.size()];
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
#Override
public Sample getItem(int position) {
return mlist.get(position);
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
view = inflater.inflate(R.layout.listitem, null);
layout = (LinearLayout)view.findViewById(R.id.linearlayoutSample);;
TextView text1 = (TextView) view.findViewById(R.id.item1);
TextView text2 = (TextView) view.findViewById(R.id.item2);
layout.setBackgroundColor(Color.BLACK);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(start==0)
{
mLastView = v;
start++;
}
if(start>0)
{
mLastView.setBackgroundColor(Color.BLACK);
mLastView = v;
}
v.setBackgroundColor(Color.GRAY);
}
});
return view;
}
}

you could try this with the previous version of your code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/transparent" android:state_pressed="false"
android:state_selected="false"/>
<item android:drawable="#drawable/list_background" android:state_pressed="true"/>
<item android:drawable="#drawable/list_background" android:state_selected="true"/>
<item android:drawable="#drawable/list_background" android:state_active="true"/>
</selector>
Then in the onClick of the view, get the relativeLayout or the LinearLayout inflating the row, and set its activated and also selected property to true, and that way i think you will get a background that remains.
Hope that helps.

Related

Android ListView selection background

Okay I'm going absolutely nuts with this. I have a ListView in mode singleChoice, when I click on it I set it to selected. And I set the background to a drawable with a selector (and I tried many states that could correspond to a selected item). However when I select it the background doesn't change and I dont get why. I visited dozens of forums for this but couldn't get an answer. Here's my code:
The ListView in my activity:
<com.gaetanl.aspa.ui.component.ExtensibleListView
android:id="#+id/payment_billing_address_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/item_selectable"
android:choiceMode="singleChoice"></com.gaetanl.aspa.ui.component.ExtensibleListView>
Its class:
public class ExtensibleListView extends ListView {
public ExtensibleListView(Context context) {
this(context, null);
}
public ExtensibleListView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.listViewStyle);
}
public ExtensibleListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(this, "item " + position + " selected = " + view.isSelected());
view.setSelected(true);
Log.d(this, "selected = " + view.isSelected());
}
});
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
The selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:drawable="#color/colorPrimary" />
<item
android:state_active="true"
android:drawable="#color/colorPrimary" />
<item
android:state_checked="true"
android:drawable="#color/colorPrimary" />
<item
android:state_selected="true"
android:drawable="#color/colorPrimary" />
<item
android:drawable="#android:color/transparent" />
</selector>
Method I:
In your list adapter,create getter and setter methods for position.Then in getView method, compare the position as in below:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item.xml, null);
holder = new ViewHolder(); holder.itemLayout(LinearLyout)convertView.findViewById(R.id.item_layout);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position==getPosition()){
holder.itemLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.back_ground_color));
}else{
holder.itemLayout.setBackgroundColor(ContextCompat.getColor(context,R.color.background_unselect));
}
return convertView;
}
public void setPosition(int posit) {
this.position = posit;
}
private int getPosition() {
return this.position;
}
Then in the activity , inside listView's onItemSelected() method: call the adapter's setPositionMethod:
list_view.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
mAdapter.setPosition(i);
mAdapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Just set the list selector to transparent ..that should do the job.
Method II:
Or may be ,You need a selector like below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#color/color_primary" >
</item>
<item
android:state_focused="true"
android:drawable="#color/color_primary" >
</item>
<item
android:state_selected="true"
android:drawablecolor="#color/color_primary" >
</item>
<item android:color="#color/#color/transparent" >
</item>
</selector>
then put list selector as #drawable/list_selector
But, if that doesn't work then I think you can set the background of item xml layout as
android:background="#drawable/list_selector"
Okay I found the answer. I was stupidly trying to set the ListView background (see code above). What I needed to do was, when creating adapter, to select a view for the items that incorporates variation for selected items. Android has one by default: simple_list_item_single_choice.
So here's the working code:
myListView = ((ListView) findViewById(R.id.listview_id));
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_single_choice, myList);
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
}
});
And then you have it, a list view with radio buttons on the right that check themselves when you click on em.
And here's the ListView in my layout.xml:
<ListView
android:id="#+id/listview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice">
</ListView>

ListView background color is not white

I am trying to create NavigationDrawer slide with ListView and the listview color is purple and not white.
This is my code:
activity_main:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/listDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:cacheColorHint="#color/nliveo_white"
android:choiceMode="singleChoice"
android:divider="#color/nliveo_transparent"
android:background="#color/nliveo_white"
android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>
MyAdapter:
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;
private static final int NOT_SELECTED = -1;
private int selectedPos = NOT_SELECTED;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
this.context = context;
this.navDrawerItems = navDrawerItems;
}
#Override
public int getCount() {
return navDrawerItems.size();
}
#Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public void setSelection(int position) {
if (selectedPos == position) {
selectedPos = NOT_SELECTED;
} else {
selectedPos = position;
}
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.rowIcon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.rowTitle);
imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
txtTitle.setText(navDrawerItems.get(position).getTitle());
if (position == selectedPos) {
convertView.setBackgroundResource(R.drawable.list_item_bg_pressed);
} else {
convertView.setBackgroundColor(R.drawable.list_item_bg_normal);
}
return convertView;
}
}
and the style:
list_item_bg_normal:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#color/nliveo_transparent" />
<item android:state_pressed="true"
android:drawable="#color/nliveo_transparent" />
<item android:state_selected="false"
android:drawable="#color/nliveo_white"/>
</selector>
list_item_bg_pressed:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#color/nliveo_transparent" />
<item android:state_pressed="true"
android:drawable="#color/nliveo_transparent" />
<item android:state_selected="false"
android:drawable="#color/nliveo_gray"/>
</selector>
color.xml
<color name="nliveo_white">#ffffff</color>
<color name="nliveo_gray">#e0e0e0</color>
<color name="nliveo_black">#000000</color>
<color name="nliveo_transparent">#00000000</color>
What am I doing wrong?The listview should be white and in selected state - gray.
I guess the problem is with the list_item_bg_pressed file.
For Listview To be White and Grey in Selected State
It Should Be Modified As :
bg_pressed
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#color/nliveo_gray" />
<item android:state_pressed="true"
android:drawable="#color/nliveo_gray" />
<item android:state_selected="false"
android:drawable="#color/nliveo_transparent"/>
</selector>
I found the problem.
I used the function setBackgroundColor instead of setBackgroundResource.
So the code should be :
if (position == selectedPos) {
convertView.setBackgroundResource(R.drawable.list_item_bg_pressed);
} else {
convertView.setBackgroundColor(R.drawable.list_item_bg_normal);
}

Android ListView does not gets selected when an OnClickListener is set

I am using Listview with custom Adapter and a selector which I have set as background of the listview row item parent layout. When I am not setting OnClick listener to the parent layout of the listview item, the selector is working fine and the row gets selected.
But as soon as I am setting OnClick listener to the parent layout of the listview item , the selector is not working. I don't want to use onItemClickListener as I have to take care of certain things from the ListView Adapter.
I have been searching for this for hours but didn't got any clue.
Please help.
Thanks
My Custom Adapter class
public class CustomAdapter extends BaseAdapter {
private final String[] mNotificationList;
private static LayoutInflater inflater = null;
private Activity mActivity;
public CustomAdapter(Activity activity,String[] data) {
mActivity = activity;
mNotificationList = data;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
if(mNotificationList == null){
return 0;
}
else{
return mNotificationList.length;
}
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int id) {
return id;
}
#Override
public View getView(final int position, View converterView, ViewGroup parent) {
if (converterView == null){
converterView = inflater.inflate(R.layout.notification_list_row, null);
}
NotificationListHolder listRowHolder = (NotificationListHolder)converterView.getTag();
if(listRowHolder==null) {
listRowHolder = new NotificationListHolder();
initializeView(listRowHolder,converterView);
}
bindView(listRowHolder,mNotificationList[position]);
return converterView;
}
private void bindView(NotificationListHolder listRowHolder, String notificationDetails) {
listRowHolder.articleName.setText(notificationDetails);
listRowHolder.parentLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mActivity, "TOAST",Toast.LENGTH_SHORT).show();
}
});
}
private void initializeView(NotificationListHolder listRowHolder,View converterView) {
listRowHolder.parentLayout = (LinearLayout) converterView.findViewById(R.id.notificationlist_row_layout);
listRowHolder.articleName = (TextView) converterView.findViewById(R.id.article_text);
converterView.setTag(listRowHolder);
}
public static class NotificationListHolder{
public LinearLayout parentLayout;
public TextView articleName;
}
}
list row item xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/notificationlist_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector" >
<TextView
android:id="#+id/article_text"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:textStyle="bold" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="40dp"
android:text="second view"
android:layout_below="#+id/article_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:textStyle="bold" />
</RelativeLayout>
list selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#color/msg_read_bg" />
<item
android:state_pressed="true"
android:drawable="#color/msg_read_bg" />
<item
android:state_activated="true"
android:drawable="#color/msg_read_bg" />
<item
android:state_active="true"
android:drawable="#color/msg_read_bg" />
<item
android:state_focused="true"
android:drawable="#color/msg_read_bg" />
<item android:drawable="#android:color/white" />
</selector>

Android GridView Selector Issue

I am using a StaggeredGridView in my application with a custom selector created by me but i must have gotten something terribly wrong with my selector because when i select an item in the gridview, all of the items change their color to green.
Here is the code for gridview item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/grid_item_selector">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ro.gebs.zonizbeacon.ui.custom.ScaledNetworkImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ro.gebs.zonizbeacon.ui.custom.AngledTextView
android:id="#+id/expired_tv"
android:layout_width="#dimen/size_favorite_angle_text"
android:layout_height="#dimen/size_favorite_angle_text"
app:angledText="#string/expired"
app:backgroundColor="#android:color/holo_orange_dark"
app:angledTextColor="#android:color/white"
app:angledTextSize="#dimen/font_size_plus" />
</RelativeLayout>
<ro.gebs.zonizbeacon.ui.custom.fonts.CustomFontTextView
android:id="#+id/store_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/default_padding"
android:textSize="#dimen/font_size_normal"
android:text="Zara"
app:fontName="Bold" />
<ro.gebs.zonizbeacon.ui.custom.fonts.CustomFontTextView
android:id="#+id/offer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontName="Regular"
android:paddingLeft="#dimen/default_padding"
android:paddingRight="#dimen/default_padding"
android:paddingBottom="#dimen/default_padding"
android:textSize="#dimen/font_size_small"
android:text="#string/info_about" />
</LinearLayout>
This is my selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#color/green_main_color" />
<item android:state_enabled="true" android:state_selected="true" android:drawable="#color/green_main_color" />
<item android:drawable="#android:color/white" />
</selector>
This is my adapter class:
public class FavoritesAdapter extends ArrayAdapter<Offer> {
LayoutInflater mLayoutInflater;
public FavoritesAdapter(Context context, List<Offer> objects) {
super(context, 0, objects);
mLayoutInflater = LayoutInflater.from(context);
}
/* #Override
public long getItemId(int position) {
return position;
}*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.item_favorite_offer, null);
holder = new ViewHolder();
holder.imageView = (ScaledNetworkImageView) convertView.findViewById(R.id.imageView1);
holder.offerText = (CustomFontTextView) convertView.findViewById(R.id.offer_text);
holder.storeName = (CustomFontTextView) convertView.findViewById(R.id.store_name);
holder.expiredTv = (AngledTextView) convertView.findViewById(R.id.expired_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//holder.offerText.setText("POSITION + "+position);
final Offer offer = getItem(position);
Date d = Calendar.getInstance().getTime();
if (offer.getActiveUntil() < d.getTime()) {
holder.expiredTv.setVisibility(View.VISIBLE);
} else {
holder.expiredTv.setVisibility(View.INVISIBLE);
}
ImageUtils.load(holder.imageView, offer.getPngLink(), R.drawable.image_placeholder, R.drawable.image_placeholder);
holder.offerText.setText(offer.getDescription());
holder.storeName.setText(offer.getStoreName());
return convertView;
}
static class ViewHolder {
AngledTextView expiredTv;
ScaledNetworkImageView imageView;
CustomFontTextView offerText;
CustomFontTextView storeName;
}
}
and in my fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_favorites, container, false);
gridView = (StaggeredGridView) view.findViewById(R.id.staggeredGridView1);
//gridView.setSelector(new ColorDrawable(getResources().getColor(R.color.green_main_color)));
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
emptyText = (CustomFontTextView) view.findViewById(R.id.empty_grid_view);
int margin = getResources().getDimensionPixelSize(R.dimen.default_padding);
int itemMargin = getResources().getDimensionPixelSize(R.dimen.padding_tell_button);
gridView.setItemMargin(itemMargin); // set the GridView margin
gridView.setPadding(margin, margin, margin, 0); // have the margin on the sides as well
mFavoritesAdapter = new FavoritesAdapter(getActivity(), new ArrayList<Offer>());
mFavoritesAdapter.setNotifyOnChange(false);
gridView.setAdapter(mFavoritesAdapter);
gridView.setOnItemClickListener(new StaggeredGridView.OnItemClickListener() {
#Override
public void onItemClick(StaggeredGridView parent, View view, int position, long id) {
Offer o = mFavoritesAdapter.getItem(position);
if (o != null) {
Intent intent = new Intent(getActivity(), WebViewActivity.class);
intent.putExtra(WebViewActivity.BUNDLE_HTML_LINK, o.getHtmlLink());
intent.putExtra(WebViewActivity.STORE_NAME, o.getStoreName());
startActivity(intent);
}
//getActivity().finish();
}
});
gridView.setOnItemLongClickListener(mOnItemLongClickListener);
//restartLoader(LOADER_FAVORITES);
return view;
}
Any ideas as to why it could be wrong?
Maybe your listitems get the state of focussed (while not intended). I always use a selector build like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_selected="true" android:drawable="#color/green_bit_transparent"/>
<item android:state_pressed="false" android:state_focused="true" android:state_selected="false" android:drawable="#color/green_bit_transparent" />
<item android:state_pressed="true" android:state_selected="false" android:drawable="#color/green" />
<item android:drawable="#android:color/transparent" />
</selector>

change color of selected listview item

I want to change color of list item when it will press
For that I did like below,
list_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
Selected
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#drawable/list_focused"/>
Pressed
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#drawable/list_selected" />
</selector>
I have set the color in colors.xml like below,
<drawable name="list_focused">#36C170</drawable>
<drawable name="list_selected">#9EC136</drawable>
and in my ListView I wrote like this,
<ListView
android:id="#+id/list_centers_complete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#drawable/list_item_selector" />
but when I am clicking on list item, the whole background color is changed instead of only list item.
How can I solve this?
Is there any way?
Thank you
Apply "#drawable/list_item_selector" to the row of that list(List item) not a List itself..
Something like, your list item (list row)..
<?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="horizontal"
android:background="#drawable/list_item_selector">
<TextView android:id="#+id/textForList"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:padding="10sp" />
.
.
.
</LinearLayout>
list_item_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#66FFFFFF" />
</shape>
</item>
<item>
<shape>
<solid android:color="#FF666666" />
</shape>
</item>
</selector>
You should be setting the selector to the row and not the listview itself.
<item android:state_activated="true">
<shape android:shape="rectangle">
<solid android:color="#333333" />
<padding android:left="5dp" android:right="5dp" />
</shape></item>
<item><shape android:shape="rectangle">
<solid android:color="#222222" />
</shape></item>
Try with a custom adapter this also helps you to have full control over your items and set a default item selected; listView XML and item XML have no special setup.
public class ListAdapter extends ArrayAdapter<MyObj> {
private final int layoutInflater;
private Context context;
private List<MyObj> items;
private int mSelectedItem = 0;
private int TAG_UNSELECTED = 0;
private int TAG_SELECTED = 1;
public ListAdapter(Context context, int resource, List<MyObj> items) {
super(context, resource, items);
this.context = context;
this.layoutInflater = resource;
this.items = items;
}
public void selectItem(int position) {
mSelectedItem = position;
notifyDataSetChanged();
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
return position == mSelectedItem ? TAG_SELECTED : TAG_UNSELECTED;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layoutInflater, null);
}
MyObj myObj = items.get(position);
TextView textView = (TextView) v.findViewById(R.id.title);
textView.setText(myObj.title);
int type = getItemViewType(position);
if(type == TAG_SELECTED) {
v.setBackgroundColor(Color.parseColor("#1da7ff"));
textView.setTextColor(Color.parseColor("#ffffff"));
} else {
v.setBackgroundColor(Color.parseColor("#f8f8f8"));
textView.setTextColor(Color.parseColor("#474747"));
}
return v;
}
}
Then in your activity:
ListView listView = (ListView) findViewById(R.id.list_view);
ListAdapter adapter = new ListAdapter(mContext, R.layout.item_layout, list);
listView.setAdapter(adapter);
adapter.selectItem(0); // Default selected item
// Get selected item and update its background
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
adapter.selectItem(position);
}
});
use this
android:background="#drawable/list_item_selector""

Categories

Resources