Search code examples
androidxmlandroid-layouttextviewscrollview

ScrollView/TextView not displaying all data when layout_gravity is set


Whenever the android:layout_gravity="bottom" attribute is set on the style xml sheet, the text seems to be cut off at the top, and extra blank lines appear at the bottom. I have set this in order to auto-scroll to the bottom when there is too much text to display. This is hosted in a DialogFragment.

Note: the text being displayed is from TextView.setText(Html.fromHtml()), with newlines being done as < br> if that matters. The a's printed are just test data.

On debugging the TextView, it appears the data is there (in the mText variable), but it just doesn't display.

Dialog xml sheet:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fillViewport="true" >

        <TextView android:id="@+id/status_olcr_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />

</ScrollView>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/buttonBarStyle" >

    <Button
        android:id="@+id/status_olcr_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:clickable="false" />

</LinearLayout>

On screen display of what is happening (but shouldn't):

Scrolling to the top (wrong)

Scrolling to the bottom (wrong)

And this is what should be happening (when there is no layout_gravity attribute):

Scrolling to the top (correct)

Scrolling to the bottom (correct)

Any idea of whats happening?

Update: I have turned on 'Show layout margins' and the TextView is rendered above the ScrollView bounds (or other way around).. See below images:

Update: Scrolling to the top

Update: Scrolling to the bottom


Solution

  • Ended up going with this solution, although I still don't understand what was going on in the first place...

    Within the relevant DialogFragment:

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