1

I want keep activity and use different layout (landscape or portrait) when rotate screen.

  • layout (portrait) : there 3 textview, one is visible and other textview is gone
  • layout-land : there 3 textview and all is visible

Followed best answer from here >> Activity restart on rotation Android , I put

android:configChanges="keyboardHidden|orientation|screenSize"

on my manifest and i put this code :

  @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.myLayout);
    }

efect before put above code : Activity and data is keeped but cannot change screen layout land/portrait

after put above code : Activity is keeped, layout is changed, but data is lost

so how to solve it ?

Community
  • 1
  • 1
Amay Diam
  • 2,501
  • 7
  • 32
  • 55

2 Answers2

0

When you write this:

android:configChanges="keyboardHidden|orientation|screenSize"

You are telling the system you will handle the rotation change yourself and so it doesnt do automatic stuff it usually do like saving the data through viewing mode change.

There is no need for this line, just create another layout directory with a resource qualifier "land", the dir should be named "layout-land" and put in it the layout you want the app to have in landscape mode and the data will be saved through the change.

Like so:

enter image description here

Ignore the contents of the directories in the image.

TomerZ
  • 605
  • 6
  • 16
0

How about using ScrollView ?

  1. There is a much simpler way which I use for my own applications,
  2. You can use a ScrollView and place your widgets inside, this will work for both landscape and portrait and you simply just need one layout file

Sample:

activity_main.xml

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- everything you already have -->

    </LinearLayout>
</ScrollView>
edwoollard
  • 12,065
  • 6
  • 41
  • 73
Devrath
  • 39,949
  • 51
  • 178
  • 266