Android set a specific layout for a specific device - android

I'm currently developing an app that as usual has to run on multiple devices but there is one more special than the others. An Acer DA241HL which is a 24" "Tablet" with a 1920x1080px resolution that should be wall mounted somewhere.
Is there a good way to identify this screen. Right now it reacts to the values-sw600dp folder. But what ever changes I make for this enormous tablet shouldn't affect normal tablet which would happen if I change stuff in that folder.
The following code:
DisplayMetrics metrics = getResources().getDisplayMetrics();
float density = getResources().getDisplayMetrics().density;
float dpHeight = metrics.heightPixels / density;
float dpWidth = metrics.widthPixels / density;
Log.e("screen", "******************************************************");
Log.w("screen", "d " + density + " y" + dpHeight + " x" + dpWidth + " deviceType:" + getResources().getString(R.string.device_type));
Log.e("screen", "******************************************************");
Print the following:
******************************************************
d 1.0 y1032.0 x1920.0 deviceType:5
******************************************************
But I dont see how I can make that useful.
The other way I thought about is to get the model number of the device but that creates new problem if they decide to get a similar device but not the same.
Suggestions?

Is there a good way to identify this screen
Use something like -swNNNdp, for a suitably high value of NNN.
Right now it reacts to the values-sw600dp folder
Well, sure, but that screen is far larger than 600dp in its smallest width.
But what ever changes I make for this enormous tablet shouldn't affect normal tablet which would happen if I change stuff in that folder.
Then add a res/values-sw1000dp/ directory, for resources to use for screens with 1000dp in the smallest width, which will include your Acer but will not include more normal-sized tablets.

Related

Trouble creating the right layout folder for 9.7 Tablets and 4:3 aspect ratio

Right now I have three layout folders:
layout, layout-sw600dp and layout-sw720ddp
Now I am getting reports that certain china tablets who often have the aspect ratio of 4:3 and are 9.7 inch won't show everything on the display. (UPDATE: apparently 9.7 are the hot new thing and everybody is releasing them now, like the Nexus 9 and budget tablets by Samsung)
I have scrollviews in almost all of my views so thats not really a problem and most of the layouts should adjust just fine that way but for the one view which can not be fitted with a scrollview I would like to create separate layouts.
These are the stats of the problematic tablets:
1024px x 768px 9.7 (4:3 aspect ratio) and
2048px x 1536px 9.7 (4:3 aspect ratio)
Now my problem is, that I dont really know how to name the folder to only target the specific tablets without tempering with the other device who get targeted by layout-sw600dp and layout-sw720dp.
I tried those various dp calculators I found online but they all dont seem to take the 4:3 ratio into account.
To make matters worse, I don't have those devices at hand, so testing is also problematic, has anyone succesfully created such a device with the emulator? If yes, could you also give me a hand on the right parameters?
Thanks for the help in advance.
UPDATE:
I think the answer is to rename layout-sw720dp to layout-sw800dp and create a new folder layout-sw768dp for the 9.7 tablets. so far in the preview it looks good and 10 inch tablets also choose the layout-sw800dp folder then again, i dont have any real 9.7 inch devices to test.
I also tried layout-w768dp-h1024dp but this always got ignored. Also layout-h1024 didnt work. Any clues why?
my uses-sdk in the manifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="22"
/>
I've also faced same issue and it took 2 days to find out the solution.
Most of 10 and 10.1 inch tablet are 1280dp width and 9.7 inch 4:3 aspect ratio tablet is of 1024dp width. If you want to support 9.7 inch tablet along with 10,10.1, and if you've any views that you want to customize it separately for 9.7 inch tablet. Then do the following thing.
Add two more folders layout-w1024dp (for 9.7 inch tablet) and layout-w1280dp (for 10& 10.1 inch tablet) and add your corresponding layouts to these folders.
You can can calculate the width of the tablet in dp using following code
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpWidth = outMetrics.widthPixels / density;
Log.d("Display", "Width in Pixel: " + outMetrics.widthPixels + " | " + "Width in DP: " + dpWidth + " | " + "Device density :" + density);

text and sp / dp, it just does not work nicely

I am testing my app on a Nexus 5 (full HD screen) and an HTC One X (720p).
Using sp as a good Android developer, actually gives me a result I do not want!
The text on the Nexus 5 is bigger than on the HTC One X, even taking into account that the screen is bigger in size.
When I use 'dp' instead of 'sp', they are actually more alike...the result I want...
But you want to use sp, as that is the way you should do it.
So I created a values-sw380dp directory and put there a different value. But, the Nexus 5 does NOT use these values! Then I used this code:
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
Surprisingly: for both phones, the dpWidth == 360! So I have no way to do this nicely for 720p and 1080p screen ?
Does anyone have an idea or solution?
I am tempted to use DP...

Differentiate tablet with phone in manifest

Is it possible to differentiate tablet with a phone in manifest: I would like to say if tablet open tablet.class Java file or else open phone.class as the main screen.
I know I can do a setcontentview according to the tablet or phone but the problem is that the layout names and other things are different in both phone and tablet.
For example:
TabletActivity.xml
Layout 1 and Layout 2 with two buttons.
But for PhoneActivity.xml
Layout 1, Layout 2, Layout 3, Layout 4 with 4 buttons.
So If I use same JAVA file - I will end up having long code with conditions - instead if I could differentiate this in the beginning like If tablet just open tablet.class if not open phone.class - It would be easier for me?
AGAIN: I am able to differentiate tablet with phone in my mainactivity.java file. I try ing to know will I be able to do it in manifest reason because I have 20 different theme so I have 20 different xml file for tablet and phone and one java file (right now, Oncreate of my mainactivity - before setcontent I am checking if phone or tablet and then theme value and setting the content accordingly). I am wondering is there any easier way than this method???
Or shall I use two different APK's that works only for phone or tablet?
Is this possible?
Let me know!
Thanks!
Try this code. You can get the screen inches
String inputSystem;
inputSystem = android.os.Build.ID;
Log.d("hai",inputSystem);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Log.d("hai",width+"");
Log.d("hai",height+"");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(width/dm.xdpi,2);
double y = Math.pow(height/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("hai","Screen inches : " + screenInches+"");
after that you just decide what is tablet and what is phone by screenInches
Screen inches alone won't do it. Lots of devices are in the "muddy" middle ground and could be called "phablet".
We use layouts to set our device. We use res/layout-sw600dp and res/layout-sw720dp folders. they correspond to devices of the smallest width of 600 or 720 display pixels which makes a nice determinant for 7" or 10" tablet sizes
You will be better served by following the Android guidelines for determining the device size with this link
http://developer.android.com/guide/practices/screens-distribution.html

Resolution trouble

I have a device with resolution 800*480.
I create "Hello word" application. Then I get DisplayMetrics in onCreate of MainActivity.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Log.d("Resolution", "resolution: " + metrics.widthPixels + " x " + metrics.heightPixels);
In DisplayMetrics there are 533*320 pixels. Why?
How can I make application for resolution 800*480 pixels.
It's because you are reading device independent pixels not real pixels. These dip are equal to mdpi resolution. So despite real screen size, you should check what density it is (mdpi or hdpi or xdpi) and then put your hi-res assets in -hdpi suffixed folder (i.e. drawable-hdpi or layout-hdpi). Usually you set your layout in dp (so one layout XML file per activity usually suffices, and you should put them as usual in generic res/layout folder). and then put hi res drawables to make app look nicer on hdpi device
See this article for pixel-size and density relations...

Can I change device DPI programmatically?

I was wondering if I can change device which decides which layout it should choose or which graphic it should choose/scale. This change should only affect my application of course. Can I set somehow device DPI?
The reason why I'm doing that is this phone: http://www.gsmarena.com/samsung_p1000_galaxy_tab-3370.php it has very bad DPI od 240DPI while it's only 170PPI and should be recognized as 160DPI device.
I have quite big graphic for layout-large which looks great on mdpi, but on hdpi they doesn't fit, they are just to big.
Maybe you have another solution than changing device DPI?
Yes it is possible.
The following will set the dpi to 240. Although you have to refresh the activity somehow e.g. restart application.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.density = 1.5f;
metrics.densityDpi = 240;
metrics.heightPixels = 1104;
metrics.widthPixels = 1920;
metrics.scaledDensity = 1.5f;
metrics.xdpi = 254.0f;
metrics.ydpi = 254.0f;
getResources().getDisplayMetrics().setTo(metrics);
you should create your application
to support with multiple screens.take a look at documentation
and hope these links helps you
Designing for Multiple Screens
Working with Multiple Android Screens
Samsung Galaxy Tab GT-P1000 take layout from layout-xhdpi if this not there then it will look for large.
Try to support multiples screen resolution for your app based on that documentation
you have to Design alternative layouts and drawables for your app

Categories

Resources