Change list background problem - android

I have a ListView and on clicking a list item, it starts another activity.
So my problem is that, when I click on list item, it should change its background and move to another activity and on pressing back on this activity the list item should retain it changed background color. And again clicking on another list, it should remove the earlier background color and again do the same.
Thanks in advance.

You'll want the setItemChecked() method in the ListView. This should make the item you click "permanently on", if you place it in the onItemClick() method in a listener. Note that when you navigate to the next activity, you will have to use onSaveInstanceState() and onRestoreInstanceState() to store which item in the ListView has been clicked; I don't believe Android will retain which one has been clicked when you navigate away from the Activity with the ListView.
There is quite a good tutorial of this in the Fragments section of the Developers website, here.
Hope this helps.

Keep track of the selected item. Use a custom adapter and override the getView method. In this method check if the position of the view is the selected item and change the background of the view accordingly.

Override the item click listener and do the following to change the color of the clicked view:
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
{
v.setBackgroundColor(Color.BLACK);
}
});

I solve it by setItemChecked() methood and use android:choiceMode="singleChoice" inside ListView in my layout xml file. I used MyClass.this.getListView().setItemChecked(position,true); on clicking the Item in the list and the on getView() i am doing this...
if(position == MyClass.this.getListView().getCheckedItemPosition()){
holder._newsLinear.setBackgroundColor(Color.CYAN);
}
else{
holder._newsLinear.setBackgroundColor(Color.BLACK);
}

Related

custom listview save selected item position

I create a custom listview.
I want to select any item using another button click or something views click.
Already I tried via these code for custom selection.
And I also want to get previous selected item on resume.
listView.setItemChecked(2,true);
listView.setSelection(2);
listView.requestFocus();
But everytime , I failed.
I used this method to create custom listview.
Android ListView with Custom Adapter Example Tutorial
Advance Thanks .
For setItemChecked to work, your list items need to be Checkable; you can, for example use CheckedTextView (at the top level of the item view, no further layout around it).
For setSelection to work, you need to use it inside a Runnable, like so:
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(2);
}
});
In both cases, don't ask why.
For a selection to become visible, you can put a statelist in the background attribute of your list item.

how to set the background color of selected row in a custom list?

i am trying to change the background color of the selected row in a listview and i am able to do it.
But when i am clicking on another row then the background color of the previously selectedrow remains unchanged. I have the position of the previously selected row, Can anybody help me that how can change the background color of previously selected row as it was before??
If you keep track and update the listItems clicked state in your model you can just put the code for displaying has been clicked-color in the adapter and then call
adapter.notifyDatasetChanged();
I think this might be easier if you look at it another way.
Currently, your logic is "if I click on this row, change its color to a special color and change the old row's color back to the original color". However, this doesn't seem to be the logic you actually want. Rather, you want the last clicked (aka selected) row to be a different color.
You haven't posted any code, so I don't know if you are implementing your own ListAdapter in this project. That is the approach I would take. Create a class which extends ListAdapter, and create an additional private variable that stores the position of the last selected row. Then in the overriden getView() method, do a quick check
if(rowPosition == lastSelectedRowPosition)
viewToReturn.setBackgroundColor();
If you aren't sure how to make your own list adapter, check out the tutorial at http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx.
In your activity use setOnItemClickListener of Your_List Object.
See Demo Code:
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
v.setBackgroundColor(Color.BLUE); // <--- Use color you like here
// ^ this v gives current row.
}
});
This will make background color changed for that row forever.

Item not clicked on a GridView

I have a GridView whose items are ToggleButtons, and I defined an event listener for clicks on items, but nothing happens when I click on them. Here's the listener:
paintActionsGrid.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View view,int position,
long id)
{
((ToggleButton)view).setChecked(true);
paintBoardView.setPaintAction(paintActions[position]);
}
});
I put a break point in the listener, but it's not reached, which means the listener is not called at all!
Can somebody please tell me what's wrong? Is it related to the fact that the items are ToggleButtons? I can attach the adapter code if needed.
Thanks.
The click event is consumed by ToggleButton hence not passed to the GridView. Similar to this post.
Yeah the problem is ToggleButton by default has its clickable enabled and hence your ItemClickListener won't work. The idea is to set the ToggleButton's focus to false by default.
Assuming that you have a custom xml for your ToggleButton, add this line to it,
android:focusable="false"
Since you have a focused element, this problem occurs. Try this simple idea.

Android - OnItemClickListener listview

Here's what I want to do:
I have this list of tasks. When I click on one of the items I want this to appear:
Assuming that menubar can be found with
findViewById(R.id.menubar);
Can someone please tell what code I have to put onItemClickListener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NOTE: The menubar visibility its set as GONE.
Thanks!
Without looking at the code, a precise answer is not possible. However, when I first wanted to implement a "Quick Action Bar" similar to the one you have shown, I had followed this example here:
http://code.google.com/p/simple-quickactions/
And this with some styling put in: https://github.com/lorensiuswlt/NewQuickAction3D
Hope this helps....
It is a bit difficult without seeing any code, but I can give you some logic ideas.
Based on your description, I'm assuming that those quick bar controls are hidden on EACH listview item so, it goes:
Fotos (visible)
Action Controls (hidden)
[End of Item]
PROJECTOS (visible)
Action Controls (hidden)
...
I think the OnItemClick method gives you the view (item: Fotos, Projectos, etc) that was clicked as the 2nd argument. If you cast that back to whatever layout you used to create the listview items, you should be able to use findViewByID to get access to the hidden controls that are on each listview and make them visible.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Assuming each item is a linear layout
LinearLayout itemAsLL = (LinearLayout)view;
//find the action bar controls
LinearLayout actionControlsLayout = itemAsLL.findViewById(R.name.of.actioncontrols.id);
//Make it visible
actionControlsLayout.setVisibility(VIEW.Visible);
}
You would need to find a way to hide those controls when another item is clicked? Maybe save the view (or position in the adapterView ) as a class variable and when another item is clicked, go to that view/find that view and hide the controls.
I finally found out the answer. Apparently, when you fill a view with lots of tasks, the adapter does not fill everything when you load the layout, so everytime you scroll down, hes putting more items on the listview.
What you have to do is make sure you put a field "loaded" and, in case is false, it does not show the menubar below that item, otherwise, when you click in one of the items of that list, when you scroll down, since they will have the same relative position (if you click on the first item, the position = 0, when you scroll down, there will be another position = 0) the menubar will appear on both items.
Hope it helps ;)

Android - setSelected in OnItemClick in ListView

I am trying to set item selected in OnItemClick event in ListView and it just wouldn't leave item selected. What am I doing wrong?
lView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(#SuppressWarnings("rawtypes") AdapterView parent, View clickedview, int position, long id)
{
clickedview.setSelected(true);
mItemsAdapter.select(position);
}
});
few things:
1. I am trying to implement Multiple Select on the list View.
2. I cannot extend from ListActivity because Activity extends from BaseActivity custom class already.
3. mItemsAdapter is a custom ItemsAdapter adapter that extends BaseAdapter.
4. I don't need a checkbox in there, just to be able to see the row selected is fine.
5. ItemsAdapter overrides getView() and sets the layout of the row by inflating xml
I could manage to get an item of a ListView to be set as selected when long-clicked:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
parent.requestFocusFromTouch(); // IMPORTANT!
parent.setSelection(position);
return true;
}
It only worked after I called requestFocusFromTouch().
I currently don't have much time. So I'll take a look again later this day.
Anyway take a look at my previous questions, I was struggling with the same:
Change ListView background - strange behaviour
Clickable ListView
In case the solution for you is not in there (I think it's in the first one) we will need more code, in order to help you.
Hope this helps a bit.
I have not had much luck using the built in functionality for selection.
I see you have your own custom adapter, which I am guessing means your inflating custom views as rows. If your row has anything more then a checkedtextview I don't think you will be able to correctly use setSelections.
I solved this problem by using my own models and functions. Each item in the list had data with it to determine it if was selected or not. I could then iterate though that array, toggle selections with and and even update the UI by changing values and calling notifydatasetchanged on the adapter (which used getView and checked against my selection model to draw checks).

Categories

Resources