Arantik
Arantik

Reputation: 584

Full screen activity in landscape mode at the same time with configChanges

In my activity, I have an ImageView that moves to (100, 100) coordinates by clicking on it. I added following code before setContentView() in my activity to have full screen view in landscape mode:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Everything works fine and when I turn device to landscape mode, I have a fullscreen view. but I need the activity doesn't recreate by turning to landscape mode. For example, if I clicked on ImageView and it moved to (100, 100), after going to landscape mode it should stay on this coordinates. For this, I added this line to Manifest:

android:configChanges="orientation|screenSize"

But by adding this line to manifest, fullscreen property stops working. How can I have these two properties simultaneously please?

Activity :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    setContentView(R.layout.activity_main);

    final ImageView imageView = (ImageView)findViewById(R.id.imageView);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            imageView.setX(100);
            imageView.setY(100);
        }
    });
}
}

XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView"
        android:background="#000000"/>
</LinearLayout>  

Manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Upvotes: 1

Views: 3339

Answers (1)

Arantik
Arantik

Reputation: 584

As Edson Menegatt said, I add the following code at the end of my activity, and now my problem is solved:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}  

The codes before setContentView() that I wrote in my question, are necessary too.

Thank you Edson Menegatti ...

Upvotes: 1

Related Questions