android different dimens not working - android

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.

Related

Same DPI but different inch size

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!

How to add more dimens.xml files in android?

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

Trouble grasping the dp unit in Android

So, I have googled a lot, but still cant quiet grasp the concept. I have 3 folders : xhdpi, lhdpi and mhdpi. I do understand the conversion and downscaling to different dip and screen densities. Android selects resources for the right screen type when the application runs.
But, how do I start? I made a background for my application in Photoshop. The background was defined in 720x1080px and exported as an .png file. I put the .png in the xhdpi folder. Everything worked out fine on my Sony Xperia Z, but when a friend loaded it on his Galaxy 3 the background was "to-small" and did not fit his screen. I assume this is because the bacground was to small.
But how large do I need to make it? What px-sizes should xhdpi drawable resources be to fit every single xhdpi screen?
Would a good approach be to start with the largest size, xhdpi? And then convert the drawables down to mdpi and ldpi later on? If so, I need a starter size in px so I can create the background for the application in Photoshop.
Here is how I use the background : (Note, i changed my height to fill_parent it fixed the "not filling the whole screen problem")
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_xhdpi" >
</RelativeLayout>
To make PX sizes scale appropriately you will need to scale using the following ratios:
2X for XDPI
1.5X for HDPI
1X for MDPI (baseline
.75X for LDPI
When you are specifying sizes in your code (or XML), use the DP parameter to specify the value (or SP if you are specifying fonts).
for example: android:layout_marginLeft="12dp"
Here is the docs: http://developer.android.com/guide/practices/screens_support.html, you will notice this image, and a lot of other details describing this.
You can find in this table a briefing of different devices screen densities and resolutions; according to it, Galaxy SIII is xhdpi with a resolution of 1280 X 720. Besides, you could want considerate to follow compatibility mode guide to avoid rendering issues in multiple screens.

Android screen size resolution

My app works perfectly in resolution 480x800. If I keep the 480x800 resolution but change the screen size to for example 2.7 inches, 3.7 inches or 5.4 inches it still is perfect. But when I change the resolution to for example 640x1066 all the ImageButtons is too small and in the wrong place in all screen sizes... I have created ImageButtons in all four folders(drawable-l, m, h, xh) but still the buttons is not in the correct size..
<ImageButton
android:id="#+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="433dp"
android:background="#drawable/imagebutton1" />
<ImageButton
android:id="#+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="188dp"
android:layout_marginTop="436dp"
android:background="#drawable/imagebutton2" />
NOTE: My problem is not when I change screen size, but when I change screen resolution.
I would really like to see some code.
If you use RelativeLayout with attributes like android:layout_width and android:layout_height and specify values in dp (density pixels) e.g. android:layout_height="40dp" it should work just fine.
Please try to share some code with us.
Make sure that your assets that you have in your ldpi, mdpi, hdpi and xhdpi folders are scaled correctly to accommodate the diff. resolutions. I accidentally scaled some assets incorrectly, and although they were in the correct folders..gave me some bad results
http://developer.android.com/guide/practices/screens_support.html

Android screen size and resolution [duplicate]

My app works perfectly in resolution 480x800. If I keep the 480x800 resolution but change the screen size to for example 2.7 inches, 3.7 inches or 5.4 inches it still is perfect. But when I change the resolution to for example 640x1066 all the ImageButtons is too small and in the wrong place in all screen sizes... I have created ImageButtons in all four folders(drawable-l, m, h, xh) but still the buttons is not in the correct size..
<ImageButton
android:id="#+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="433dp"
android:background="#drawable/imagebutton1" />
<ImageButton
android:id="#+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="188dp"
android:layout_marginTop="436dp"
android:background="#drawable/imagebutton2" />
NOTE: My problem is not when I change screen size, but when I change screen resolution.
I would really like to see some code.
If you use RelativeLayout with attributes like android:layout_width and android:layout_height and specify values in dp (density pixels) e.g. android:layout_height="40dp" it should work just fine.
Please try to share some code with us.
Make sure that your assets that you have in your ldpi, mdpi, hdpi and xhdpi folders are scaled correctly to accommodate the diff. resolutions. I accidentally scaled some assets incorrectly, and although they were in the correct folders..gave me some bad results
http://developer.android.com/guide/practices/screens_support.html

Categories

Resources