ScrollView not scrolling and application restart on orientation change

266 views Asked by At

I am building an Android application which connects with a Bluetooth module. When the application is created, a thread to connect to the module is executed. When i rotate the phone, the application restarts and the connection thread is run again. To get around this, I added

android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateHidden"

to the Android manifest file. Now the application doesn't get restarted, but a ScrollView I used in the activity does not scroll. Any idea to get the scrollview scrollable and prevent the application from restarting?

Here is my XML file

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_above="@+id/imageView">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/imageView"
        android:background="#bf000000"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/txtReceive1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <TableLayout
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff3f3f2"
            android:shrinkColumns="*"
            android:stretchColumns="*">

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"

                    android:padding="8dip"
                    android:text="CH" />

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="kWh" />

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="Load" />

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="Date" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/t11"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="1" />

                <TextView
                    android:id="@+id/t12"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t13"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t14"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/t21"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="2" />

                <TextView
                    android:id="@+id/t22"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t23"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t24"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView

                    android:id="@+id/t31"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="3" />

                <TextView

                    android:id="@+id/t32"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:id="@+id/t34"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView

                    android:id="@+id/t41"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="4" />

                <TextView
                    android:id="@+id/t42"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:id="@+id/t43"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:id="@+id/t44"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/t51"
                    android:layout_width="wrap_content"

                    android:gravity="center"
                    android:padding="8dip"
                    android:text="5" />

                <TextView
                    android:id="@+id/t52"

                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t53"

                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t54"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>
        </TableLayout>

        <TextView
            android:id="@+id/txtReceive"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/table"
            android:background="#ff8c00"
            android:padding="6dip" />

        <LinearLayout
            android:id="@+id/lin2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingTop="5dp"
            android:visibility="visible"
            android:weightSum="2">

            <LinearLayout
                android:id="@+id/rel"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:visibility="visible"
                android:weightSum="4">


                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="@drawable/btn_01_small"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton1"
                        android:layout_centerHorizontal="true"
                        android:text="Channel"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton1"
                        android:layout_centerHorizontal="true"
                        android:text="0"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton2"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_02"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton2"
                        android:layout_centerHorizontal="true"
                        android:text="kWh"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton2"
                        android:layout_centerHorizontal="true"
                        android:text="000.0"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton3"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_03"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton3"
                        android:layout_centerHorizontal="true"
                        android:text="Load"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton3"
                        android:layout_centerHorizontal="true"
                        android:text="000"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton4"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_04_small" />

                    <TextView
                        android:id="@+id/textView8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton4"
                        android:layout_centerHorizontal="true"
                        android:text="Days"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton4"
                        android:layout_centerHorizontal="true"
                        android:text="000"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />

                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/rel2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/txtReceive"
                android:layout_weight="1"
                android:orientation="vertical"
                android:visibility="visible"
                android:weightSum="4">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton12"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="@drawable/btn_01_small"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView32"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton12"
                        android:layout_centerHorizontal="true"
                        android:text="Auto ON"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView92"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton12"
                        android:layout_centerHorizontal="true"
                        android:text="00:00"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton22"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_04_small"
                        android:textColor="#ffa5ff45" />


                    <TextView
                        android:id="@+id/textView62"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton22"
                        android:layout_centerHorizontal="true"
                        android:text="Auto OFF"
                        android:textSize="22sp" />


                    <TextView
                        android:id="@+id/textView102"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton22"
                        android:layout_centerHorizontal="true"
                        android:text="00:00"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />


                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton31"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="-14dp"
                        android:layout_marginTop="7dp"
                        android:background="@drawable/send_btn_01"
                        android:text="SET TIME"
                        android:textSize="22sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton32"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="-7dp"
                        android:background="@drawable/send_btn_01"
                        android:padding="0dp"
                        android:scaleType="matrix"
                        android:text="RECHARGE"
                        android:textSize="22sp" />
                </RelativeLayout>

            </LinearLayout>
        </LinearLayout>


    </LinearLayout>
</ScrollView>

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:adjustViewBounds="true"
    android:background="#bf000000"
    android:src="@drawable/base_irya_left1" />

2

There are 2 answers

0
Charlie Scott-Skinner On BEST ANSWER

Activities get destroyed and recreated on screen rotation, and you probably want this as it resizes all your UI elements properly.

Create your Bluetooth connection/thread in a separate singleton class so it becomes independent of the activity, or make its relevant activity members static.

However, be sure to destroy it properly so it doesn't leak/persist beyond the legitimate application lifecycle.

1
Pedro Valério On

The application isn't restarted when the screen rotates, the activity is.

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Can you show us the XML declaration of the ScrollView?