I have an LinearLayout in my xml file for my UI.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="170dp"
android:gravity="center_vertical|center_horizontal">
<ImageView
android:layout_width="fill_parent"
android:layout_height="70dp"
android:gravity="center_horizontal|center_vertical"
android:src="#drawable/menu_profile_icon" />
</LinearLayout>
I specified the height of Layout in dp. My GUI is different in different emulators . It is good in Nexus 5 emulator but in Nexus S emulator it is not correct . My LinearLayout is out of the screen in Nexus S emulator . What is the solution for it ?
I tried using sp also, but no use,
Thank You.
The android system will choice the right dimens.xml from the "values" folder according the device characteristics.
Some Characteristics:
Screen Density
Screen Size
Screen Orientation
Example:
Device A is a HDPI with a LARGE SCREEN.
The folder name should be:
values-large-hdpi
You have to build the dimens with specific values for each set.
Like the set of Devices with a large screen and XHDPI.
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/guide/topics/resources/available-resources.html
Related
I know the Internet is overwhelmed with questions about DPI px inches and so on.
But after several hours of googling my situation doesnt seem to happen to anyone else!
I have 2 devices custom build with android studio which are both mdpi.
BUT one device is 3.65inch and the other device is an 10.1 inch.
I have created a folder with 2 images 250x125 with the dpi set to 160 dpi
If normally I would declare my 2 images in my XML with dp units instead of pixels...I would suppose on both screens the result should be the same right ?
Well it seems the images keep remaining the same size and don't look # how many inch the device is
So to set things clear:
What do I have to change at my resources or my code so that my layout scales identical for different Inch sizes ?
This is my GOOD layout for my mdpi 10.1 tablet :
This is my BAD layout for my mdpi 3.65 device
How can I make it so that even on the 3.65 inch screen the buttons will scale to the same PROPORTIONS as the 10.1. Not the inches...not the pixels...the proportions....
This is my XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/buttonEnglish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/english"
android:layout_marginBottom="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="2sp"
android:layout_marginTop="0sp" />
<Button
android:id="#+id/buttonNederlands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/nederlands"
android:layout_marginBottom="5sp"
android:layout_marginLeft="20sp"
android:layout_marginRight="5sp"
android:layout_marginTop="0sp"
/>
</LinearLayout>
I'm desperate...
Thanx in advance
This might help explain the problem you are facing...
You have an image that is 250x125 - that is 250 pixels wide by 125 pixels in height.
You have specified 160 dpi - which means that 1 inch = 160 pixels.
So, both screens are doing what you ask and displaying the 250 pixels across 1.5625 inches. On the large screen it looks "proportionally" correct. On the 3.65" screen the button takes up more than half the screen - just like you asked it to.
If you want the smaller screen to look like the larger screen, then you have three options:
adjust the size of the image and provide 2 image assets (or more for a wider variety of screens). This is why you can have resource folders for mdpi, hdpi, xhdpi, etc. You adjust the pixels in the image to accommodate the screen size.
You use a "weighted" LinearLayout that adjusts the size of the space provided based on the available screen space. For this type of layout you should not worry about performance.
Do runtime scaling of the image based on screen size - use DisplayMetrics to get the size and density of the screen and adjust your image to fit the screen appropriately.
The last option is the most flexible in many ways, because if you end up with a screen that is either very large or very small, you can make adjustments to do things like move buttons or text to another place on the screen (above or below other content). But for your specific problem, any of them will suffice.
There is no need of Designing two xml layout.
You can use Dimension for margin and padding according to device.
You are giving static value for margin.
Use dimen.xml in value folder each device.
Following code in your layout.xml will work for you.
android:layout_marginLeft="#dimen/margin_button"
Value folder name for dimen.xml:
values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
values-sw600dp
create dimen.xml in each values folder.
and in dimen.xml you have to define value for margin in all values folder but value of that property is different according to device size like this:
values-mdpi
<dimen name="margin_button">20dp</dimen>
values-hdpi
<dimen name="margin_button">23dp</dimen>
like wise in all values folders.
Thanx everyone for the answers. Due to answer from #Iacs I discovered that I had to made changes to my folder structure.
I have completely overlooked the fact that in the /res folder there can be more directories then just the standard "layout" directory. You can create other directories with these names : layout-large, layout-xlarge, layout-small, and so on...
In these folders you can paste your layout.xml and adjust the values...
This is how things look now in my android studio
note the layout folder structure:
And now ofcourse my 2 devices with both the same DPI but different screen size are showing my buttons the way I want them to be showned!
I have four dimens.xml files in my values folder and I have different values for different dimens.xml files.
for example, in dimens.xml(hdpi) u_video_width is 306dp and dimens.xml(xhdpi)u_video_width is 404dp.but this values not working, because when i run my app in Nexus S (480X800) hdpi model genymotion
This is what my View looks like:
[![This is what my View looks like][1]][1]
As I said in hdpi dimens folder, I have 302dp but I have different width when I run my app (in XML activity file)
this is a my XML code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D53362">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<VideoView
android:layout_width="#dimen/u_video_width"
android:layout_height="#dimen/u_video_height"
android:id="#+id/video_view"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This is a my dimens xml files
Nexus S has 480X800 resolution and i want to my VideoView to have 302dp in hdpi resolution and another resolution--another dpi how I can solve my problem?
To make same dimension appear the same on higher dpi devices android multiply these dimension with a constant ie
for mdpi it is 1x
hdpi 1.5x
xhdpi 2x
xxhdpi 3x
So in your case it is 306 * 1.5 = 459px
reset of the space (14dp ~35 px) is available on both sides of VideoView
Please refer android developer page
At least this is what I understood. Hope it helps you.
As far as I've understood, it seems it's working right. Your screen is 480px wide and your VideoView is 306dp(in a hdpi screen ~453px), and as it's centered, it remains a bit of margin.
If you want the same dp in another resolution, just remove u_video_width from the other files.
I am currently trying to adjust my Android App so it will look and feel similar on multiple screens/devices.
I know by now that a major part in this is to provide multiple image sizes for every image file according to the x1, x1.5, x2, x3, x4 ratios for mdpi, hdpi, xhpi, xxhdpi and xxxhdpi respectively, and I have finished doing so today.
After doing this, I have defined Density independent Pixel dimensions in the #dimen.xml values resource that correspond with the actual image sizes in pixels of the MDPI resources. Subsequently, i have set the imageviews in question's layout_width and layout_height to this dimension.
I am currently at a loss, however, as to why my application still looks significantly different on an MDPI emulator than it does on an HDPI emulator. To highlight what I mean, I'll provide the following screenshot showing the HDPI and MPDI emulator next to one another (left is HDPI (4" WVGA Nexus S) and right is MDPI (5.4" FWVGA)). I want both of them to look like the MPDI version, but what I've done so far apparently isn't working.
I have three theories of my own as to why this is not working the way I intend it to:
1. I am not supposed to set ImageView layout_width and layout_height to a dp value, but rather match_parent or wrap_content (?) (and change the structure of my .xml layouts in the process).
2. I am not only supposed to define multiple drawable resources, but also multiple layout resources for different screen sizes (?).
3. I have misunderstood the entire idea behind how this is supposed to work (?).
I will also give you an example of one of the list items that can be seen in the first screenshot (#drawable/phone_speed_icon is a 64 x64 pixel resource in MPDI and a 96x96 resource in HDPI, and #dimen/icon_attribute_size is 64dp):
<LinearLayout
android:id="#+id/llSpeed_PreSession"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingEnd="20dp"
android:paddingStart="20dp"
android:weightSum="100">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/icon_attribute_size"
android:layout_weight="20"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="70"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="#dimen/icon_attribute_size"
android:layout_height="#dimen/icon_attribute_size"
android:src="#drawable/phone_speed_icon" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="30"
android:gravity="center_vertical"
android:paddingStart="10dp"
android:text="Speed"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#878787"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:gravity="center">
<android.support.v7.widget.SwitchCompat
android:id="#+id/swSpeed_PreSession"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
In the end, I have four questions that I'd like answered:
1. How do I make the list items in screenshot 1 look the same on MDPI devices as HDPI devices? Does this have anything to do with one of the three theories I mentioned earlier?
2. Why is the header text ("What do you want to measure?") wrapped on one device, but not on the other? They use the sp unit (via android:style/TextApperance.TextApperance.Large)?
3. Shouldn't everything be more spaced out on an HDPI device (if anything) rather than less spaced out? The HDPI emulator looks as if it "has got way less pixels available", if you can understand what I'm saying even a little.
4. How do I make the Fragments on the second screenshot look the same? Or should i not even want this, because the HDPI is (for some reason) physically smaller, which is why the layout is less spread out?
Anyway, I have been at this all day and the more I read the more thouroughly confused I get, so any help is greatly appreciated!
You have the option to create multiple dimens.xml files depending on a variety of factors. Here you'll see what my current project looks like with the various dimens.xml files in Android Studio based on screen width.
However, you can have different requirements for each dimens file you want. For example, you can create dimens files for each density:
The attached layout which works correctly for a phone (Samsung Infuse), but it does not show
layout correctly when the app is uploaded to a tablet (Nexus 7). The button sizing is incorrect. How can I fix this?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/"
style="#string/menu_settings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/gecko25"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:textAlignment="center"
android:visibility="visible" >
<Button
android:id="#+id/link1_btn"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:minHeight="58dip"
android:minWidth="98dip"
android:text="About"
android:textStyle="bold" />
<Button
android:id="#+id/link2_btn"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:minHeight="58dip"
android:minWidth="98dip"
android:text="Experience"
android:textStyle="bold" />
<Button
android:id="#+id/link3_btn"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:minHeight="58dip"
android:minWidth="98dp"
android:selectAllOnFocus="false"
android:text="Bulacards"
android:textStyle="bold" />
</LinearLayout>
Make different layouts for different densities. ie, place the same xml file in layout-hdpi,layout-xhdpi,layout-tvdpi
To create those folders in res/layout follow these steps:
Right click on layout folder --> new-->other-->Andorid xml layout-->next-->give the filename(must be same in all densities)-->AvailableQulaifiers tab select density--> move it to chosen qualifiers with the help of arrow in between--> there select for what density (medium high tv)--->finish
Like for example:
Nexus S and Nexus one ---> hdpi
Galaxy Nexus ----> xhdpi
Nexus 7 ---> tvdpi
and give heights and widths for the Button.. best practice is to set them to wrapcontent , if for your scenario it requires height follow the above steps. and give height and width such that different for phone and tablets which comes under different densities
It's not recommended that you use tvdpi for Nexus 7 screens.
See this:
http://android-developers.blogspot.com/2012/07/getting-your-app-ready-for-jelly-bean.html
Probably the best way to detect 7" tablets (like the Nexus 7) is to use the qualifier:
layout-sw600dp
And the qualifier for 10" tablets:
layout-sw720dp
You might want to try specifying the button sizes in sp rather than dp as they may need to bigger or smaller if the user changes the default font size but wrap_content and/or use weights are usually best.
I am reading through http://developer.android.com/guide/practices/screens_support.html and trying to understand how pre-scaling works.
I have the following layout file which is optimized for the galaxy tab :
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayoutmain"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/mainmenu"
>
<ImageButton
android:id="#+id/start_button"
android:layout_marginLeft="150dip"
android:layout_marginTop ="70dip"
android:layout_width="500dip"
android:layout_height="214dip"
android:background="#drawable/startsession"
android:contentDescription="#string/descbuttonstart"
/>
I have placed the images for the screen in the drawable-ldpi folder. The problem is that when I test the app on a smaller screen (using the emulator) the screen does not scale properly - the button is massive and not positioned correctly.
Is it possible to just have one layout file for all screens and if so what is the secret to getting this working ?
Thank you.
NO , AFAIK its not possible you need to have three differents folders(hdpi,mdpi,ldpi) with different dimensions for buttons,images etc
and also for layout again you need to have three layout folders (layout-small,layout-medium,layout-large)
Screen size and screen resolution are two independent things. the resources in ldpi folder are for low resolution and not necessory small screen sizes. The way I prefer is to define resources for high resolution and let them scale down on others.