ScrollView not scrolling and restarting app on orientation change
I am creating an android app that connects with a bluetooth module. When the application is created, a thread is executed to connect to the module. When I turn my phone, the app restarts and the connection flow starts again. To get around this, I added
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateHidden"
to the android manifest file. Now the app doesn't restart, but the ScrollView I used in the activity doesn't scroll. Any idea to get the scroll scrolling 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" />
source to share
The actions will be destroyed and recreated when the screen is rotated, and you probably want this, as it will change all the UI elements correctly.
Create your bluetooth connection / stream in a separate singleton class to make it activity independent or expose its respective activity members.
However, remember to destroy it properly so that it doesn't leak / persist outside the legitimate lifecycle of the application.
source to share
The app does not restart when the screen is rotated, this is an action.
Note: your activity will be destroyed and recreated every time the user rotates the screen. When the screen changes orientation, the system destroys and re-creates the foreground activity because the screen configuration has changed and your activity may need to load alternative resources (like layout).
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Can you show us the XML ScrollView declaration?
source to share