Search code examples
androidandroid-layoutandroid-orientationandroid-screen-support

Landscape Screen Orientated not working in Android Studios


I have created Android layout resource files for landscape mode for all different screen sizes such as, small, large, medium and extra large , however, when I run the app it does not work in landscape mode as half of the buttons, images are missing from the screen.

I have also included the below line in the android Manifest file.

android:configChanges="orientation|screenSize|keyboardHidden"

I have included the below in all of the actvity java file and still not working.

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

        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(getApplicationContext(), "Portrait Mode", Toast.LENGTH_SHORT).show();
        }else if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(getApplicationContext(), "Landscape Mode", Toast.LENGTH_SHORT).show();
        }

Android does switch it to landscape mode, however, the elements within the screen are displayed incorrectly. I have created a landscape layout resource file, however, the problem is that Android does not use this layout as it uses the same layout in portrait and landscape mode.

Please advice how I can make Android aware of when to use the landscape and portrait mode.


Solution

  • use this in your configuration method:

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }