1

I have a LinearLayout with layout_gravity="bottom" inside a ScrollView

The problem is when the content reach the top of the ScrollView I can't scroll to see the top of this content.

Problem 1


I only can scroll to see the bottom of the screen but i can see only empty space .

problem 2


This is my Layout XML Code :

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.projects.zak_dev.errors.MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp"> <LinearLayout android:layout_gravity="bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/content" /> </LinearLayout> </ScrollView> </android.support.constraint.ConstraintLayout> 

Can someone help me please !

2
  • because any layout start with in top of the layout. if you want to see space in top the set margin in top. Commented Oct 25, 2017 at 13:30
  • @Destro i am trying to create a chat app .. you know we scroll to see the old messages and always the new messages came from the bottom . Commented Oct 25, 2017 at 14:22

4 Answers 4

1

You have to remove android:layout_gravity="bottom".

To get to the bottom of your ScrollView, add android:id="@+id/scrollView" to your ScrollView xml tag and simply add the below code to your onCreate function or any other place where you need it after you've set the text.

final ScrollView scrollView = ((ScrollView) findViewById(R.id.scrollView)); scrollView.postDelayed(new Runnable() { @Override public void run() { scrollView.fullScroll(ScrollView.FOCUS_DOWN); } },100); 

You can try to use post() instead of postDelayed() like this:

final ScrollView scrollView = ((ScrollView) findViewById(R.id.scrollView)); scrollView.post(new Runnable() { @Override public void run() { scrollView.fullScroll(ScrollView.FOCUS_DOWN); } }); 

It does not have a delay, but this can lead to problems in some circumstances.

Sign up to request clarification or add additional context in comments.

Comments

0

See this

ScrollView scr = (ScrollView) findViewById(R.id.scr); scr.setNestedScrollingEnabled(true); 

1 Comment

This requires API Level 21 and i am using 14 as minimum API is there is any other solution ?
0

Add this to the scrollview.

android:fillViewport="true" 

1 Comment

also add android:fitsSystemWindows="true" to the parent layout
0

Use This

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.oss.myapplication.MainActivity"> <ScrollView android:id="@+id/src" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="0dp"> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="vertical" android:layout_below="@+id/layout"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/content" /> </LinearLayout> </RelativeLayout> </ScrollView> </RelativeLayout> 

1 Comment

I want this LinearLayout in the bottom.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.