I don't know if such devices really exist but, considering that they could, consider the following two devices:
I was trying to adjust size of widgets giving them a certain percentage of width, height etc, and since android devices are categorized as x, xhpi, xxhdpi etc, I thought dpi would be a good way to adjust their size but as you can see above this isn't the case.
An example case is: say if the device is small then I want a menu bar to take half of the screen and if it is large I'd want it to take only one third.
What would be a better way to adjust these widgets?
EDIT: If I use ConstraintLayout some of the buttons would have to be hardcoded with their size, but I want the buttons to get slightly bigger/smaller according to the screen sizes.
When your app app launch Find display's width and height.
Using this code, you can get the runtime display's width & height:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
According to your height and width onCreate method to set component programmatically Like Button, Textview and others.
First, for your requirement of being able to use percentages, you can utilize a ConstraintLayout. I'd recommend the official training documentation to start. ConstraintLayout is relatively new (first release Feb 2017) so there aren't as many third party tutorials that are updated - but there should be enough info out to answer any questions you have to start.
Here's a short example:
<MenuBar
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.5" />
For the second part of your question, you can provide alternate resources with different config qualifiers like xxhdpi like you mentioned above. More information on that here. To store a float value as a dimen in both the device form factors you want you'd declare in dimen.xml in res/values-[config_qualifiers].
<item name="bottom_menu_bar_height_percent" type="dimen" format="float">0.3</item>
Referencing from xml
<MenuBar
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_constraintHeight_percent=
"#dimen/bottom_menu_bar_height_percent" />
Hope this helps :)
Related
I have several buttons in my application that are displayed at the bottom of the screen. Right now the buttons have text in them. When running on the emulator, the buttons with text fit nicely. Now, that I am running on the actual device, some buttons' text takes more than two lines and the screen is not very presentable. I could change the font to make it work for the device in question, but there is no guarantee that it will work on some other device. Should I create button images (with text embedded as part of the image) and then have multiple versions, depending on the size of the device screen being used? That seems like a lot of work, is there a simpler solution to this?
Thank You,
Gary
You need to give equal weights to all buttons.So that all of them look similar and occupy same amount of space.
You have to get screen resolution and set sizes as a proportion of this resolution.
Here is the sample code to obtain screen width and height.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
It is not hard but little bit tricky.
In this purpose you can use built in draw-able folder. In android project there are many draw-able folder like drawable-hdpi, drawable-mdpi, drawable-xhdpi where you can put different size of images and it will automatically render image based on device screen. Check this tutorial for more understanding Supporting Multiple Screens
Or you can take screen size dynamically. Based on the screen size you can set the button height and width.
You can find multiple screen size handling tutorial here:
Supporting Multiple Screens
your emulator may have specific resolution that is different than the one of your actual device.
So I have an Android forms-based app that fills a 7" screen beautifully, portrait only, and there are over 50 forms. Rather than have to redesign each form (or the entire app) to suit each different resolution, my business case solution is to just scale the form to fill the screen. A 10" tablet will look a bit spaced out (but I can add some margins), and on a phone you better have a stylus and good eyesight!
Rather than argue about whether these are good choices, my question is: can it be done? On Windows CE the forms scale with the text size via "dialog box units", which works quite well. Android doesn't have anything like that, unless there are tricky ways of using 'sp' units. Regardless, getting a form to fill a screen nicely on different resolutions looks like hair-pulling-out territory, unless I'm missing something.
In answer to comments, it's a native app defined using XML layouts. For an example, you could take just about any sample app and show how to make it scale to different screen sizes, so you if the screen is small you get tiny text regardless of resolution.
Just to be clear: I don't think this question can be helpfully answered by describing how to use the standard Android way of handling multiple screen sizes: http://developer.android.com/guide/practices/screens_support.html. This is what I don't want to do this time. This really is about how to do other thing -- scaling to fit -- for when you really need it.
I hope I understand your situation well:
If it is a web application (you are using a webview) then use the viewport tag:
<meta name="viewport" content="width=device-width;">
This will fit the page content to the device screen.
Or if it is a native application (also would work for a web application), you can add fill-parent for your layout heigth and width, in your layout XML.
Use this code:
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
To get the height and width of any Android screen, and with this particular dimension, set the proportion for your layouts.
For example,
RelativeLayout fieldMain = (RelativeLayout)findViewById(R.id.field_layout);
fieldMain.setLayoutParams(new RelativeLayout.LayoutParams((Int)(0.8*width), (int) (0.9*height)));
I have a view in my android app that I would like to toggle between visible/gone on smaller screens, and visible/invisible in larger sizes. The initial set up (gone for small, invisible for large screens) is done by having two separate XML layout files under layout and layout-sw600dp-land, but then when I need to dynamically swap the visibility setting, how can I determine from within Java code which one to pick based on screen size?
Edit: more specifically, I want to detect in my code the same condition that causes Android to use layouts from layout-sw600dp-land. I was thinking even recording the value somewhere in the values-sw600dp-land directory, but not sure which file to put it into and how to access it.
You can get the size in pixels of the screen using the following.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
However, your question was ambiguous as to whether size of screen meant pixels or inches. You may need to take advantage of the dm.densityDpi value, to convert the values from pixels to inches, for a more useful calculation of the "size" of the screen.
ANSER FOR EDITS:
There are two potential solutions. One is referenced in this thread, very simple and you alluded to it above.
How to programatically determine which XML layout my Android apps is using?
The second isn't a solution, but rather an explanation. The layout-sw600dp-land file replaces an old naming convention pre 3.2 that went like this layout-xlarge-land. This essentially manes "xlarge" screen in "landscape" mode. So you can detect this programmatically by finding xlarge screen sizes, in which the width > height. Below is a good reference to compare the old convention vs the new "sw600dp" = smallest width is 600 dp convention.
http://developer.android.com/training/multiscreen/screensizes.html
I have searched for a while, to know which screen resolution i should consider before starting to design an android app.
I found these things:
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
Which device resolutions should be kept in mind when developing Android Apps?
but actually these are not what i wanted.
what i wanted to know is should i design my application for each of these resolution or take the most used resolution alone into consideration
or
if i am not using any hardcoded values for widths, heights and margins etc.., i never need to worry about the screen resolutions
or
how good is this - find the device width and height using Display metrics and create all views according to these values Dynamically ?
Should I design my application for each of these resolution or take the most used resolution alone into consideration.
You should make sure your application works correctly on all screens, not just the most popular one. I would begin from the bottom up... first make sure it works correctly on small/normal screens (and in doing so, you make sure it works on all screens). Then you can optimize your layouts for tablets by using multi-pane layouts, etc.
If I am not using any hardcoded values for widths, heights and margins etc., I never need to worry about the screen resolutions.
Not sure what you are trying to say here, but you should always be wary about different screen resolutions by using dp (density independent pixels) instead of px.
Find the device width and height using Display metrics and create all views according to these values dynamically?
This should be a last resort. Most of the time, your layouts won't be that complicated though, and it won't be necessary. A lot of the time you'll use wrap_content and match_parent to set the widths/heights, and often times you'll use RelativeLayouts to space the views relative to one another. You only really need to measure the widths/heights of the views if you find its absolutely necessary.
IMHO you should always code your application such that every device is supported. From the launcher icon which is detailed here (different resolution for different screen size): http://developer.android.com/guide/practices/ui_guidelines/icon_design_launcher.html to the layout of your app which should be designed so that everything is placed relative to the screen size (by using attributes such as match_parent and wrap_content).
You can try to code it such that the views are created dynamically after discovering screen size, but I think its easier and just as effective to do your first idea!
Best option is to get the screen width and height dynamically using simple height and width getting parameters.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
other option is to do create it for device with highest screen width and height and then handle it mathematically for respective screen size.
Well, it is recommended to design for all the sizes. But I know it is a lot of overhead.
So what you could do is design for the largest device you're aiming for and have the devices adjust the images based on their sizes. Do not use hard coded values. Use wrap_content or fill_parent.
And yes, dynamically adjusting the images is a good idea, even though it requires more coding on your side. This is the technique I use.
When I try to run my project on LG Android Mobile then there is no alignment issues come with this device it is a 3.2 HVGA but when I try to run it on Motorola it is a 3.7 WVGA then it gives complete layout alignment issues so can you tell me suggestion to implement layouts uniquely to every device.
I don't know is it possible or not to make a unique layout design for all devices.
You can't create custom layouts for different devices, but you can for different screen densities and sizes, Supporting Multiple Screens has all of the information you should need.
You can create custom layouts by reading the device layout screen like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
There are two ways you can create your layout. You can create your own algorithm that checks the difference between the resolution you have made your layout for, and gets the difference for the current screen size, and adjust all those values progmatically (long and tedious, but DEFINETELY will work on all devices)
Or, you can define a layout for each of the common devices in your layout folder. This requires alot more space and time though.
Sometimes back I was having this type of problem. Because I was using Pixels as my unit to specify height, width or any layout specific dimensions. But I changed those px to dp and it worked for me. I got same layout for all the screens. Hope if this may help you anyways...
Here is a quick checklist about how you can ensure that your application displays properly on different screens:
1.Use wrap_content, fill_parent, or dp units when specifying dimensions in an XML layout file
2.Do not use hard coded pixel values in your application code
3.Do not use AbsoluteLayout (it's deprecated)
4.Supply alternative bitmap drawables for different screen densities