Adjusting to different screen sizes not working? - android

I thought I was successful in adjusting for different screen sizes(I was using the eclipse emulators and creating different screen sizes to test my app) but when I test my app on actual devices the result of my app varies. for example for a large screen size I set my emulator to a Nexus S and it will work and look fine, but then I try on an Alcatel One Touch Fierce(real device) which is still considered a large screen size the app play is just a little bit off, then I play it on another device which is also considered a large screen size the app will play just like the emulator. So i guess mt question is why? a samble of how I'm checking for different screen sizes is below:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi)
{
case DisplayMetrics.DENSITY_LOW:
// layout for small sized devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// layout for medium-sized devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// layout for large devices.
break;
case DisplayMetrics.DENSITY_XHIGH:
// layout for really large devices.
break;

Before your case statement, try to print the density value like this:
Log.i("Sushil", "displaymetrics.densityDpi : " + displaymetrics.densityDpi);
And check if it matches with any of your defined case statement. Else add new case statements, it should work. Few more defined cases are :
DisplayMetrics.DENSITY_TV
DisplayMetrics.DENSITY_XXHIGH
Hope this helps.

i don't see any thing wrong with the outcomes. if you run an app designed on a phone emulator-----on a tablet, the layouts will not match.
you have to decide if you wanna support different screen sizes or not if you do, then you'd have to create diffrent layouts for different screen sizes and set the corresponding layout in the OnCreate method of your Activity.
here is how to check if the device is a tablet or a phone:
if(isTablet==true){
setContentView(R.Layout.my_tablet_layout);
}else{
setContentView(R.Layout.my_phone_layout);
}
public boolean IsTablet() {
return (getApplicationContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

Related

Using different screen layouts

I have an app with different versions for screens (small, normal, largue, and extraLargue XML files) each with its XML designed for each type, but I found a Huawei phone with this screen 3.5" HVGA 320x480.
My question is, should not the UI of Android use the small configuration for this screen? Is that the app when running on this phone uses the normal configuration as if it were a nexus4 4.7" 768x1280 as I change that?
I tried to create various types of screen (create other) with multiple configurations without optimal result.
Assuming the 3.5" measurement is on the diagonal, this works out to about 165 dpi which falls into the mdpi (or normal screen size) bucket according to Android's Supporting Multiple Screens guide.
DPI = sqrt(w^2 + h^2) / d
where
w is the width of the display in pixels
h is the height of the display in pixels
d is the physical diagonal measurement of the display in inches
It is quite simple.
You need to add different UI and make controls VISIBILE and GONE.
int appScreen = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(appScreen) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
UIControl.setVisibility(View.GONE);
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
UIControl.setVisibility(View.GONE);
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
UIControl.setVisibility(View.GONE);
break;
default:
UIControl.setVisibility(View.GONE);
}

Setting different orientations for Phone and Tablet on Android

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.

App to fit to other Screen sizes

I have finished creating an app but after taking a look on other phones I have seen that the content of the app does not fit or has moved on the screen from what I set it out as on Eclipse.
So I was looking around on StackOverflow to find out how to do this and I found this short code.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
// layout for small sized devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// layout for medium-sized devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// layout for big-sized devices.
break;
}
I am a little confused on where to put this and what to do, I would guess that this code would go into the pages XML files? and in the //layout for xxxxx would go the xml code for that size phone.
But how do I know how to space the app out without having lots of phone to test them on? just keep using the inbuilt emulator to test on the smallest screen size the medium and the large screen size till it all fits?
Create a single layout using WRAP_CONTENT attribute and use different type of images for all resolutions like drawable-mdpi,drawable-hdpi etc. the key is to use different drawables in proportion and let the OS calculate things and draw the layout for you.
Also refer to this answer.

How to change image according resolution?

I am working on an android application. i have used different layout folder. Like layout,layout-large,layout-xlarge. So that it can adjust for all resolutions. But i am setting images dynamically in activity. How can i change them according to screen resolution? How too big image will replace smaller if we change resolution?
Android works with density buckets, which go from l(low)dpi to xx(extra extra)h(high)dpi.
You want to create different versions of your images in folders as
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
and drawable-xxhdpi if you want to support the Nexus 10.
That's kind of loose from the layout-large folders, which enable you to define different layouts for different sizes.
2 pretty different things, which you can read much more about at
screen practices in Android.
=======
Edit; Seems this wasn't exactly the question.
If you're doing it 'the right way' the Android system will choose the correct image for you, even when adding them dynamically (you can still call R.drawable.my_image from java code).
If for some reason you do have to choose, you can simply check for the current density with something like (a little outdated);
public String getDensity() {
String density;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// will either be DENSITY_LOW (120) , DENSITY_MEDIUM (160) or
// DENSITY_HIGH (240)
switch (dm.densityDpi) {
case 120:
density = "ldpi";
break;
case 160:
density = "mdpi";
break;
case 240:
density = "hdpi";
break;
// use hdpi as default, because flurry shows this will be suited for
// most of our users.
default:
density = "hdpi";
break;
}
return density;
}

Supporting different screen size

I want to make game for supporting all Android device(Tablets and phones).I can get screen size through coding.
Do I have make 3 0r 4 layouts small , medium and large?
Which size for small , medium , large and extra large?
Is there any other way to set layout for all devices?
Here is the Android documentation for what you need:
http://developer.android.com/guide/topics/resources/providing-resources.html
You can use the screen-size, screen pixel density, or other things to determine how to separate your layouts.
You can specify different layouts (and you should try to). Different sizes may use the same layout. What layout the device ends up using depends on a set of rules.
So for example if you wanted to use screen size, you would want to make folders under your res folder called layout-small, layout-medium, layout-large, layout-xlarge. This is because layout is the default folder for layout resources, and then you add a -. In this case, small, medium, large, or xlarge.
Edit: Alex's link above might be what you're looking for. My link is more about HOW to do it, but Alex's link is more about how to do it WELL. I wasn't sure what you were asking exactly.
(1) One way to support all the layout sizes is to put images of suitable sizes into drawable-hdpi, drawable-mdpi, drawable-ldpi and drawable-xhdpi. The image for a device of a particular screen size will be automatically picked by Android runtime system. In this approach you will NOT have to make different layout files for each size.
(2) Second way to achieve the same is to dynamically detect the size(density) of the device and then set the layout accordingly, which can be done like this:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
// your layout for small-size devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// your layout for medium-size devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// your layout for big-size devices.
break;
}
In this approach, you MAY have to make different layout files for each size separately.

Categories

Resources