2

I have a problem with the display of the layout when I rotate the screen from landscape to portrait o viceversa.

I've created a layout land (layout-sw720dp-land/example.xml ) and port (layout- sw720dp-port/example.xml ).

Until everything is right.

At the layout port, I've given the precise dimensions and the layout land other precise dimensions...

Previously I had a problem with the switch of the two layouts.

The transition from horizontal to vertical layout passed successfully, but the activity was destroyed and rebuilt.

I solved the problem by inserting in AndroidManifest.xml this code:

android:configChanges="orientation|screenSize"

Now the problem is that the transition from horizontal to vertical is not automatic. For example, I open the app horizontally and the app displays the correct layout (the layout-sw720dp-land / example.xml because I use a tablet) but once the app open and i turn the tablet vertically , the vertical layout is not displayed but remains in the horizontal version .. ( for example in the horizontal layout I have a button that its text is " Start " while in the vertical I have the same button that its text is " Finish " and when the app is open and i rotate the tablet from horizontal to vertical the the layout turn making me see , however, the horizontal version , so with the " start " button).

How can I do to show the correct layout WITHOUT re-create and destroy the activity ??

N.B : Using the fragment ... in short, I have a activity_main.xml file that references to activity "MainActivity.java" that inside it has this code:

<FrameLayout
        android:layout_below="@+id/toolbar"
        android:id="@+id/fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:foreground="@drawable/shadow_toolbar"/>

inside of this FrameLayout I view the two layouts in this way :

FragmentExample fragment = new FragmentExample();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

In FragmentExample.java there are the two famous layout:

public class FragmentExample extends Fragment {


public FragmentExample() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.example, container, false);

    // Inflate the layout for this fragment
    return rootView;
}

}

How can I do so to make the automatic switch of the two layout without closing and reopening the app and re-create it without ??

Sorry for my english =)

curiousMind
  • 2,812
  • 1
  • 17
  • 38
Edoardo Goffredo
  • 303
  • 2
  • 5
  • 20
  • override onConfigChanges method and do whatever you want – Khizar Hayat Mar 16 '16 at 13:52
  • Do note that this may sound a lot easier than it really is, particularly depending on the level of complexity in regards to your layout. Not that this is really "helpful" but it is worth weighing if it is worth it. – zgc7009 Mar 16 '16 at 14:09
  • you could post a small example for the two layouts with that method ? please =) – Edoardo Goffredo Mar 16 '16 at 14:09

1 Answers1

0

You cannot use two different layouts for landscape and portrait mode WITHOUT recreating activity. Parameter android:configChanges="orientation|screenSize" says activity to not recreate itself, but it doesn't work properly because in this case another layout file is not set.

If you want to use two different layouts for landscape and portrait mode then remove android:configChanges="orientation|screenSize" from your activity in AndroidManifest.xml and handle saving and restoring activity state through methods onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle savedInstanceState).

Example (see full article):

    public class MainActivity extends AppCompatActivity {

    // These variable are destroyed along with Activity
    private int someVarA;
    private String someVarB;

    ...

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("someVarA", someVarA);
        outState.putString("someVarB", someVarB);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        someVarA = savedInstanceState.getInt("someVarA");
        someVarB = savedInstanceState.getString("someVarB");
    }
 }

See this solution to save and restore fragments.

See also Handling Runtime Changes Documentation.

Community
  • 1
  • 1
dzikovskyy
  • 5,027
  • 3
  • 32
  • 43