Highlight selected items in ListFragment (MultiChoiceModeListener)? - java

I want to be able to see what items that have been selected in my list. Before turning ListView into ListFragment, the selected items had a blue background color (Holo Dark), but now I can only see the blue color while pressing the items - then it disappears. I do not have a problem with the items getting "checked", because they are. You just can't see it.
Here's what I do in onActivityCreated():
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setItemsCanFocus(false);
getListView().setMultiChoiceModeListener(new LongPress());
LongPress() is a private class implementing ListView.MultiChoiceModeListener. It opens up a contextual action bar when holding finger on an item. The user can then select more items, and this is where the problem occurs. You can not see which items are checked or not (I want them to be highlighted). Why did it work before extending ListFragment? I tried to create a custom selector in my .xml-files, that didn't seem to work either. I would prefer using the custom one, if there's any.
Here's another method in my ListFragment class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_listview, container, false);
}
fragment_listview.xml isn't doing anything special apart from changing the background color and the color of the divider.
What should I do?
I tried this, but it didn't work:
In my list_item.xml:
android:background="#drawable/selector"
selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/item_checked" />
</selector>
item_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http//schemas.android.com/apk/res/android" >
<solid android:color="#000000" />
</shape>

I was having the exact same issue.
I found a solution in this post: Highlight custom listview item when long click
The trick:
Set in the layout file of your list's row (in the top level component, usually a LinearLayout or RelativeLayout):
android:background="?android:attr/activatedBackgroundIndicator"

I cannot find the details right now, but you have to provide a statelist drawable for the listview-items that provides a separate drawable for the checked state.

Related

How to color selected item of ListView on android at onListItemClick method

I know that familiar question exist my is a little different.
I'm implementing ListView on android.
This method should color item when it clicked.
#Override
protected void onListItemClick(ListView l, View v, int position, longid) {
super.onListItemClick(l, v, position, id);
Language lng = lng.get(position);
l.getChildAt(position).setBackgroundColor(Color.GREEN);
}
My problem is that when i click on item some other items are get colored to.
why is that happens?
why is that happens?
Its happen because of ListView's recycling mechanism.
And is there a difference between l.getChildAt(position) to view v
itself ?
No.
To solve your problem, i suggest you to use a Selector to change rows color depending on state.
eg:
1) Create a selector xml file: listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_focused="true"
android:drawable="#drawable/listview_selector_focused" />
<item
android:state_pressed="true"
android:drawable="#drawable/listview_selector_pressed" />
</selector>
2) Add the selector to your ListView:
<ListView
...
android:listSelector="#drawable/listview_selector"
/>

Functions for Buttons

I'm trying to figure out how to do this thing for an android app.
So I made an array of buttons
Button btn[][] = new Button[10][10];
How do I make it so that once I click a button that it, for instance, turns a different color?
I have had trouble making it because I can create the array, and it looks nice, but how do I assign different functions for individual buttons? Are buttons in the array already labeled and can I use individual ones? Thanks.
Any time you would like to make a view change a different color based on the user actions, you should use a state list drawable
Here is a very simple example of a state list drawable which you would use to trigger only off whether the user had pressed the view or not.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:color="#color/brown" />
<item android:state_pressed="true" android:color="#color/brown_selected" />
</selector>
You would then set this on the view with the following attribute to the view in xml
android:background="#drawable/background"
This though, will only be changed while the user is pressing the button. If you would like it to permenantly change color, use a on click listener. For example if you would like to change the background color to white:
button.setOnClickListener(new View.OnClickListener() {
/**
* Handle a user clicking on the view v
* #param v the view the user clicked on. In this case the button
*/
#Override public void onClick(View v) {
// Set the background color to white
v.setBackgroundColor(Color.WHITE);
}
});

Change ListView item background to gradient

I am able to setup a listview using custom adapter which shows correct output..Now i want to change the background of each listview items to a gradient.. how it could be done?
My files...
activity_main.xml....Main xml file
MainActivity.java....Main activity
Item.java.... item selector method
ItemAdaptor.... custom adapter
list_item.xml..... item (textView) styling
I have created my gradient code which is like..
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFAFAFA"
android:centerColor="#FFFFFFFF"
android:endColor="#FFFAFAFA"
android:angle="90"/>
</shape>
also how could I change the separator color,and the background color of the whole listview..
Have you mentioned android:background="#drawable/customshape" in your rowLayout.xml so that it knows which shape is to be used? Also use android:divider="#FFCC00" in your listview tag to change the seperator color. Add <solid android:color="#F0F0F0" /> to your shape so as to change the background color.
Have a look on this tutorial
author created list-view with gradient effect, I think this is exactly what you need.

List Selector for multicolor list in Android

I am having a list in my android application. The list items are custom objects and depending on the custom object property, list item color will be decided.
Now the problem is, when I select any item for such list, List selector is not displayed.
How can I fix this issue? To set list item color, I am using following method in adapter.
convertView.setBackgroundColor(Color.LTGRAY)
Is this the right way to set color? if not what else I can use.
Thanks in advance.
Swapnil Dalal.
You can fix this by two ways:
1.Write a selector for the itemview which set the background to the transparent in the pressed state, then set the selector as the backgound of the itemview.
<item android:state_enabled="true"
android:state_pressed="false"
android:drawable="#color/gray" />
<item android:state_enabled="true"
android:state_pressed="true"
android:drawable="#color/transparent" />
2.Remove the listselector, just write a selector for the itemview with the color you wanted by the different state and set it as the background of the itemview.
Please add your getView method code so that we can help you better. One common mistake we do is that we don't always create new view for each item in the list view for loading different layout list Items.
For example common approach for identical itemed list:
if(view == null)
{
vi = inflater.inflate(R.layout.fb_list_row, parent, false);
}
While for different object listview items you need to remove if statement like this;
vi = inflater.inflate(R.layout.fb_list_row, parent, false);
and then do the changing for each list item..
Hope this will help. Else put some more code.
I got solution for this question,
I created to different xml files for different objects in the application.
So in the getView of adapter, depending on condition we can load either one of the xml.
Example:
`if(true) {
convertView.setBackgroundResource(R.drawable.firstxml);
}
else {
convertView.setBackgroundResource(R.drawable.secondxml);
}`
In these xmls, we can specify colors as required.
Example:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/transperent"/>
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/listview_selector" />
</selector>
Thanks,
Swapnil Dalal.

Android: Custom Gallery setSelection() issues

I have an extended BaseAdapter that has LinearLayout children (an ImageView and a TextView in each) hooked up to a custom Gallery.
When I first launch my Activity, I want to call setSelection(position) to cause the ImageView to change its selector to the "selected" image. This works once I fling the Gallery, on subsequent selected children, but not the very first time the app is launched.
My selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/home_image_select" />
<item android:state_selected="false"
android:drawable="#drawable/home_image" />
</selector>
My first guess was to call notifyDataSetChanged() on the adapter after calling setSelection(), which I attempted to do like this:
((CustomAdapter) gallery.getAdapter()).notifyDataSetChanged();
That didn't do anything. I also tried overriding the setSelection() of the Gallery class to do this:
View v = this.getAdapter().getView(position, null, this);
((ImageView) v.findViewById(R.id.gallery_image)).setSelected(true);
That doesn't work either. Anything I'm missing or could try?
I found the solution to my own problem by overwriting the setSelection() of Gallery (it worked after all).
#Override
public void setSelection(int position) {
super.setSelection(position);
View v = this.getAdapter().getView(position, null, this);
v.setFocusable(true);
v.requestFocus();
}
I think you should not call notifyDataSetChanged(), selection state will be cleared when underlying dataset changed.
Just call setSelection(position), it works in my app.

Categories

Resources