Search code examples
androidtextviewhorizontalscrollview

set TextView in HorizontalScrollView to the right


I have a simple construction; a TextView in a Scrollview, but I cannot make it work as I want.

The problems I get now;

  • The ScrollView keeps expanded when once the TextView was larger than the width of the screen, even when I set the text of TextView to nothing.
  • The TextView is on the left side.

I got the following:

       <HorizontalScrollView
            android:layout_weight="1"

            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:fillViewport="true"
            android:layout_gravity="right"

            android:background="@drawable/background_scrollviews_display"

            android:id="@+id/scrollViewSum">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:layout_gravity="right">

                <TextView
                    style="@style/TextStyleDisplay"
                    android:textSize="@dimen/fontSizeTextViewSum"

                    android:paddingLeft="@dimen/paddingDisplayTextViews"
                    android:paddingRight="@dimen/paddingDisplayTextViews"

                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:gravity="right|center"

                    android:id="@+id/textViewSum"/>

            </LinearLayout>
        </HorizontalScrollView>

What I want;

  • The TextView on the right;
  • The ScrollView only expand when the TextView is larger than width of the screen;

I tried a lot, but I simply cannot find the right solution.

I hope someone can help me out!


Solution

  • I have a two clue to solve your problems:

    1) make LinearLayout orientation Vertical, then you can position TextView on the right

    <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
    
                android:layout_gravity="right">
    
                <TextView
                    style="@style/TextStyleDisplay"
                    android:textSize="@dimen/fontSizeTextViewSum"
    
                    android:paddingLeft="@dimen/paddingDisplayTextViews"
                    android:paddingRight="@dimen/paddingDisplayTextViews"
    
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:gravity="right|center"
    
                    android:id="@+id/textViewSum"/>
    
            </LinearLayout>
    

    2) make TextView wrap_content instead of match_parent

    <TextView
                    style="@style/TextStyleDisplay"
                    android:textSize="@dimen/fontSizeTextViewSum"
    
                    android:paddingLeft="@dimen/paddingDisplayTextViews"
                    android:paddingRight="@dimen/paddingDisplayTextViews"
    
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:gravity="right|center"
    
                    android:id="@+id/textViewSum"/>