Search code examples
androidlistviewandroid-listviewprogrammatically-created

How to set listview to its original position?


I have spent enough time to make it work for my listview to setProperty "above" of layout which align_parentBottom=true.

Seems like very easy right? No its not.

Please see this image so you can visualize the problem and have some idea about it.

enter image description here

When User scrolls listview, home button @bottom hides.

And by default, home button stays as it is if list scrolling is in idle state. This needs to be done.

So my problem is that After home button Visibility goes GONE then listview fills whole space towards bottom means MATCH_PARENT, thats fine.

But when scroll reaches to idle state & Visibility set to Visible then last Item of listview goes/hides behind home button.

It means listview is not getting to its original place as Image shows.

Tried By making it programmatically to set "above" property of list as Home Button is align_parentBottom=true.

also tried by changing whole layout to Linear layout and giving weight=1.

But didn't succeed. Any help will be appreciated.

Thanks.

Layout File Fragement:

<LinearLayout
    android:id="@+id/llmain"
    android:visibility="visible"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/d_10"
        android:layout_marginTop="@dimen/d_5"
        style="@style/text_size_title"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/d_10"
        android:layout_marginRight="@dimen/d_10"
        android:layout_marginTop="@dimen/d_5">

        <TextView
            android:id="@+id/txtDateTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/comment_n_time_color_text"
            android:textSize="14sp" />

        <LinearLayout
            android:layout_width="@dimen/d_1"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/d_3"
            android:layout_marginRight="@dimen/d_3"
            android:background="@color/comment_n_time_color" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_like" />

        <TextView
            android:id="@+id/txtLike"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/d_3"
            android:layout_marginRight="@dimen/d_3"
            android:textColor="@color/comment_n_time_color_text"
            android:textSize="14sp" />

        <LinearLayout
            android:layout_width="@dimen/d_1"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/d_3"
            android:layout_marginRight="@dimen/d_3"
            android:background="@color/comment_n_time_color" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/comment" />

        <TextView
            android:id="@+id/txtComment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/d_3"
            android:layout_marginRight="@dimen/d_3"
            android:textColor="@color/comment_n_time_color_text"
            android:textSize="14sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/article_detail_separator"
        android:layout_marginTop="@dimen/d_5"
        android:background="@color/comment_n_time_color"></LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/d_10"
        android:layout_marginTop="@dimen/d_5"
        android:text="@string/article_comment"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/d_10"
        android:layout_marginRight="@dimen/d_10"
        android:layout_marginTop="@dimen/d_5"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/txt_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/d_10"
            android:layout_weight="1"
            android:hint="@string/article_comment_hint"
            android:textColor="@color/black" />

        <ImageView
            android:id="@+id/img_send"
            android:padding="@dimen/d_10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/send_button" />

    </LinearLayout>

    <ListView
        android:id="@+id/listview_comments"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@null"
        android:listSelector="@null">
    </ListView>
</LinearLayout>

And My Activity Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/parent"
    android:background="@android:color/white">

    <include
        android:id="@+id/toolbaar"
        layout="@layout/partial_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true" />

    <View
        android:layout_below="@+id/toolbaar"
        android:id="@+id/viewline"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/separator_color" />

    <FrameLayout
        android:layout_above="@+id/llbottom"
        android:layout_below="@+id/viewline"
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

    <LinearLayout
        android:id="@+id/llbottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <include
            android:id="@+id/img_home_"
            layout="@layout/home_icon_footer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

Solution

  • I have fix this issue by

    listview.scrollTo(0 , homeBtn.getHeight());

    Which actually moves my list items to scroll on up Y direction when I get last item or say When there is not the first item of the list.