0

I have a layout like this:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/default_background">

<LinearLayout
    android:layout_above="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/toolbar"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawablePadding="10dp"
    android:layout_gravity="center"
    android:background="@drawable/color_transparent"
    android:layout_alignParentBottom="true"
    android:textStyle="bold"
    android:visibility="gone"
    android:padding="15dp"
    android:gravity="center"/>

Now when something happens, I want to show the hidden textview below recyclerView simply by setVisibility() and then hide it again. when the textview shows up, the LinearLayout above has to squueze a bit to make room for it, resulting in hiding a bit of content of the recycler at the bottom of the screen. now my question is is there a way to instead of hiding the bottom part it scrolls a bit down hiding the same ammount of space at the top and showing textview just below the previous bottom of the screen. thanks in advance?

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41

2 Answers2

0

Modify your layout a bit

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/default_background">



        <include
            layout="@layout/toolbar"
            android:id = "@+id/toolbar"
            android:layout_alignParentTop = "true"/>
       <ScrollView
            android:id="@+id/scroll_view"
            android:layout_below="@id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
             android:layout_above="@+id/text_view"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical">

                  <android.support.v7.widget.RecyclerView
                   android:id="@+id/recycler"
                   android:scrollbars="vertical"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"/>

                 <TextView
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@id/text_view"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:drawablePadding="10dp"
                  android:layout_gravity="center"
                  android:background="@drawable/color_transparent"
                  android:textStyle="bold"
                  android:visibility="gone"
                  android:padding="15dp"
                  android:gravity="center"/>
         </LinearLayout>
    </ScrollView>
</RelativeLayout>

Now in your activity file do this

//when you want to show your textView suppose on a button click write this snippet
 bottomText.setVisibility(View.VISIBLE);
 scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
         @Override
         public void onGlobalLayout() {
         scrollView.post(new Runnable() {
                public void run() {
                scrollView.fullScroll(View.FOCUS_DOWN);
                }
         });
     }
});

this will take you to the bottom of page and automatically will scroll the view. Hope it helps.

Ashwani
  • 1,294
  • 1
  • 11
  • 24
0

Try this :

<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/main_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/default_background">

<TextView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/text_view"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:drawablePadding="10dp"
   android:layout_gravity="center"
   android:background="@drawable/color_transparent"
   android:layout_alignParentBottom="true"
   android:textStyle="bold"
   android:visibility="gone"
   android:padding="15dp"
   android:gravity="center"/>

<LinearLayout
android:id="@+id/linear"
android:layout_above="@id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    layout="@layout/toolbar"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

 </RelativeLayout>

Problem is your layout can't find textview's id because its declare below Linear Layout. So put TextView above Linear Layout and then give android:layout_above="@+id/text_view" property to Linear Layout.

Note: give id to widget by @+id not by @id . See this Difference between "@id/" and "@+id/" in Android

Vishal Jadav
  • 924
  • 9
  • 11