How to change ListView Item programmatically in Android? - android

I have a ListView and I want to change the listitem of it programmatically. Is there any way to do this? Thank you in advance.
This is my ListView XML code.
<ListView
android:id="#+id/foodsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:stackFromBottom="false"
android:transcriptMode="disabled"
tools:listitem="#layout/item_food">
</ListView>
And here is what I want to show instead of item_food:
"R.layout.item_message"

Use RecyclerView instead of ListView. It was create for this t

Related

Android custom list view?

In my layout I need a custom list view like a box type.
How can do that ?
My XML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="317dp"
android:gravity="top"
android:orientation="vertical"
android:background="#android:color/transparent"
>
<ListView android:id="#+id/slistview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:divider="#2867EA"
android:dividerHeight="1dp"
android:cacheColorHint="#00000000"
>
</ListView>
</LinearLayout>
You should create custom list adapter and supply xml layout for each row.
here's a comprehensive tutorial that fits your needs:
Link
You can use customBaseadapter and viewHolder ...
Create a xml for row of the list view and set the background of the row and create a custom adapter and set adapter to list view.
custom list view

ListView element background color changes to black while scrolling

I have a listview, when I scroll it, that time listview element background color changes to black. I tried couple of hours, but not getting solution.
I tried
android:cacheColorHint="#android:color/transparent"
or
android:cacheColorHint="#00000000"
set the
android:cacheColorHint
to the color you used as a background to the ListViewItem
In my case this works fine
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:layout_weight="1"
android:background="#ffffff"
android:choiceMode="singleChoice"
android:cacheColorHint="#android:color/transparent"
android:scrollingCache="false"
android:fastScrollEnabled="true">
</ListView>
One of the developer says, you have to use these 2 properties compulsory..
android:cacheColorHint="#android:color/transparent"
android:scrollingCache="false"
You can use this code:
// you can use your color also
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
Both color should be same, then your problem will be solved.
Hope it will help you.
Add following attributes to your listview inside xml layout.
android:scrollingCache="true"
android:cacheColorHint="#ffffff"

android:headerDividersEnabled isn't working

I want to have a divider at the top of a ListView (which is placed in RelativeLayout), but when I try to use this, I get only the bottom divider:
<ListView
android:id="#+id/last_contacts_picked_list"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true"
android:paddingTop="50dp"
android:dividerHeight="5dp"
android:layout_marginTop="50dp"
android:divider="#android:drawable/divider_horizontal_textfield"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="..."
android:layout_alignLeft="..."
android:layout_alignRight="..."
android:layout_below="..."
/>
I've got something like this:
http://i051.radikal.ru/1211/4f/a408db5e717f.png
Why doesn't it have the header divider too?
That is not what android:headerDividersEnabled is supposed to do. It means that if you are adding a list header whether that header needs to be separated by a divider or not.
For your problem simply use a linearlayout with ImageView having the divider as source followed by ListView. Hope this helps.

scrolling list view color change in android

I have a problem with the ListView's color while scrolling. When I scroll the ListView, by default I am getting orange color in divider. How to change the color of divider when scrolling the ListView? Can anyone help me?...
my code is:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:dividerHeight="5dp"
android:layout_marginTop="5dp" >
</ListView>
Add this to your xml listview layout
android:listSelector="#android:color/transparent"
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:choiceMode="singleChoice"
android:background="#4a006f"
android:cacheColorHint="#00000000" />
You Must Declare this tag in ListView: android:cacheColorHint="#00000000"
You should set the CacheColorHint
Use this method:
setCacheColorHint(Color.ONE_OF_YOUR_COLORS).
You don't need to change the list items' background color.
You can do also in the xml layout.

How do I remove lines between ListViews on Android?

I'm using two ListViews like this:
<ListView
android:id="#+id/ListView"
android:text="#string/Website"
android:layout_height="30px"
android:layout_width="150px"
android:scrollbars="none"
android:transcriptMode="normal"/>
<ListView
android:id="#+id/ListView1"
android:text="#string/Website"
android:layout_height="30px"
android:layout_width="150px"
android:scrollbars="none"
android:transcriptMode="normal"/>
There is one blank line between the two ListViews. How do I remove it?
To remove the separator between items in the same ListView, here is the solution:
getListView().setDivider(null);
getListView().setDividerHeight(0);
developer.android.com # ListView
Or, if you want to do it in XML:
android:divider="#null"
android:dividerHeight="0dp"
If you want to remove a divider line, use this code:
android:divider="#null"
If you want to add a space instead of a divider line:
android:divider="#android:color/transparent"
android:dividerHeight="5dp"
So, you can use any drawable or color in the divider attribute.
There are different ways to achieve this, but I'm not sure which one is the best (I don't even know is there is a best way). I know at least two different ways to do this in a ListView:
1. Set divider to null:
1.1. Programmatically
yourListView.setDivider(null);
1.2. XML
This goes inside your ListView element.
android:divider="#null"
2. Set divider to transparent and set its height to 0 to avoid adding space between listview elements:
2.1. Programmatically:
yourListView.setDivider(new ColorDrawable(android.R.color.transparent));
yourListView.setDividerHeight(0);
2.2. XML
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
Set divider to null:
JAVA
listview_id.setDivider(null);
XML
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
/>
In XML:
android:divider="#null"
Or in Java:
listView.setDivider(null);
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
android:dividerHeight="0dp"/>
You can put below property in listview tag
android:divider="#null"
(or)
programmatically listview.Divider(null);
here listview is ListView reference.
Or in XML:
android:divider="#drawable/list_item_divider"
android:dividerHeight="1dp"
You can use a color for the drawable (e.g. #ff112233), but be aware, that pre-cupcake releases have a bug in which the color cannot be set. Instead a 9-patch or a image must be used..
You can try the following. It worked for me...
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
I find it easier to implement it in the XML file as it can be harder to trace the line of code in a class with hundreds of lines.
For the XML you can use "null":
android:divider="#null"
For ListFragment use
getListView().setDivider(null)
after the list has been obtained.
If you want to remove lines from
⛔ Problem
Having lines between items from <ListView>
✅ Solution
add an attribute android:drivider="#null"
If this android:divider="#null" doesn't work, maybe changing your ListViews for Recycler Views? 
String txt = ( (TextView) view).getText().toString();
adapter.remove(txt);

Categories

Resources