Question

Screenshot:

enter image description here

I am working on this android theme template. It has a scrollview on its layout so that the cards can scroll vertically. The issue that I am facing is that whenever I open the app, it opens up like in image no. 1 i.e. scrolled automatically to the bottom whereas I would like it to open up at the top just like in Image no. 2. I am a beginner at coding so don't know much about layouts.

Here is the gridview_main.xml code

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/app_bg" >

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

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/feature_banner" />

        <FrameLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/list_padding"
            android:layout_gravity="center"
            android:id="@+id/container" />

    </LinearLayout>

</ScrollView>

and here is the gridview_layout.xml code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center" 
    tools:context="listview.Main">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_height"
        android:background="@drawable/card_bg_pressed"
        android:orientation="horizontal"
        android:padding="@dimen/list_icon"
        tools:ignore="UselessParent" >

        <ImageView
            android:id="@+id/list_image"
            android:contentDescription="@string/content_description"
            android:layout_width="@dimen/listview_icon"
            android:layout_height="@dimen/listview_icon"
            android:layout_marginLeft="@dimen/list_icon"
            android:layout_marginRight="@dimen/list_icon_right"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/icon_email" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_marginTop="-1dp"
            android:layout_marginRight="@dimen/list_padding"
            android:layout_toRightOf="@+id/list_image"
            android:text="Join our Google+ Community and share your themes or find themes     
            android:textSize="@dimen/list_desc_text"
            android:textColor="@color/list_desc_color" 
            tools:ignore="HardcodedText"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/description"
            android:layout_alignParentTop="true"
            android:layout_marginTop="6dp"
            android:text="Community"
            android:textColor="@color/list_title_color"
            android:textSize="@dimen/list_title_text"
            tools:ignore="HardcodedText" />

        <ImageView
            android:id="@+id/imageView1"
            android:contentDescription="@string/content_description"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/card_bg_pressed_top" />

    </RelativeLayout>

</RelativeLayout>
Was it helpful?

Solution

You can use android:descendantFocusability attribute to control that descendants of ScrollView should get focus or not. If you want that the ScrollView should not scroll down automatically then set the value of android:descendantFocusability attribute to blocksDescendants and add this attribute to the LinearLayout which is the child of ScrollView.

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >

OTHER TIPS

To gain focus to the top view you can add this code in your java file....

scrollview.fullScroll(View.FOCUS_UP);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top