Selector for Imageview in ListView - android

I have a problem with ImageView in my ListView.
I want to change its drawable according the specified selector.
My ListView item is a simple LinearLayout, containing a TextView and an ImageView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/rightMenuItemName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|right"
style="#style/right_menu_item_name"
/>
<ImageView
android:id="#+id/rightMenuItemIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:clickable="true"
android:src="#drawable/selector_menu_item"/>
</LinearLayout>
The selector_menu_item drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/right_menu_s" />
<item android:state_focused="true" android:drawable="#drawable/right_menu_s" />
<item android:state_selected="true" android:drawable="#drawable/right_menu_s" />
<item android:drawable="#drawable/right_menu" />
</selector>
The ListView itself has his own selector for background coloring android:listSelector="...". The TextView has also a color-selector for the foreground color. This both selectors works like expected, only the image of the ImageView will not change.
My Adapter:
public class RightMenuAdapter extends BaseAdapter {
…
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if (convertView == null) {
convertView = newView(type, parent);
}
bindView(position, type, convertView);
return convertView;
}
#Override
public View newView(int type, ViewGroup parent) {
View v = inflater.inflate(R.layout.list_screens_right_menu, parent,
false);
v.setTag(new ViewHolder(v));
return v;
}
#Override
public void bindView(int position, int type, View view) {
ScreenNavigationData item = items.get(position);
ViewHolder vh = (ViewHolder) view.getTag();
vh.name.setText(item.getMenuTitle());
}
}
Any suggestions?

change android:src="#
drawable/selector_menu_
item"/> to android:background="#
drawable/selector_menu_
item"/>

If you are overriding getView() of your list adapter surely this will work. Kindly note down, from your xml you are using same image only for all state.

Damn, I found the solution:
I have to remove the android:clickable="true" attribute from the ImageView. Now everything works ...
Thank you anyway for your help!

This is only partial fix for the problem.
If you need to have click event added to ImageView, then even if you set it in XML, it won't work.
What you need to do is set the android:duplicateParentState="true" parameter in ImageView XML, so it get working and you still can have the ImageView clickable.
My ImageView XML looks like so:
<ImageView
android:id="#+id/ivFavIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/btn_history_fav"
android:duplicateParentState="true"
android:visibility="visible"
/>
and 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="#drawable/ic_hist_add_fav_highlighted"/>
<item android:state_pressed="false" android:drawable="#drawable/ic_hist_add_fav_normal"/>
</selector>
I spent few hours of checking different paths, hope it helps someone...

Related

How to define color for selected TextViews once in Android?

I have a ListView and created a xml file for ListView items.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_item_selector_back"
android:minHeight="?attr/listPreferredItemHeight"
android:orientation="vertical">
<CheckedTextView
android:id="#+id/list_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:textColor="#color/list_item_selector_fore" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/list_item_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/list_item_selector_fore" />
<!-- and many more TextView elements -->
</LinearLayout>
</LinearLayout>
list_item_selector_fore.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_pressed="true" />
<item android:color="#android:color/white" android:state_activated="true" />
<item android:color="#android:color/white" android:state_focused="true" />
<item android:color="#android:color/black" />
</selector>
As you can see, I defined the background once in the root LinearLayout. But I had to set android:textColor="#color/list_item_selector_fore" for every TextView.
I tried it with foreground, but nothing happened. How can I get this smarter?
HOW TO LOOK:
two list view items
If you want do it for all TextView, the best way should be to subclass TextView
public class CustomeTextView extends TextView{
public CustomeTextView(Context context) {
super(context);
this.setTextColor(R.color.white);
}
}
If I understand the question correctly you want to change the colour of the text inside a TextView. From your XML it seems you want it to be onClick therefore you can do this:
TextView textView = (TextView) findViewById(R.id.myTextView);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setTextColor('set text color here');
}
});
EDIT:
If you have these items inside of a ListView I would suggest that the easiest way for you to achieve what you want is to set an OnItemClickListenerand then change the textColor based on the OnItemClick event, which may look something like this;
ListView listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view.findViewById(R.id.rowItemTextView);
textView.setTextColor('color goes here');
}
});
Hopefully this short snippet will guide you in the right direction to go.

Keyboard navigation with Android GridView doesn't scroll grid

I'm trying to use a keyboard to navigate through a GridView of items.
In the short demo, which only contains a GridView (containing item views with android:focusable="true"), you can see that none of the items gain focus - the grid is the thing that scrolls (the items don't turn orange).
Adding the android:descendantFocusability="afterDescendants" allows each item to be focused first, but still, doesn't continue to scroll the GridView so you can navigate down:
Clicking on an item (with mouse or by pressing Return if the item was focused, after the descendent focusability fix) turns an item blue - indicating it's in pressed state.
#layout/activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2" />
#layout/dummy_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dummy_item_parent"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:focusable="true"
android:clickable="true"
android:background="#drawable/item_background" />
#drawable/item_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/holo_blue_light" />
<item android:state_focused="true" android:drawable="#android:color/holo_orange_light" />
<item android:drawable="#android:color/holo_red_light" />
</selector>
MyActivity.java (with ListAdapter):
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
AbsListView listview = (AbsListView) findViewById(R.id.listview);
listview.setAdapter(new DummyAdapter(getLayoutInflater()));
}
private static class DummyAdapter extends BaseAdapter {
private static final int COUNT = 25;
private final LayoutInflater layoutInflater;
DummyAdapter(LayoutInflater layoutInflater) {
this.layoutInflater = layoutInflater;
}
#Override
public int getCount() {
return COUNT;
}
#Override
public String getItem(int position) {
return "Item " + position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = layoutInflater.inflate(R.layout.dummy_item, parent, false);
}
((TextView) view).setText(getItem(position));
return view;
}
}
}
Swapping the GridView for a ListView, without changing the adapter, or the item views/item view attributes, will behave as expected - items can be focused and the ListView will scroll to the bottom, so that all items can be focused using just keyboard input. Also, using a RecyclerView with LinearLayoutManager / GridLayoutManager will also work correctly.
I have tried this on API 16 and 22 with the same results. I have tried settings the android:descendantFocusability attribute on the GridView to afterDescendents with the same result.
I tried your code and it doesn't work with GridView, as you said, but i have some issue also with ListView. The ListView scrolls correctly, but only the first item of every page is highlighted.
With these little modifications you can achieve the effect you want on both GridView and ListView.
Remove android:descendantFocusability in activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2" />
Remove android:focusable in dummy_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dummy_item_parent"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:clickable="true"
android:background="#drawable/item_background" />
Change from android:state_focused to android:state_selected in item_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/holo_blue_light" />
<item android:state_selected="true" android:drawable="#android:color/holo_orange_light" />
<item android:drawable="#android:color/holo_red_light" />
</selector>

[Android]ListView cuts TextView text

In my project I have an Activity with a ListView with a BaseAdapter that shows a custom item list layout.
This list item layout has a textview and his text is cut off if with more than one line.
The code that I have written is like this:
Activity's onCreate method:
lv = (ListView) findViewById(R.id.listView3);
listaCompleta = new ArrayList<String>();
listaCompleta.add ("Item1");
listaCompleta.add ("Example text");
listaCompleta.add ("This is an example text for writing more than a line in the listview item row blablabla blablabla blablabla");
adapter = new BaseAdapter () {
#Override
public int getCount() {
return listaCompleta.size();
}
#Override
public Object getItem(int position) {
return listaCompleta.get(position);
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.item_list_layout, null);
TextView tv1 = (TextView) view.findViewById(R.id.testoItemList);
tv1.setText(listaCompleta.get(position));
return view;
}
};
lv.setAdapter(adapter);
item_list_layout.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/item_list_layout">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testoItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"/>
<CheckBox
android:id="#+id/checkBoxItemList"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="#drawable/checkbox_selector"/>
And checkbox_selector.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/star" android:state_checked="false"/>
<item android:drawable="#drawable/star_active" android:state_checked="true"/>
<item android:drawable="#drawable/star"/>
I found online many post as mine but all without solution.
How can I fix this problem?
Try replacing your TextView with this
<TextView
android:id="#+id/testoItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="false"
android:gravity="center_vertical"/>
I think you should change your layour_heigth in the text view and Linearlayout for wrap_content. If textview have more lines de container expand to adjust. I you choose match_parent the size of container set by the listview.
You change this line:
View view = getLayoutInflater().inflate(R.layout.item_list_layout, null);
for this:
View view = getLayoutInflater().inflate(R.layout.item_list_layout, parent,false);
or layout parameters are ignored.
This works fine
Try this. Change
android:layout_height="match_parent"
to
android:layout_height="wrap_content"
for example:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testoItemList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"/>

Selectable TextView in a ListView (Animation missing)

I built a ListView with a Custom Adapter so that I have a Checkbox (invisible at the time) and a TextView to display some text.
Now I want to have the beautiful animation when selecting an item in the List (Background beeing blue for the time you touch the element). Is this possible?
I already added an OnLongTouchListener and it worked but no animation!
Would be cool to get some help :)
Layout-XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/list_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:visibility="gone"
android:text=""
android:focusable="false"
android:clickable="false">
</CheckBox>
<TextView
android:id="#+id/list_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:clickable="true"
android:longClickable="true" >
</TextView>
</LinearLayout>
The Java-Code:
private class CommunityListAdapter extends ArrayAdapter<Community> {
private List<Community> items;
private Context context;
public CommunityListAdapter(Context context, List<Community> communities) {
super(context, R.layout.list_item, communities);
this.items = communities;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, null);
}
Community item = items.get(position);
if(item != null) {
TextView textView = (TextView) view.findViewById(R.id.list_text);
if(textView != null) {
textView.setText(item.getName());
textView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View view) {
startActionMode(new MultiChoiseCABListener());
Toast.makeText(context, "TEST", 10).show();
return false;
}
});
}
}
return view;
}
}
Create pressed state xml in your drawables in which you set different bitmaps to be displayed at different states of tap, and set that xml as your main layout background.
example state xml.
list_item_bg_state.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/blue_bg" />
<item android:drawable="#drawable/white_bg"/>
</selector>
where blue_bg and white_bg are two.png images in your drawables.
note above xml should be in your drawables folder. Now set this xml as background of your list item main layout. like,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_item_bg_state"
android:orientation="horizontal" >
</LinearLayout>
regards,
Aqif
You havent defined animation please define animation and associate with this view.
Animation a = new AlphaAnimation(1.00f, 0.00f);
a.setDuration(1000);
YOURTEXTVIEW.startAnimation(a);

How to highlight ListView-Items

i got the following problem.
i have a ListView with custom rows consisting of an imageview and a textview.
the textview's xml code is
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:textColorHighlight="#FEC403"
/>
then i have an itemclicklistener that works fine and i want to highlight the textview that has been clicked by doing the following.
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
long id) {
//TODO: ACTIONS
String pathtofile = (String) adaptview.getItemAtPosition(position);
View rowview = (View) adaptview.getChildAt(position);
rowview.setSelected(true);}
i wanted the highlight color to be "#FEC403" in the xml (that is a light orange) but the highlightcolor still is gray. so how to set the highlightcolor correctly?
thanks in advance
EDIT:
here is how i did it finally:
this is my ListView Item xml file:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/rowselector"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/musicicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/musicicon"
android:paddingLeft="3px"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false"
/>
and the rowselector.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/orange" />
</selector>
and at last my OnItemClick is very short now:
#Override
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
long id) {
//TODO: ACTIONS
clickedview.setSelected(true);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
{
for(int a = 0; a < parent.getChildCount(); a++)
{
parent.getChildAt(a).setBackgroundColor(Color.BLACK);
}
view.setBackgroundColor(Color.RED);
}
You should use a Selector.
This question and its answer might help....
In your Activity:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long itemid) {
itemOnclickList(position, view);
}
});
public void itemOnclickList(int position, View view) {
if (listview.isItemChecked(position)) {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_checked));
} else {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_uncheck));
}
adapter.notifyDataSetChanged();
}
In your Adapter:
public View getView(int position, View view, ViewGroup parent) {
View convertView = inflater.inflate(R.layout.listdocument_item, null);
ListView lvDocument = (ListView) parent;
if (lvDocument.isItemChecked(position)) {
convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_checked));
} else {
convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_uncheck));
}
return convertView;
}
Good luck!
This is how it should look using selector:
Create a file within drawable called selector_listview_item.xml, which looks like (change the colors to your own):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#color/md_grey_200" android:state_pressed="true" />
<!-- default -->
<item android:drawable="#color/white" />
</selector>
Now in your layout which describes the list row (e.g. layout_list_row.xml), On the root layout add the android:background="#drawable/selector_listview_item", So the layout would look something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/selector_listview_item" <!-- this is the important line -->
android:orientation="vertical">
<!-- more stuff here -->
</LinearLayout>
(Note: you might want to add android:focusable="false" on you items within that listview row, to make the row clickable, as mentioned here)
Done.

Categories

Resources