Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android windowBackground turns with device

I have an androidapp with a background but I want to stay it in portrait always. So the activity can rotate, just the wallpaper can't. Is this possible?

like image 966
vincentkriek Avatar asked Jan 28 '26 13:01

vincentkriek


1 Answers

You could do this through code or through using the native behaviour of how landscape and portrait layout xml files are chosen based on the screen rotation.

In you res/ folder you by default have the layout/ folder. In here for example you might have main.xml with the background of your element set to @drawable/background_portrait.

What you can then do is create a new folder in the res/ folder called layout-land/ and within here you will also have main.xml

In the latter instance you should update the main.xml (In the layout-land/) folder to specify the background as @drawable/background_landscape.

You can then customise each background image as required. The Device will display the main.xml file based on the current orientation.

Through Code In Code, in the onCreate() method of your activity or perhaps through binding to an orientation change event you could determine the current Orientation and update the background property of the layout.

protected void updateBackground()
{
    if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
    {                   
        //pseudo code
        setBackround(R.drawabl.background_landscape);
    }else{
        setBackround(R.drawabl.background_portrait);
    }
}
like image 176
Emile Avatar answered Jan 31 '26 05:01

Emile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!