Maddie_J
Maddie_J

Reputation: 171

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.

Upvotes: 2

Views: 7188

Answers (2)

Kostas Drak
Kostas Drak

Reputation: 3260

Change this:

android:configChanges="orientation|screenSize|keyboardHidden"

to this:

android:configChanges="screenSize|keyboardHidden"

With the first statement you basically tell android not to recreate the activity when screen rotates!!!

Upvotes: 5

Haniyeh Khaksar
Haniyeh Khaksar

Reputation: 804

use this in your configuration method:

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

Upvotes: 3

Related Questions