I have an activity that has two layouts one in the folder layout and the other one in
layout-large, (one is for the phone factor and the other one for the tablet form). How
do I detect which layout is being loaded since in the tablet form I display more data?
Thanks
EDIT:
I know that in the tablet it will load the layout-large but how do I know that I am running in something with a tablet form factor?
You can detect it programmatically:
(getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE;
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
and with other methods and properties in getResources().getConfiguration()
A simple approach would be to keep a Text view with visibility set as GONE. Keep text property for each layout different. In your code check for this view and identify which layout has been loaded.
Related
I stuck in a little bit problem. I have situation like this:
Login activity with one layout.
Three Fragment every one with three different layouts, (Phone portrait, landscape, Tablet landscape)
For better image, Fragments function is: login, register and confirmation code.
What i want to achieve is, good screen rotate + after rotate open correct fragment with stored register data.
I'm not sure if it will helpful but i use in project greenrobot event bus.What i need is suggestion how i could implement it correctly. Which is best approach etc.
Thank you for your feedback!
You should use quantifiers to load the great layout depending on your screen size and orientation.
For example, activity_main.xml from layout-large-land and layout folders wont react the same. The first one will only be loaded if you are on a tablet and landscape oriented. The second one will be the default layout.
You will need to use small, large, landscape (land) and portrait (default) quantifiers.
About loading datas, you can use saveInstanceState bundle. Note that views with an id will automatically handle it.
I have two background images. I want Image A to display when the screen is horizontal and Image B to display when the screen is vertical. Any easy way of achieving this?
Do this
Result Values for Oreintations will be:-
Portrait == 1
Landscape == 2
int i = context.getResources().getConfiguration().orientation;
if (i == Configuration.ORIENTATION_PORTRAIT) {
yourimg.setBackground(yourAimage);
} else {
yourImg.setBackground(yourBimage);
}
you can create two xml layouts here one for landscape and other for portrait. In portrait xml you can add different images for background. By this we can use two different layouts for two orientations for a single activity. see the below picture, hope it should help.
take two images A & B. save image A in drawable-land and image B in drawable-port
note that the images are saved with same names..
you can check this in graphic layout also..
Create each XML for horizontal & Vertical
Create one more folder name "layout-land"
n just coy & paste the main.xml(your layout file) and just change the background respectavely.
you'll done...
My layout has TextViews that need to be in a certain position on the screen in that layout. If I have a layout that displays how I want it on say a 10" tablet, how can I get it to display the same on say a phone screen and keep it relative to what it would be on the tablet?
meaning how can I get both the tablet and the phone to look exactly the same xml layout views without changing them manually? Is there an xml attribute I can use that will scale the view correctly to the device screen?
This can help you:
Supporting multiple screens
When making apps for Android 3.x, you could basically just check if the device was in landscape to see if it could fit multiple fragments side by side. But this is only because you were basically guaranteed that the device is a tablet.
With the introduction of the compatibility library and android 4.0, I can't think of what I would consider as a 'good' way to determine if I can get away with side by side fragments or should revert to standard one fragment per 'screen.'
In the NewsReader example app, Google has a special values file for every common resolution, each with a Boolean value for whether the resolution should support the 2 fragment layout, but I think this way is poorly conceived. The only way I can think of is to check the size of the screen (to guess if it is a tablet or at least big enough to not ruin the layout), and then check the orientation.
So if anyone out there has an idea how to easily and efficiently check this, please let me know!
You can actually see the screen size type (small, normal, large, etc) by using the following:
getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK
That'll return an int that you can check against any of the following:
Configuration.SCREENLAYOUT_SIZE_SMALL
Configuration.SCREENLAYOUT_SIZE_NORMAL
Configuration.SCREENLAYOUT_SIZE_LARGE
Configuration.SCREENLAYOUT_SIZE_UNDEFINED
Then you can check for orientation, using
getResources().getConfiguration().orientation
Which will match any of the following possibilities
Configuration.ORIENTATION_PORTRAIT
Configuration.ORIENTATION_LANDSCAPE
Configuration.ORIENTATION_SQUARE
Configuration.ORIENTATION_UNDEFINED
With this you can essentially simulate the multiple views by adding your fragments as space allows based on screen size and orientation.
I create 2 layouts, one with one container (ViewGroup) for a fragment, and the second layout has 2 containers side by side. I use Android's folder structure for resources to specify that the scenarios where I want the multi-fragment (2 container) layout. Then in code, check for the existence of the second container to determine if you are multi-panel or not.
isMultipanel = false;
ViewGroup container2 = findViewById(R.id.container_2);
if (container2 != null) {
isMultipanel = true;
}
Try not to think of it as tablet or phone, but rather wide enough or not wide enough for 2 panels.
If I wanted multi-panel on normal size devices (<= 5") and large (5" - 7") only when they are in landscape, but always on xlarge (> 7"), I would put the multi-panel layout file in:
layout-land
layout-large-land
layout-xlarge
Then you don't have to do size checks in code since Android is already handling this for you.
I have downloaded the google io application and I noticed that for landscape layout, its only the dashboard layout that have a landscape layout (in the layout-land folder).
My question is the following, is it for all the layout available in our project that we must create a landscape layout or it is only for the dashboard layout?
Thanks.
Kind Regards.
You are allowed to create a landscape layout for any layout in your app. You should do this when you want to have a different layout in portrait vs. landscape. Sounds to me like the Google IO app specifies a different landscape layout for only one screen (the home screen/dashboard), presumably because that's the only screen the developers felt needed a different landscape layout. Point is, it's up to you which screens have alternate layouts.
It depends on the data you have to show.
Sometimes there is more data which can be shown because of extra width in landscape and you have to compress data vertically in landscape. For such cases use different layouts