Search code examples
androidscrollview

Positioning a scrollview within a popup window


I'm working on my first sizeable android project and I'm stuck on one issue. I have to display a popupwindow with text for Terms & Conditions. Due to its size the text has to scroll. My specific problem is that for visual reasons I'd like the top to be well down the screen and nothing I do will make it move from the very top.

The xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:padding="10dip"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ScrollView
        android:id="@+id/tc_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_long"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="A very long string of text"></TextView>

        </LinearLayout>
    </ScrollView>
</LinearLayout>

I've tried adding:

tools:layout_editor_absoluteY="100dp" />

and

android:layout_marginTop="100dip"

in various places - the scrollview itself, the parent LinearLayout etc but nothing works.

I've gone to the code that puts up the window and tried using the scrollTo method within the ScrollView class as below:

 View tcView = inflater.inflate(R.layout.t_and_c_dialog, null);
        ScrollView tcScroll = (ScrollView) tcView.findViewById(R.id.tc_scroll);
        tcScroll.scrollTo(0, 200);
        PopupWindow pwTC = new PopupWindow(tcView, width, width, true);
        pwTC.showAtLocation(new LinearLayout(this), Gravity.CENTER, 0, 0);

Still no joy. Does anybody have any ideas? Many thanks Tony Reynolds


Solution

  • I went on trying various things and came up with the following, which I don't fully understand but works pragmatically. Firstly, using the following code to put up the popup:

    View tcView = inflater.inflate(R.layout.t_and_c_dialog, null);
    PopupWindow pwTC = new PopupWindow(tcView, width, width, true);
    pwTC.showAtLocation(new LinearLayout(this), Gravity.NO_GRAVITY, 0, 800);
    

    This places the window at a hardwired y-coordinate.

    The xml is now:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical"
        android:padding="10dip"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ScrollView
            android:id="@+id/tc_scroll"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:scrollbars="none" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/tv_long"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
    
                    android:text="A very long string of text"></TextView>
            </LinearLayout>
    </ScrollView>
    
    </LinearLayout>
    

    So setting a hardwired window depth. This works OK as shown below.

    Scroll example

    It only gives a correct appearance for a given emulator, so I must work out how to set the sizes programmatically.

    I'd be obliged for any comments on what exactly is going on here, and/or tips on improvement