Change GroupView background on expand (ExpandableListView) - android

I was searching how to change groupview's background as it expands/collapse. I didn't found my answer, but I solved by simple doing this:
BaseExpandableListAdapter:
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolderGroup holder;
if ( convertView == null ){
convertView = inflater.inflate(R.layout.home_cell, null);
holder = new ViewHolderGroup();
convertView.setTag(holder);
holder.tvNome = (TextView) convertView.findViewById(R.id.cellNome);
holder.btCodigo = (ToggleButtonEbrimaBold) convertView.findViewById(R.id.btCodigo);
holder.tvLancadas = (TextView) convertView.findViewById(R.id.cellLancadas);
holder.layoutBackground = (LinearLayout) convertView.findViewById(R.id.layoutBackground);
}
else{
holder = (ViewHolderGroup) convertView.getTag();
}
holder.layoutBackground.setSelected(isExpanded);
return convertView;
}
ViewHolderGroup:
class ViewHolderGroup {
TextView tvNome;
ToggleButtonEbrimaBold btCodigo;
TextView tvLancadas;
LinearLayout layoutBackground;
LinearLayout layoutReceiver;
}
home_cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layoutBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="#drawable/disciplina_line">
... GroupView content ...
</LinearLayout>
</LinearLayout>
disciplina_line.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:drawable="#drawable/line_white"/>
<item android:state_selected="true" android:drawable="#drawable/line_selected"/>
</selector>
I wanted to share this alternative. Hope it helps !

Related

[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"/>

Selector for Imageview in ListView

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...

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

Android progressbar in a listview is always indeterminate

I'm trying to set a fixed progress on items in a ListView. I only see an indeterminate spinning circle in the ListView.... what am I doing wrong? (I'm sure the answer is simple... I'm just not seeing it)
For the following adapter layout xml:
<LinearLayout 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/some_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="#+id/some_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Code:
public class SomeListAdapter extends ArrayAdapter<MyItem> {
private static class ViewHolder {
TextView nameTextView;
ProgressBar valueProgressBar;
}
public BestChoiceListAdapter(Context context, List<MyItem> items) {
super(context, R.layout.list_item, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) view.findViewById(R.id.some_text);
holder.valueProgressBar = (ProgressBar) view.findViewById(R.id.some_progress);
view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyItem optionItem = getItem(position);
holder.nameTextView.setText(optionItem.getOptionName());
int progress = (int) (optionItem.getOptionValue());
holder.valueProgressBar.setProgress(progress);
return view;
}
}
The simplest way is to apply the horizontal style to it:
<ProgressBar
android:id="#+id/some_progress"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
This will set up all the attributes you will otherwise need to add manually to apply a determinate progress mode.
If you're curious, this is what that system style looks like, in case you wanted to apply the properties individually:
<style name="Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#android:drawable/progress_horizontal</item>
<item name="android:indeterminateDrawable">#android:drawable/progress_indeterminate_horizontal</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
</style>
HTH
Change the indeterminate mode to false!
<ProgressBar
    android:id="#+id/some_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
/>

ImageView is still null, after findbyView

Hey im trying to get a List working, in that case i have a xml defined item, which i inflate in a extended ArrayAdapter. But after i have inflated the xml, then when i try to grab the imageview, it returns null by findviewbyid...
Here is xml for the item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/product_item_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="if no image avail show this"
android:visibility="gone"/>
<ImageView
android:id="#+id/product_item_imageview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
/>
</LinearLayout>
Method that instatiates the item:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SimpleListItem item= items.get(position);
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.product_item, null);
if(item.getPicture()!=null)
{
ImageView imgVw = (ImageView) findViewById(R.id.product_item_imageview);
String picturePath = item.getPicture();
Bitmap picture = ImageManager.get(getBaseContext(), picturePath);
imgVw.setImageBitmap(picture);
} else {
TextView txtVw = (TextView) findViewById(R.id.product_item_textview);
txtVw.setText(item.getText());
txtVw.setVisibility(View.VISIBLE);
}
return v;
}
Its a,
ImageView imgVw = (ImageView) v.findViewById(R.id.product_item_imageview);
also the same for TextView,
TextView txtVw = (TextView) v.findViewById(R.id.product_item_textview);
Use View v for getting ImageView and TextView from your inflated xml file.
//you are inflating layout in a view v so you need to get them by
ImageView imgVw = (ImageView)v. findViewById(R.id.product_item_imageview);
TextView txtVw = (TextView)v. findViewById(R.id.product_item_textview);

Categories

Resources