disabling and greying out list items - android

I have a list view. In that list view, I have to grey out and disable some items, and enable the rest list items with seperate color. How to do this?

You should write a custom adapter extending BaseAdapter for your ListView. To disable certain items, you have to override the "boolean isEnabled(int position)" in this adapter, and return false for every position you'd like to be disabled.
As for changing the background color for certain list elements: you could store the background color value in the data structure you're displaying. In your custom adapter's 'getView()' method, you should check this color value for the current element, and return a View with the correct background color set.
Or you could just call 'getChildAt()' on the ListView, getting back the View object for the desired element in the list, and change it's background color. I think I'd rather use the previous solution.
Remember to call 'notifyDataSetChanged()' on your ListView's adapter after you make changes like this.

Related

set text color for each custom adapter from a listView

I want to make a diable switch that can instantly gray out and disable a list of options from a listView, but I am not sure how to acchieve this with a CustomAdapter for each single row. With only textview it should be easy to set it with a onclick method, but can I also do that with a CustomAdapter?
Your question is kind of abstract because it doesn't have any code samples and no specific problem as well. So my answer will be abstract as well.
To achieve this set a listener on your switch. Once it's clicked, update the model that is used for displaying your adapter items.
If you need to update all items in the list, then you can add a boolean variable to the adapter class. Use this variable in your getView() method to decide whether the item should be grayed out or not. When the value is changed, call notifyDataSetChanged() on the adapter object. It will trigger redrawing all items in the list.
If you need to update only specific items, then add this boolean variable to the item model itself. Iterate over collection and set this flag where needed. All other logic is the same - use this variable in getView() and call notifyDataSetChanged().

How to scroll ListView to a particular position and change the text color

There is custom view and a ListView, both are coupled as such if one changes its state other must change. I am able to control custom view if one item of ListView is selected.
If a item is selected in custom view, corresponding item in List view should become visible and text in it must be hilighted.
I tried
listView.smoothScrollToPosition(clueIndex);
no scrolling happens on execution of this line.
Second question -
How can I get access to TextView in a particular adaptor postion and change its text color outside OnItemLongClickListener() ?
Issue resolved, due to error clueIndex was always -1.

Preselect items in listView Android

All i need is an idea or path to continue searching, i have a list of items that i add to a listview adapter. And i am trying to make some items in the list to have a specific background color (as an example) when the activity loads. Something like having those rows "preselected". The method public ListView getListView() does not help me much.
Thank you either way.
Need to set Multiple Choice Mode via setChoiceMode(), and then you can call setItemChecked() for each of the items you want checked. Then you probably have to make a custom adapter that overrides getView(), cast the parent to ListView and ask if the View (a list item) you are getting at the position given is in fact checked by calling something like getCheckedItems() on the parent. Then you can do what ever you want with that view, like set its background color etc.

How to disable a child item in a listview(make its color gray and unclickable)

in my application, there is a listview, and clicking each item of it leads to an operation.
but in some situation, some operation can not be done.
how to disable a child item in a listview (make its color gray and unclickable)?
You should override isEnabled in your adapter. Just keep track of the items you want to disable and return the correct value for them.

Listview item color

My list view is a multiple selection list view. i have to show the selected list view items in one color(say green) and the other items in some other color(red). How to achieve this?
It depends what you mean by showing "items in [...] color." The easiest way to change items' colors is to do it in the Adapter's getView() or bindView() method.
In the onItemSelectListner change the item background to whichever you need.
You could call the setBackgroundColor() method in the select event to change the background color.

Categories

Resources