I have developed an application for Android Smartphones (including 7 inch tablets and Notes) , i forced the application to be run in vertical (by AndroidManifest.Xml)
Now i want to develop the same application for tablets ( specially for 10.1 inches) :
it will be the same APK , or its better to Create another APK ?
Is there anyway to find if device is tablet or not? (size of screen or something)
if it will be same APK , how can i force the app to stick on Vertical for Smartphones and stick on Horizontal for Tablets? (
Meaning if app is running on tablets it will never rotate to
vertical and vice versa for smartphones )
how can i define layouts for horizontal ?!
if anyone got special experience on doing this please share it with me
I would like to give you some suggestions.
it will be the same APK , or its better to Create another APK ?
It should be a single app.
if it will be same APK , how can i force the app to stick on Vertical
for Smartphones and stick on Horizontal for Tablets? ( Meaning if app
is running on tablets it will never rotate to vertical and vice versa
for smartphones )
For layout issues concider this. which should give you some basic idea about how to acheive it.
how can i define layouts for horizontal ?!
For horizontal layouts you can define a seperate layout. You check for the screen size or whatever stuffs which proves for horizontal layout and set the layout.
if anyone got special experience on doing this please share it with me
I have followed it by using the screen sizes and I have used the layout depending on the size. Have a look at this explains somethings to you.
Edit :
Same applies for the vertical layout as well. That is have a vertical layout defined.
For Horizontal use dp. Know about it with above first link. There you can have it like below.
Density-independent pixel (dp) A virtual pixel unit that you should
use when defining UI layout, to express layout dimensions or position
in a density-independent way. The density-independent pixel is
equivalent to one physical pixel on a 160 dpi screen, which is the
baseline density assumed by the system for a "medium" density screen.
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
The conversion of dp units to screen pixels is simple: px = dp * (dpi
/ 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical
pixels. You should always use dp units when defining your
application's UI, to ensure proper display of your UI on screens with
different densities.
Related
I am new to android and looking for the clearity . I am trying to understand how does the components are placed inside the layout . For eg : if I have a relative layout and I am trying to place a textview inside it and I need to place it with the margin-top of 10px or 10dp if it will show at different positions in different devices (because of the phones of different screen sizes) if so how it can be generic so that margin-top will remain same on all the devices ??
Thanks
You gotta go with dp for safeness sake. ixel population may vary across devices. So using pixels is surely not safe. But dp(density-independent pixels) is not specific. It takes advantage of the pixel density and adjusts itself across various devices.
One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density). Android translates this value to the appropriate number of real pixels for each other density.
Official explanation here
I am in android development for last 6 months, recently got a design from a client. I implemented the design and he said this is not exactly look like what the designer has done. Then again he gave me the design with following specifications
my question is - is this right to add the spacing as he has given will this be proper in all devices else how can I explain him what he says is wrong
Please correct me am I wrong or he is wrong
As other users told you, px is not a relative dimension. As an example, we suppose a mobile phone with 5x10 cm screen dimensions, the model has hdpi resolution (480x854 px). For this device, a horizontal margin of 100px takes up more than 20% of the screen width. But, we also have another phone with the same screen dimensions (5x10 cm), but this one is more expensive and it has higher resolution (xxhdpi -> 1080x1920 px). For this case, a horizontal margin of 100 px will not take up more than 10% of the screen width. Therefore, it is needed a relative dimension as dp.
A possible way to deal with your designer could be decide a device to do the designs (a common one). This is a useful link with some devices and their configurations: https://material.io/tools/devices/ . I could recommend you to design using a 720x1080 px (xhdpi) one as MotoG or Nexus 4. For this case, you only need to divide by 2 the px value that he provides you to get each dp value.
You should take in account 3 Things:
Px - dp proportions:
Read this answer : https://stackoverflow.com/a/2025541/5884956 (actually the whole post is useful)
Percentage doesn't work for you:
What you improve by using dp instead of px is that for screens with same
aspect ratio and different pixel density, the layout looks exactly the same (dp-density independent pixel). It means everything inside the screen will look with the same proportions (buttons, imageviews, margins... etc).
Then, you would imagine all android phones have the same aspect ratio (16:9 for example). Which means that for every 16 dp of high, they have 9 dp of width , or in other words if you divide the height of the phone between the width of the phone will give you the same solution as 16/8.
But this is way far from the reality. The most common one is 16:9 but some phones like "Samsung Galaxy S8" have 18,5:9 and there are a lot more aspect ratios: https://material.io/tools/devices/. If you work with %, the time you see your design in a different aspect ratio, your items will look stretched. And this is not the desired.
Size does matter
By this time you should be convinced that dp is the best solution, if not, someone pease improve my answer. Buuuuuut, as the screen size vary, the number of dp also does. So for a phone of bigger dimensions (even for same aspect ratio) we have more dp. Then you should work knowing that some parts are going to be stretched (it should be the spaces between items and margins).
Ideally (Android Studio should do all this work for you) working with same aspect ratio , % of dp should be the mesure and when changing the aspect ratio
this changes must be reflected in spaces between views. But this is a hard work.
TL;DR
Select a standard device that designers are going to use to design (use a common one).
Tell them the design is not going to look exactly the same for every device (unless you design a different layout for each).
They need to decide which parts of the design are going to be the
stretched (stretch spaces and margins please, not buttons or images).
Let them work with px, they may feel more comfortable (meanwhile you must convert it to dp and work with dp). Also if you choose an 'mdpi' device dp = px.
Look at the layouts that allows you work better with this kind of the
designs ( I feel comfortable with ConstraintLayouts, but by working you will notice that this doesn't fit for everything ), but this is up to you.
Extra Information
https://developer.android.com/training/multiscreen/screensizes
I've developed an interface and everything has used dp settings so I thought it should have been good for most screen sizes. However when testing it gets messed up on most screen sizes, bigger and smaller. I get the icons not showing right since I only have one set for Nexus 4 size and density as they are only placeholders until I build the proper icons at the proper scales.
What is going wrong? Is dp the wrong way to go about designing it for scale? I've considered designing specific layouts for various screen sizes but I'm not sure where to begin in regards to what to target.
Here is what is should look like. (Nexus 4)
Smaller Screen (Nexus One)
Bigger Screen. (Nexus 5) - See how the text collides with the vertical divider.
dp - an abstract unit that is based on the physical density of the screen. which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use.
It doesn't mean that image can scale automatically. It works in the specific density devices well.
Generate ur icons with http://romannurik.github.io/AndroidAssetStudio/icons-generic.html and place the icons and check.
For text don't use dp : instead use sp
Android developers site,and most of you,Push everybody to use dp units on their layouts,
I can understand this approach if you using different layout for different densities,but when my requires is one layout.xml ,and one drawable folder,it is make no sense to me to use it,
because it is NOT preserve proportion.
It seems weird to me that there is no simple way to preserve proportion for all devices.
Just help you to understand my point.
1.make any layout with view inside it
2.set the sizes on dp as Android advise you to do
3.and then check it on the eclipse graphical view of your XML.
It looks way way different between different size devices.
Button with width of 15 dp look like 10 times bigger in 640*480 devices then Tablets.
So what's the point?
I understand all the math of conversion between dp and pixel with density,
but i looking for simple way to preserver the proportion on my layout,without define others for others densities.
The only way that i find to help is using android:weightsum,
But it can not use for margins and other settings.
Any idea?
You can always use your values folder and implements the exact dp size for each of different device densities, so you wont create different layout on different density.
Tablet and handheld devices will never have the same sizes that is where the different layout folders are created to let the developers design on different devices.
I would recommend a different layout design on tablet and handheld devices which most apps applies.
Think of dp units as referring to actual size (like cm and inches). So something that is 250dp will look (for example) about 1 inch on a phone, but also roughly 1 inch on a tablet. No matter the size and resolution of the device, it will always be about 1 inch.
Why is it done this way? Well basically it is the Android creative vision that you won't simply scale your app from a handheld screen to a tablet sized screen. This is why they don't allow you to specify heights and widths as % of screen size. The one small exception is layout_weight in LinearLayout.
You may agree or disagree with their vision, but that's the reality.
I created a simple Android app. It only has one activity and one view.
The app was designed for phones. I used the "device independent pixel" unit (dp) for all of my UI element measurements, and RelativeLayout for most of my layouts. The app looks good on a small screen.
On a large screen (such as a tablet), all the elements are tiny, spread apart, and there is a large empty space at the bottom. It's true that the physical size of the UI elements is roughly the same between the tablet and the phone, which is what the dp unit promises, but it's not really what I want for this app. I'd like the whole interface to just get bigger and fill the screen as much as possible, like when you run an iPhone app on an iPad and push the "2x" button.
What's the best way to do this? Do I have to write separate views for each screen size? Should I use pixels (px) instead of dp?
Thanks.
Using px as a measurement would actually be worse - as new tablets have high DPI screens, meaning 100px is going to be visually smaller on a tablet than it would on a mid spec phone.
Your best option is to use layout overrides (layout-sw600dp, layout-sw700dp). Although simply enlarging a phone layout is strictly discouraged.. if you want to take the shortcut all you have to do is copy your phone layout into the sw* folders and tweak all the dp measurements.
You will also need to force the tablet into portrait mode using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);