Is it possible to have two Views in Android; one of which is the "background" and wont rotate when the device is rotated. And have the other one on top (the "UI-elements") rotate on orientation change?
I know I can disable/enable orientation changes for a whole activity but how would I go about that for separate Views in one activity?
I'm not sure if that's possible, but usually to support "landscape" and "portrait" orientations, you would create another folder in your "res" folder and call it "layout-land" then copy your .xml file in "layout" to "layout-land" and make the appropriate changes to your UI-elements.
If you want to keep the background the same, then I suppose you can leave it unchanged or you might have to rotate the image and save a copy of the rotated image to use in landscape orientation.
Hope that's helpful!
Related
I´ve made an app where´s just some text and a background image.
When I tilt my phone my background image gets streched out..
What I want to do is when I tilt the phone I get now background image.
What you need to do is create different layouts for your views in landscape and portrait mode and put the landscape ones in res/layout-land and the portrait ones in res/layout-port. Simply get your current XML file and copy it into both the portrait and landscape stalks and then just change the drawable that is referenced for the background. That way you can have one that fits well for portrait and another that you've edited to fit for landscape views.
you need to provide a different layout for that orientation.
create a layout-landscape folder, and in that layout change the background to the one you want for your landscape layout. besides you can completely customize your landscape orientation that way.
yes you can change the background
for that you have to create another xml file with same name of the xml you are using right now
then copy all the code from your previous xml to this new xml
you have to create this xml inside the land named folder,if that folder dosent exist than crete a folder name land inside your layout folder..
you just have to change one thing in your xml and here you gooo
android:background="#drawable/yournewbackgroundname"
do not change any thing in our old xml
I have a simple gradient background for my app.
So is there a way to set it so when the orientation is changed from Vertical to Landscape, the background changes to a bigger version of picture (a seperate picture, but same gradient effect)?
say you are using this image as a background in the main.xml layout file. what you have to do is create a new folder in 'res' directory named 'layout-land'
create another background image for the landscape mode.
copy the main.xml to layout-land directory.
set the landscape image as the background for the main.xml in layout-land directory.
now you have two different main.xml files. Android OS will automatically pickup the correct layout according to the orientation.
please note that widgets in patriot main.xml file should exist in landscape main.xml too with the same ID. otherwise you will get nullpointer.
You should have to create the another directory as Asanka have said in answer and do it. That solve your issue.
For more information see this.
Enjoy. :)
If I got a graphic design for lets say a TextEdit control as a background image, is it possible to stretch the image when changing from portrait to landscape view?
I'm coming from WEB and with CSS where I could divide the background picture to 3 parts and make the background stretch dynamically. Is something like this possible in android environment or should I use different layouts for each orientation?
You can declare different graphics by dropping them in separate sub-folders of the /res directory in your project. By naming convention, your application will pick them up automatically.
For example, if you use a background image called textBackground.png and you have two versions of it depending on orientation. The app will use res/drawable/textbackground.png by default and res/drawable-land/textBackground.png when in landscape mode, for example.
Check out this doc for the full story:
http://developer.android.com/guide/topics/resources/providing-resources.html
Which is more appropriate to use in Screen Orientation.
Declaring <activity android:screenOrientation="sensor" />
or
the long process of creating a new folder layout-land & a new layout? and why? I'm using the first
one now and seems it has the same function as the latter. Thanks!
I think the two are different. When we use android:screenOrientation we're telling android what orientation we want to be displayed in. Having two layout's, one for portrait and another for landscape helps us in cases where we want to layout our views such that it takes advantage of the increased width. If you don't think if need this then just use a single layout. Android will use the same for both orientations.
If you want to prevent android from displaying your view in a particular orientation, etc you can use the former.
sensor just returns the orientaton of the device. It does not optimize the layout like using layout-land would.
From http://developer.android.com/guide/topics/manifest/activity-element.html#screen
The orientation is determined by the device orientation sensor. The
orientation of the display depends on how the user is holding the
device; it changes when the user rotates the device. Some devices,
though, will not rotate to all four possible orientations, by default.
To allow all four orientations, use "fullSensor".
I want to use screen rotation in android device.
But I don't want to rotate whole activity.
How is this possible to rotate only some of views in a activity
Like there are two button and one ImageView.
I want to rotate only ImageView on screen orientation change.
Plz suggest the way.
Thanks.
Create another xml file with same name and put it in new folder inside res/ folder. Name that new folder layout-land. and change in your xml file according to your requirements. It will automatically change the layout file when the orientation will be changed.