Title: listview with header and stackFromBottom set to true
Tags: android;listview
Question: I have a ListView and I'm inflating a header view into it like so,
getListView().addHeaderView(headerView);
The issue I'm having is that when I set stackFromBottom="true" and there is no item other than the header, it's being drawn at the bottom of the list, it looks like the header just another item in the list.
Is is possible to keep the header at the top when there is no items in the list?
ListView XML
<?xml version="1.0" encoding="utf-8"?&gt;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &gt;
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/form"
android:layout_alignParentTop="true"
android:cacheColorHint="@android:color/transparent"
android:divider="@null"
android:dividerHeight="10dp"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:listSelector="@android:color/transparent"
android:transcriptMode="alwaysScroll" /&gt;
</RelativeLayout&gt;
And the header
<?xml version="1.0" encoding="utf-8"?&gt;
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:paddingLeft="5dp"
android:paddingRight="5dp" &gt;
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="afterDescendants"
android:orientation="vertical"&gt;
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" &gt;
<ImageView
android:id="@+id/imgV1"
android:layout_width="70dp"
android:layout_height="70dp"
app:border_width="4dp" /&gt;
</RelativeLayout&gt;
</LinearLayout&gt;
</FrameLayout&gt;
Comment: Can you post your xml code?

Comment: Just add the header in the xml file above listview and set it to visible or invisble according to your requirement. You can then set listview to stackFromBottom to true. Setting stackFromBottom to true inflates the layout from bottom.

Comment: No it will not be messed up just put the whole layout in the scrollview and put your header view listview inside it. Just try doing it from xml. It may help you.

Comment: That's your wish whether you want it with your way or prefer to try some different way to solve it.:-)

Comment: I don't think that's the right way, as I mentioned earlier I'm using addHeaderView, if I do what you said the scroll will be be messed up.

Comment: I have to change everything with your approach, I'll do that only if there is no other way to do it.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/05/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><listview>', 'Title': 'listview with header and stackFromBottom set to true', 'ViewCount': 212, 'id': 29452853, 'user_AboutMe': None, 'user_AccountId': 4932114.0, 'user_DisplayName': 'Adroid Freak', 'user_Reputation': 329.0, 'user_Views': 34.0, 'user_id': 3971554.0}}