Android Development related to Xml layouts - android

I have made a layout for an app,it runs fine on my phone but when I use a smaller size phone to run it the user interface changes and is not in accordance.For example the buttons go out of screen or their order of placement changes.What should I do?

you have to develop different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens.
For layouts
res/layout/mainactivity.xml // layout for normal screen size ("default")
res/layout-small/mainactivity.xml // layout for small screen size
res/layout-large/mainactivity.xml // layout for large screen size
res/layout-xlarge/mainactivity.xml // layout for extra large screen size
res/layout-xlarge-land/mainactivity.xml // layout for extra large in landscape orientation
For drawables
res/drawable-mdpi/ic_icon.png // bitmap for medium density
res/drawable-hdpi/ic_icon.png // bitmap for high density
res/drawable-xhdpi/ic_icon.png
put following code in the Manifest
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
Kindly look at the following links for more reference
https://developer.android.com/training/multiscreen/screensizes.html -> Diff Screen Size
https://developer.android.com/guide/practices/screens_support.html -> Diff Screen Supports

Few tips for single layout:
Design your views relatively(Check Relative-Layout and Frame-Layout) .
Take the advantage of weight(Check Linear Layout).
Use scroll view if your layout height is big.
For Ex: If you want to put some buttons(for say 2) on the bottom of the screen just use RelativeLayout as parent and inside that LinearLayout with alignparentbotton=true after that you can define buttons, you also can use weight on buttons to equally align horizontally.

This is one of the way to support different screen sizes. You need to create layout folders depending on screen sizes. (like layout-normal, layout-large, etc). Place your xml layout files in each folders. Don't use hard dp/sp values.
Go through
Supporting Multiple Screens for more understanding.

Use weights inside Layouts to adjust the views properly.
It is the most reliable way to do it.

Related

Screen Size Compatibility

I'm starting just starting out in android development. My IDE is Android Studio and I'm currently building my own calculator app. I used Nexus 5X
's screen which is 5.7":
My app using the 5.7 screen
The only problem I encountered is that when I tried to my friend's phone which has a 4.7 screen, it somewhat looks like this:
My app using the 4.7 screen
I thought that it the app would automatically scale everything just like when you're using viewport on web development. I'm a web-developer (html, php, javascript, css) and I'm planning to migrate to android development for ease-of-use. Is there any way to fix this without re-designing everything again guys?
Thanks, and peace out.
Android devices come in a variety of screen sizes and resolutions. That’s why handling the multiple screen size in android is most important.
Look into this: https://stuff.mit.edu/afs/sipb/project/android/docs/guide/practices/screens_support.html
Actual physical size, measured as the screen’s diagonal.For simplicity, Android groups has four generalized sizes: small, normal, large, and extra large.
To set up support for multiple device sizes in Android, add the element into the AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest ..>
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens= "true"
android:anyDensity="true"
/>
<application... >
….
</application>
</manifest>
As we design our UI for different screen sizes, we’ll discover that each design requires a minimum amount of space.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape
We can also define it on the basis of dp like :
res/layout/main_activity.xml // For handsets
res/layout-sw600dp/main_activity.xml // For 7” tablets(600x1024 mdpi).600dp wide and bigger.
res/layout-sw720dp/main_activity.xml // For 10” tablets (720x1280 mdpi).720dp wide and bigger.
Steps to create Different Screen Layouts :
Step 1 : First go to Android to Project mode in Android studio change at below of the project name.
Step 2 : Create Folders for all screen sizes.
Step 3 : create same xml layout on all these folders.
For Screen Compatibility Issues Try to Use Relative Layout & with Scroll View So To Make your app compatible to Tablet & Horizontal Layout.
I had the same issue and I used this library and it did the thing for me.
The thing is you just have to replace sp with ssp in your xml after adding this library in your gradle and you'll be good to go.
Also for width and height I used this library.
Just replace dp scales with sdp.
I've also answered a question like this here aswel, have a look.

How to place items according to their size?

Currently, i develop a android application. In my activity, i would like to place my elements according to their size. i explain : i have 3 LinearLayout in a GridLayout like this :
GridLayout (2,2, horizontal) allow to put the LinearLayouts like this :
I would like to do the same, but no with GridLayout(2,2,horizontal), but just according to the size of the element.
For example, if the screen is large, it's possible to have the 3 LinearLayout on the same line and inversely if the screen is small, juste 1 LinearLayout by line.
I hope I have been clear, Thanks in advance.
This layout would be a "FlowLayout" which doesn't exist in standard.
But there are some open-source implementations of it around :
You should take a look at
https://github.com/ApmeM/android-flowlayout
or
https://github.com/blazsolar/FlowLayout
Try these instead of gridlayout
https://github.com/blazsolar/FlowLayout
https://docs.google.com/file/d/0B_VKZCqEnHblNDdiY2UxODgtYWNhNS00MmU4LWE4NDMtZjQ1OWI5MDMxZTVh/edit?num=50&sort=name&layout=list&pli=1#
Yes, of course you can do.
You need to create different layout directory for different density devices.
For example layout-ldpi, layout-hdpi, etc. and define the required kind of layout inside these.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
And inside the different my_layout.xml file define either one, two or three linearlayouts you need.

Android layouts size and resize

I am doing a lot of work about android layout I still can't create a layout working in every phone. I am not sure about best the way to create a layout so correct me if I am wrong . There are three things to keep in mind :
Screen px (resolution, for example 1080x1920 px)
Screen dimension "inches"
Screen density dp or also called dpi ( dp is a virtual resolution, it's correct?)
To draw a layout working in every phone (my app will works for phones) do I have to create a directory "layout-kindofdensitydpi for every screen density (layout-ldpi,layout-mdpi,layout-hdpi,layout-xhdpi,layout-xxhdpi,layout-xxxhdpi) and draw "manually" or do I have to do something else?
I did a test, I created these 6 directory and drew manually for every resolution. It requires a lot of time, also device with a resolution of 768x1280 my app doesn't respect what I drew, for example spaces aren't respected, the collocation of elements doesn't result correct and frame layout with ImageView inside isn't scaled.
What I have to do? In some Android books isn't mentioned that elements could not resize and usually them explain how to put some text or image without analyse resize in every device.
Thanks in advice
First you should find the appropriate layout type for your UI (RelativeLayout or LinearLayout). Sometimes using a good layout(or nested layouts) can make the UI look good on every screen. I prefer LinearLayout cuz I can simply set layout_weight for components.
Then set different sizes in dimens.xml file for different densities or screen size buckets. Like this:
And you can also use match_parent or wrap_content
Don't forget to set the screen orientation of your activity if it doesn't need to rotate. Having one orientation makes it much easier to design.
If you couldn't make a good layout using tips above you should create multiple layouts to fit every screen size or density (Screen size and density are two different things).
You should find the best way to determine your screens according to.
Screen size bucket (small, normal, large, and xlarge) picks a layout that fits the screen (or the closest), density bucket (ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi) picks a layout according to the density ,or the smallest width (I think it's almost the same thing as density).
480dp is the sw of these two devices
You can make layouts for different screen orientations too.
I don't like creating layouts for different screens for some reason. One of them and the most important is that sometimes same screen sizes have different densities and it makes it hard and time consuming. To create a layout and you should also provide a lot of pictures.
Use
android:layout_width="match_parent"
android:layout_height="match_parent"
to the outer layout to get access of the full screen of the device.
Also, if there are no changes in your UI then you don't need to create different layout folders.
Refer http://developer.android.com/training/multiscreen/screensizes.html
You should have a look at
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/index.html
I would suggest using layout-swXXXdp or layout-large etc instead of layout-KindOfDensitydpi
Create your relative layouts using RelativeLayout and use the weights of LinearLayout. Don't hard code any of the positions in the layout

XML for all screens

I have a layout with 8 buttons 2 image views and 2 text views. On my device (Galaxy S) it fits but on devices with smaller screen the bottom side is not appearing.How could I adapt my layout for all screens?
if you want your app to support all kind of screen sizes,
you need to create different layout xml file for each kind of the screen types.
place each one of the above on resource folder:
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
on run time - android will automatically choose the right resource according to the device's screen type
you can read more about it on -
http://developer.android.com/guide/practices/screens_support.html
use RelativeLayouts or LinearLayouts with layout_weight property. If you can post some xml, i would give you more suggestions.
The problem occurs generally with the ImageViews mostly, as they occupy more space on smaller screen, since smaller screen has lesser no.of pixels per inch. It's a general practice to restrict the sizes in dp.
You can define more layouts with different width and height.Then check the height and width and density of device and use that specific layout for that device.
Use screen dependent pixels dp and sp. Design a different layout for different screen sizes. http://developer.android.com/guide/practices/screens_support.html <-- refer here
For most width and sometimes height problems you can use layout_weight to assign the exact ratio catered for each View.
If there is no other choice and that your layout have to be that way you might want to consider using a ScrollView to wrap your parent layout so that they can be scrollable. http://developer.android.com/reference/android/widget/ScrollView.html <-- refer here.
Good luck.

Creating common layout structures in android?

I am developing an application which can be installed on any android device. Where should I put the following code in manifest file?
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
Also, I have created three folders layout-normal, layout-small and layout-large. Uniformly i maintain LinearLayout in all my layout and different resolution of images in drawable-hdpi, drawable-mdpi, and drawable-ldpi.
The problem is layout is not suitable for some device, because it take different images based on density and i use mostly WRAP_CONTENT for most of component.
The alignment is not uniform in all device.
How can I maintain the Uniform alignment and common layout and images to support all android device Version?
If I am understanding you correctly... you want to display the proper layouts based on screen densities. Is this correct?
If so then you will want to formally name your layout directories. From Android: Supporting Multiple Screens
For example, the following is a list of resource directories in an
application that provides different layout designs for different
screen sizes and different bitmap drawables for medium, high, and
extra high density screens.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

Categories

Resources