Android GridView Selector Issue - android

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>

Related

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);
}

Frame layout foreground property is not working in grid view

iam developing an android custom gallery where am using grid view and a framelayout as gridview item(for foreground property for selector). everthing works fine , but it's not drawing it's selected state after scrolling ..
here is my get view method
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final GalleryHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_child_gallery_item, parent, false);
holder = new GalleryHolder();
holder.folderThumbnail = (RecyclingImageView) convertView.findViewById(R.id.iv_thumbnail);
holder.baseLayout = (GalleryFrameLayout) convertView.findViewById(R.id.baseItemLayout);
convertView.setTag(holder);
} else
holder = (GalleryHolder) convertView.getTag();
if(mSelections.contains(String.valueOf(position))){
holder.baseLayout.setSelected(true);
}
else{
holder.baseLayout.setSelected(false)
}
if(getGallerytype().equals(GallerySwitcherFragment.IMAGES))
loadBitmap(GallerySwitcherFragment.IMAGES,
Long.valueOf(mFiles.get(position).getThumbnailData()), holder.folderThumbnail);
else
loadBitmap(GallerySwitcherFragment.VIDEOS,
Long.valueOf(mFiles.get(position).getThumbnailData()),holder.folderThumbnail);
holder.baseLayout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
setMultiSelectionEnabled(true);
holder.baseLayout.setChecked(true);
mController.toggleActionBarItems(true);
return true;
}
});
holder.baseLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isMultiSelectionEnabled){
if(mSelections.contains(String.valueOf(position)))
mSelections.remove(String.valueOf(position));
else
mSelections.add(String.valueOf(position));
}
else
return;
}
});
return convertView;
}
and here is my xml
<?xml version="1.0" encoding="utf-8"?>
<com.sams.customcomponents.GalleryFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="119dp"
android:descendantFocusability="blocksDescendants"
android:id="#+id/baseItemLayout"
android:addStatesFromChildren="true"
android:foreground="#drawable/gridview_selector" >
<com.sams.customcomponents.RecyclingImageView android:id="#+id/iv_thumbnail"
android:contentDescription="#string/iv_content_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#color/transparent_black" />
</com.sams.customcomponents.GalleryFrameLayout>
here is my selector
<?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/selector_blue"/> <!-- pressed state -->
<item android:state_focused="true"
android:drawable="#color/selector_blue"/> <!-- focused state -->
<item android:state_selected="true"
android:drawable="#color/selector_blue"/> <!-- selected state -->
<item android:drawable="#android:color/transparent"/> <!-- default state -->
</selector>

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>

Set Background image, if the Custom Listview item is pressed

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.

Android ListView list:selecter is not woking

I am new to android and my an using list view, I want to change list item color on selection but it is not working.
I have check almost all links on stack overflow and tried them all but no luck... Please check my code ... and tell me where is the problem.
public class Main extends Activity {
private ListView listView;
private String[] menuArray;
private List<String> menuList = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menuArray = getResources().getStringArray(R.array.Menu);
menuList = Arrays.asList(menuArray);
listView = (ListView) findViewById(R.id.listView_main_menu);
listView.setAdapter(new EfficientAdapter(this,menuList));
}
/******************************** EfficientAdapter ************************************/
public class EfficientAdapter extends BaseAdapter{
private LayoutInflater mInflater;
#SuppressWarnings("unused")
private String TAG=EfficientAdapter.class.getSimpleName();
#SuppressWarnings("unused")
private Context context;
private List<String> data= new ArrayList<String>();
public EfficientAdapter(Context context,List<String> data) {
super();
mInflater = LayoutInflater.from(context);
this.context=context;
this.data.addAll(data);
}
public EfficientAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
this.context=context;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.main_list, null);
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.textView_mainlist_item);
convertView.setTag(holder);
}
holder =(ViewHolder) convertView.getTag();
holder.textLine.setText(getItem(position));
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public String getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView textLine;
}
}
}
Here is my xml file with list view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Gainsboro"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="#+id/listView_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#color/DarkGray"
android:dividerHeight="1dip"
android:fadingEdge="none"
android:listSelector="#drawable/row_color"
android:scrollbars="none" >
</ListView>
</RelativeLayout>
row_color.xml
<item android:state_pressed="false" android:state_focused="false"
android:drawable="#color/Green" />
<item android:state_focused="true"
android:drawable="#color/Blue" />
<item android:state_pressed="true"
android:drawable="#color/Brown" />
<item
android:state_selected="true" android:drawable="#color/Cyan" />
main_list.xml; this xml contain textview which is inflating
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView_mainlist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="TextView"
android:textColor="#color/DarkSlateGray"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|center"
android:paddingRight="30dp"
android:paddingTop="6dp"
android:text=">"
android:textColor="#color/DarkSlateGray"
android:textSize="28sp"
android:textStyle="bold" />
</FrameLayout>
Just found that only this portion is working fine
<item android:state_pressed="false" android:state_focused="false"
android:drawable="#color/Green" />
Instead of
convertView = mInflater.inflate(R.layout.main_list, null);
Try
convertView = mInflater.inflate(R.layout.main_list, parent, false);
EDIT:
I see what you are trying to do now. You don't want to have android:ListSelector on ListView, but rather on each individual FrameLayout. So:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:listSelector="#drawable/row_color" >
You would need to apply it to each individual view in order to have them change color when pressed, etc.
Visit http://android-codes-examples.blogspot.in/2011/03/customized-listview-items-selection.html
Hope it works for you

Categories

Resources