Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout change when orientation changes in Android Fragments

please suggest a solution.

When i rotate my fragment it should change in to landscape mode and to display another layout.But screen is not rotating to landscape.

My code blow:

 <activity
        android:name=".activites.MainActivity"
        android:launchMode="singleInstance"
        android:configChanges="keyboardHidden|screenLayout|screenSize|orientation"
        />

This is main layout called dashboard and now it is in portrait mode:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View  view=View.inflate(getContext(), R.frag_dashboard,null);
     changeview= (ShimmerTextView)view.findViewById(R.id.changeview); 
     return view;
}

when i rotate the screen this fragment changed to landscape mode and set another layout, and prayer_times is the new layout.

   @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(getContext(), "landscape", Toast.LENGTH_SHORT).show();
        view=View.inflate(getContext(), R.layout.prayer_times,null);

    }
}

and i create layout_land for prayer_times

like image 997
AndroidSRS Avatar asked Jul 19 '16 05:07

AndroidSRS


People also ask

What happens when orientation changes Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

What will happen if an activity with a retained fragment is rotated?

Fragments — Scenario 3: Activity with retained Fragment is rotated. The fragment is not destroyed nor created after the rotation because the same fragment instance is used after the activity is recreated.

How do you change orientation when retaining data?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

Which method of an activity is fired whenever there is a change in display orientation?

When the screen orientation of an Android device is changed, the current activity being displayed is destroyed and re-created automatically to redraw its content in the new orientation. In other words, the onCreate() method of the activity is fired whenever there is a change in screen orientation.


1 Answers

If your fragment has no issue of reloading when orientation change you can simply reload.

Add two layout with same name in layout and layout-land folders.

This will show correct oriented layout when load, for change layout when device rotate add following in onConfigarationChanged method inside fragment itself.

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        try {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (Build.VERSION.SDK_INT >= 26) {
             ft.setReorderingAllowed(false);
            }
            ft.detach(this).attach(this).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
like image 62
UdayaLakmal Avatar answered Oct 08 '22 08:10

UdayaLakmal