guys... i have developed my first app... i have tested this, in Galaxy SIII, it's work fine... but if i run my application in a Galaxy SII the TextView is big...
XML layout:
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv4"
android:layout_alignBottom="#+id/tv4"
android:layout_alignLeft="#+id/tv2"
android:layout_marginLeft="23dp"
android:text="#string/my"
android:textSize="15dp" />
How do I adjust the resolution automatically with all the screens?
What is the easiest choice?
I'm confised :|
Thank you!!!
Try using sp units instead of dp units for the text, as follows:
android:textSize="15sp"
From the Documentation:
A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An sp is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).
Also, you can find the differences between these units on Difference between px, dp, dip and sp in Android?
Related
I am relatively new to android. I want to develop an UI which works fine on 2 hpdi devices.
For example lets say on these two devices
Nexus 9
Size 8.86"
Resolution 2560x1600
Density xhdpi
Nexus 10
Size 10.05"
Resolution 2560x1600
Density xhdpi
As seen above both have different screen size and different resolution but density is the same.
My understanding is one layout should work perfectly on both devices, but unfortunately that is not the case. I see differences. Can someone please suggest what changes i should make.
Adding below the portion of the code(xml) which is giving me a problem.
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2" >
<customSeekBar
android:id="#+id/abc1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_marginTop="40dp"
android:thumb="#drawable/eqthumb"
android:progressDrawable="#drawable/progress_eq"
android:max="19"
android:progress="10" />
<TextView
android:id="#+id/abc2 "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/ abc1"
android:textColor="#FFFFFFFF"
android:layout_marginTop="-10dp"
android:textStyle="bold"
android:text="300"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/abc3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ abc1"
android:text="0 dB"
android:textColor="#FFFFFFFF"
android:textStyle="bold"
android:singleLine="true"
android:layout_marginTop="-5dp"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Thank you.
Density is only one part of your layouts though. These devices have different aspect ratios so available device independent pixel (dip or just dp) differs.
So your available width and height differs, but sure logical screen density is the same.
Having said that, android's layouts are rather flexible/adaptable and regardless of screen density. Your layout might need to compensate for different widths or heights though. Just as it might want/need to change due to portrait or landscape mode, different aspect ratios (a potential issue with these two devices) has the same difference.
So you are incorrect in assuming that your layout differs a lot due to hdpi/xhdpi/xxhdpi etc. You might want to qualify your layouts on available screen width or similar (sw720dp for example) instead if needed.
Nexus 9 has a logical screen density of: 1024 x 768 dip
Nexus 10 has a logical screen density of: 1280 x 800 dip
Having said that, your particular layout issue needs your specific xml layouts to be explained. For many layouts those two devices should be able to look the same.
Note: Your device data is off. Nexus 9 is 2048 x 1536 pixels and has an aspect ratio that's 4:3 mind you, compared to 16:10 for the Nexus 10. Both fall in the xhdpi bucket though for resource qualifiers etc. Physical screen density is 288 for the Nexus 9 and 300 for Nexus 10.
As seen above both have different screen size and different resolution but density is the same. My understanding is one layout should work perfectly on both devices
That's not true, you are building on top of this wrong premise. Simply put:
Devices with different screen sizes, but same density, will always work with the same layout
False: different screen sizes (in inches) mean you probably will have to adjust your views. So, provide different layouts, or at least different dimensions.
Devices with different densities, but similar screen sizes, should always work with the same layout
True: if the screen size is the same, a 40dp button will cover the same portion of screen in both devices. That's because dps are density independent pixels, and allow us to work on all densities out there with a single value.
I suggest you read this official resource, that should clear your mind. In the very first bullet list, we read:
Provide different layouts for different screen sizes
Provide different bitmap drawables for different screen densities
As you can see, there's no Provide different layouts for different screen densities, which was the premise of your question. That's just something you don't do usually, it makes no sense if you use (and understand) dps.
UI designer drawn this design for Android application:
Size is 1080x1920 px. This is same to Nexus 5 display which has xxhdpi density. In this density 1dp = 3px.
I have this letter on my design JPEG image. Its height - 45px.
So I calculated my TextView size 45px/3=15sp
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Large Text"
android:textColor="#5B5C5D"
android:singleLine="true"
android:id="#+id/tvFolder" />
But when I am running my app on Galaxy Nexus - 720x1280px. Density - xhdpi. 1dp = 2px. I am getting this result - my letter 'd' height is 24px which is equal to 12dp.
Of course, I can recalculate (by multiplying to 1.25) and make TextView 19dp. But I want to understand why this is happening and can we solve this. Maybe I am confused and making a mistake. So my question, what is wrong with sp?
Did you check in your device settings that the font scale is set to 1?
If you want to define your font size in device specific pixel, you have to use dp, if you want the user to be able to scale according to his wishes, you use sp.
In Html we could use em, and it would adjust the size and format of everything to the appropriate size despite Resolution. Does android have a unit of measurement that adjusts views to its appropriate sizes and formats despite the different screen resolutions?
Android uses scalable points or SP
here is a sample
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text - 26sp"
android:textSize="26sp" />
Reference
You normally use dp for View sizes.
And sp for font sizes.
you can normally use dp,sp, for ref
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
This one is bedeviling me. What am I overlooking? I want a button exactly one inch wide, on any density screen. I declare it in the layout like this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="160dip"
android:text="How wide?"
/>
What I get is a button that is exactly 7/8ths of an inch wide on all screens, not the expected 1 inch. Is the difference maybe the margin or padding around the button? If so, how to set that to zero?
What about this ?
android:layout_width="1in"
dip are based on virtual densities, not actual physical densities. For instance, on a T-Mobile G1, the device's display physical density is around 180dpi, but we treat it as a 160dpi display. Android currently uses 3 "density buckets," 120, 160 and 240. Each device uses whatever is most appropriate. If you want accurate physical measurements, use physical units like in, mm, etc.