In the Android document: "Supporting Multiple Screens" Google describes how to employ different layout schemes for different screen sizes:
res/layout/my_layout.xml
res/layout-small/my_layout.xml
res/layout-large/my_layout.xml
res/layout-large-land/my_layout.xml
res/layout-xlarge/my_layout.xml
...
The problem is that for every layout the same code is reached: in 'onCreate' I'm using:
setContentView(R.layout.my_layout); and of course the right layout (xml) will be called according to the screen size.
What I want is that the xlarge and small screens will have very different layouts, in that case the Java code will be very different. My question is how do I differentiate these cases in the code? do I have to use some if/else or can android do it automatically?
My question is how do I differentiate these cases in the code? do I have to use some if/else or can android do it automatically?
You will have to "use some if/else", something like this:
if (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_LARGE)==Configuration.SCREENLAYOUT_SIZE_LARGE) {
// yes, we are large
}
else {
// no, we are not
}
I wanted a navigation drawer to open on launch on the smaller screen sizes and stay closed on launch on tablets.
This is the code I ended up with:
if(getResources().getConfiguration().smallestScreenWidthDp < 600){
//Open the navigation drawer automatically
((DrawerLayout) findViewById(R.id.drawer_layout)).openDrawer(GravityCompat.START);
}else{
//Don't do anything
}
Related
I have created one Android application.
When I run my application in Mobile Phone it works very well, but when I run in Tablet the layout of application is changed.
So, how to make responsive Android application which is used in Mobile and also in Tablet?
On Android we can use screen size selector, introduced from Android 3.2, to define which layout to use.
More details available at http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html. Following code snippet has been extracted from the same link :
public class MyActivity extends Activity
{
#Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate();
Configuration config = getResources().getConfiguration();
if (config.smallestScreenWidthDp >= 600)
{
setContentView(R.layout.main_activity_tablet);
}
else
{
setContentView(R.layout.main_activity);
}
}
}
Another good reference for size configuration is keeping separator. This is explain in details at : http://www.vanteon.com/downloads/Scaling_Android_Apps_White_Paper.pdf
I am only talkin about Mobile Responsive Design.
With layouts, I believe you can only current differentiate by the following:
res/layout/my_layout.xml // layout for normal screen size
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-large-land/my_layout.xml // layout for large screen size in landscape mode
You can find more info on what you can add to the folder structure to differentiate between different settings Documentation and android-developers.blogspot
In order to accommodate other types of tablets and screen sizes android introduces a new way to specify resources for more discrete screen sizes. The new technique is based on the amount of space your layout needs (such as 600dp of width), rather than trying to make your layout fit the generalized size groups (such as large or xlarge).
Update: There are essentially two ways you can give your audience a good experience utilizing responsive design:
Optimize the layout of your content.
Adapt the content that’s shown.
Update2: Write this code in your activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscapeView);
} else {
setContentView(R.layout.portraitView);
}
And also add this line in your Manifest file
android:configChanges="orientation|keyboardHidden|screenSize"
So this will handle both things, it will not restart your activity and will load the layout as per your orientation changes. For more information go to http://www.codeproject.com/Articles/422431/Handling-screen-layout-changes-in-Android
#Sagar Zala
Try to use constraint layout as parent view for your layout which will help to make your application responsive for phone and device.
Constraint Layout
You have to create layout for different screen sizes as creating a single design layout and hoping that it would work and look good on all screen size is next to impossible
As defined in android docs for supporting multiple screen sizes we need to focus on using 3 things.
using more Constraint Layout as it provide us with using more relative styling as compared to other layout component
Create alternate layout using qualifiers in android studio
use bitmaps or svg component that could stretch without being blurred.
ConstraintLayout
It supports things like percentage values,now combine those with guidelines it becomes a powerful tool for designing layout
Alternate Layout
In the layout design tab of android studio click orientation change icon then select create other... options from the dropdown list . Now you need to select qualifier from the provided list and create layout relative to different screen sizes without the need of extra code to write for showing those layout. Those layout would appear when a provided qualifier layout design matches.
You could follow the link provided above to see how they used smallest width qualifier with values
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a large phone screen ~5" (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
These are the commonly used custom values that covers nearly all device sizes, you could add more refined sizes according to your need and then you could also add different orientation qualifiers too with addition to smallest width qualifiers.
Combination of all those would make your application completely responsive
I know i am answering to an old question but may be my answers helps other in search of similar query with a recent available solution.
I would like to do the following in my app:
Portrait mode only for the phone
Portrait mode and landscape mode for tablets.
I know i can easily change the orientation in the manifest file but that will affect the entire application.
I have also thought of creating separate activities, to handle the different versions but i don't know how to detect the type of device using the application.
Does anyone know how to tackle this?
You can use the following piece of code to differentiate between normal/large screen and block the orientation change accordingly.
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
In activity's onCreate method check whether app is running on phone or on tablet. If app is running on phone set activity screen orientation to portrait only.
Add these files to your res folder.
res/values/vars.xml:
<?xml version="1.0" encoding="utf-8"?><resources><bool name="is_tablet">false</bool></resources>
res/values-sw600dp/vars.xml:
<?xml version="1.0" encoding="utf-8"?><resources><bool name="is_tablet">true</bool></resources>
In onCreate method off all your activites add this code:
if (!getResources().getBoolean(R.bool.is_tablet)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
You have to identify the tablet with Android screen qualifiers:
Then you put in the right layout folder the settings you want. For example you can add the attribute orientation:portrait|landscape only in the layout xml file for large screens, and orientation:portrait for all the others. See the folder structures here:
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
Take a look to the google guide: http://developer.android.com/guide/practices/screens_support.html
i suppose you can do small trick for it, try use Configuration.screenLayout :
switch (this.getResources().getConfiguration().screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
return "small";
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return "normal";
case Configuration.SCREENLAYOUT_SIZE_LARGE:
return "large";
case 4: // screen xlarge
return "xlarge";
default:
return "undefined";
}
}
thanks
The problem that you are coming across unfortunately becomes a grey area. We can decipher what density a phone is and the physical size of a device. But there is nothing that really decides to us, what a phone is and what a tablet it. With phones being sometimes 7 inches tall and some tablets dedicated as just e-readers and such, they really could be any density or size.
However, the table suggested by Santacrab above could be used along with using the appropriate layout attributes that will resize accordingly regardless of the size of the screen. There will be circumstances of course where it makes sense to use fragments to split screens and on a smaller screen to avoid overcrowding the screen, but the links that spin off of the link from Santacrab should provide some good guidance on that as well.
I read info about "Supporting Multiple Screens" and other post here... but Im really confused about how can I develop my application that run on multipple devices.
I was starting develop on a determinate screen (normal size layout), then I run my apk on a S4 galaxy so I see that every object of my apk was diferent size to my xperia to the S4.
what I need to do to make my apk compatibility for all the devices?
I read information that the only source to make that is:
res/layout/my_layout.xml // layout for normal screen size
("default")
res/layout-small/my_layout.xml // layout for small
screen size
res/layout-large/my_layout.xml // layout for large
screen size
res/layout-xlarge/my_layout.xml // layout for extra
large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra
large in landscape orientation
For:
res/drawable-mdpi/my_icon.png // bitmap for
medium density
res/drawable-hdpi/my_icon.png // bitmap for high
density
res/drawable-xhdpi/my_icon.png // bitmap for
extra high density
is this true? so I need to make different layouts for different devices?
guys please help me with this issue cz I relly dont understand how is the deal for multiple devices compatibility.
For the layouts:
You can most likely get away with one set of layouts for your application. By default, Android's gui system is done in such a way that you should be able to make good use of the screen of any device, for example use layout_weight and specify things in dp (density-independent pixels). By providing only one layout though, you're most likely going to have it optimized for phone or tablet sized screens. If you create it for phones, then tablets will probably have a lot of empty space, whereas creating another set of layouts would allow you to put extra info on the screen than the phone layouts could fit. It depends how willing you are to customize your app experience for each users device. To create multiple layout, it's recommended to use the Android 3.2 size qualifiers. For tablets, you could use res/layout-sw600dp. See here under "Using new size qualifiers".
For the drawables:
You will most likely not want to have just one set of drawables. You can, and the Android system will scale all your drawables to maintain the same physical size on all screen sizes, but due to scaling, the images will not look very good. That's why you provide an image that will look good for each "category" of screen density, and the Android system will choose the one for that device's screen density, and any scaling it has to do will be minimal.
It is kind of confusing at first, but you most likely don't have to worry about the layouts specifiers just yet. I would make sure to read the supporting multiple screens article and writing your xml layouts such that they make use of the screen size in a relative way.
Have you read this page? Or this one?
You create resources with duplicate names located in folders that are qualified a certain way. At runtime, the system will choose the resources that best match the current configuration of the device. If you want your device to work on multiple screen sizes and multiple screen densities, you will need to create different resources (layouts, drawables, etc) and qualify them appropriately.
I am new to android and having requirement to design my home screen for the app.which look like as shown below.
Home screen image.
as the picture shown above contain five button which having five different drawable image with different design and different size.the same layout while running in one device look different compare to the one running on other device.different device means here the different screen size and different resolution.so now the question is how to design that size and resolution of image and layout so its fit with any screen size and resolution on any device android device available in market and never expand or collapse with different screen size.i am testing the app in three different screen size device and resolution also and the look change for every device.Any kind of reply will be very helpful.
Use RelativeLayouts to separate the different boxes.
Apart from that, whatever you asked is broad.You should refer the Designing for Multiple Screens documentation available at android.
You should follow the advice that's detailed in the documentation on how to design for different screen sizes. You're going to have to have more than one image to handle all your scenarios obviously.
http://developer.android.com/guide/practices/screens_support.html
place Button background images in 4 resolutions in the res folder as ldpe mdpi hdpi and xhpdi .use Dip insted of dp.
I am trying to make a application in Android. I want that it should be able to run on multiple phones of different screen sizes, so i studied support multiple screen on developers and according to that i have to create 3 different xml files for supporting three different screen sizes and also 3 different types of images for each type of xml file. But on a blog i get the idea of doing this by using current screen size method. So i am confused what i should do. means which is optimized and performance increasing way. And which one will be more perfect for supporting all types of screen(except extra large screens)
Defining height, width and other parameters in the XML file is the better option rather than on run time.
Because XML files works as metadata (data carrier) to the activity and avoids alot of confusion when onCreate mothode in called.
Plus, create different folders for image quality (hdpi,xhdpi,ndpi,ldpi)
7 inch device use mhpi
10 inch devices use hdpi and xhdpi
While NEXUS tabs use hdpi and xhdpi irrespective of their size.
Mobiles use ldpi and ndpi.
Beauty lies here is that android device automatically pick-p the suitable content when found, i.e layout and image.
If not found it would first search other Layout folders,e.g a layout not found in x-large folder then it will search in large,then medium, small, which one of them suits the best ,(if a layout is not found in its respective folder).
Nexus will create alot of trouble for you.
To check how your layout would look on different devices, try using the options, which tells you how it would look on that device with those height width, present in the Graphical (view of a ) layout.
You can use three different layouts for different screen sizes ,and android will pick the suitable layout , but Using three different layouts for each type of screen format will not be a good idea , because it will cause problem in handling all layout , if screens are less then its fine but if number of screen increases it will get difficult . Like if you forget to add change in one of the screen size it shall crash with any exception .
What you can do is keep images of different size in different folders and practice layout to make standard in one layout by using layout weights , and margins in in dp .
See my this answer
Table Layout spacing issues
and check this layout will look similar for all screen sizes.
If your design is same for all screens sizes you can use dp and have only one xml for all screens.
But you should support icons for all screens.
I think it's less confusing David Ohanyan way, but forgot to say something...
Whenever you can, use styles in your xx_layout, images, etc, so you'll have 1 layout.xml and 3 styles files inside folders: values, values-small, layout-large.
At least for me, it's less confusing than opening 30 different layout files.