Creating common layout structures in android? - 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

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.

Android Development related to Xml layouts

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.

Different screen size layout [duplicate]

i want to my apps support different screen sizes. i add folders "layout-small and layout-large " in /res directory. but XMLs inside this folders aren't accessible in my activity.so i add all my XMLs in default layout and add this code
if((getResources().getConfiguration().screenLayout &&
Configuration.SCREENLAYOUT_SIZE_SMALL) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
setContentView(R.layout.main1);
}else if((getResources().getConfiguration().screenLayout &&
Configuration.SCREENLAYOUT_SIZE_LARGE) == Configuration.SCREENLAYOUT_SIZE_LARGE){
setContentView(R.layout.main2);
}
else
setContentView(R.layout.main);
in my activity, but when my AVD skin is 1024*600 and hw.lcd.dencity is 160 (large) it didn't work.
any help?
Size: small, normal, large
Density: ldpi, mdpi, hdpi,
nodpi(no auto-scale)
Aspect ratio: long, notlong
Orientation: land
Usage:
res/layout/my_layout.xml
res/layout-small/my_layout.xml
res/layout-large/my_layout.xml
res/layout-large-long/my_layout.xml
res/layout-large-land/my_layout.xml
res/drawable-ldpi/my_icon.png
res/drawable-mdpi/dpi/my_icon.png
res/drawable-hdpi/my_icon.png
res/drawable-nodpi/composite.xml
Restricting your app to specific screen sizes(via the AndroidManifest):
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true" />
...
</manifest>
And for code level tweeking:
float scale = getContext().getResources().getDisplayMetrics().density;
And don't forget:
dpi = 160; //At 160dpi
pixels = dips * (density / dpi)
Also refer support multiple screen in android
Layout name must be same
layout-small \ main1.xml
layout-normal\ main1.xml
layout-large \ main1.xml
You dont need to handle this, android automatically decide which layout will be used
setContentView(R.layout.main1);
Please look at this:
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
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
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
more read so please see this link How to support different screen size in android
Android automatically does this for you. For the different screen sizes, make different xml files, but give them the same name, for example main.xml and put the one for large in the folder /res/layout-large, for small in /res/layout-small, et cetera.
in the onCreate(), just put setContentView(R.layout.main)
For more information, consider reading this site from Android Developers.
Android will automatically pick up the best resource for a given particular device.If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and density.
The "default" resources are those that are not tagged with a configuration qualifier.

Defining layout.xml files based on screen density (v.2.3.3+)

Afaik resolution is concerned with density (correct me if I'm wrong) and my goal is to use one of three layouts according to device's density. What're my options with 2.3.3 (most popular atm) sdk version ?
Thank you.
Not really.
There are two primary aspects to consider:
Screen density. ldpi, mdpi, hdpi, xhdpi. This is the number of pixels per inch. It relates to the size of the pixels (not the screen).
Screen size. small, normal, large, x-large - or specified by pixel size. This relates to the actual physical size.
How you work with them depends on your project's needs. Typically you would provide images in various densities (so icons etc look good on all devices), but if you're building an app to work well on devices of very different sizes (small phones through to 10" tablets) you should also provide layouts for the various sizes of screen - perhaps one for phones, one for 7" tablets, and one for 10" tablets.
http://developer.android.com/guide/practices/screens_support.html
Take a look at the android documentation about supporting multiple screens - the gist is that you can have multiple folders with different naming conventions that target devices based
on a whole array of things (screen density, screen size, portrait/landscape etc) and they can be as vague or as detailed as you want.
For example if you wanted to target all xhdpi devices you make a layout folder called 'layout-xhdpi', make a layout inside it and all xhdpi devices will use that over any other. If you wanted to be more specific and for example target the Galaxy Nexus specifically you can create a folder named 'layout-w360dp-port-xhdpi'.
Another way which I prefer to do is have a single layout file and have multiple 'values' xml files targeting the different screen sizes, and in the values files change the values taken in for padding/heights and point to these values in your layout. e.g. in 'values/dimensions.xml'
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="webview_height">53dip</dimen>
<resources>
then in your layout:
<?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="vertical" >
<WebView
android:layout_height="#dimen/webview_height"
android:id="#+id/mainWebViewMobile"
android:layout_width="match_parent"
></WebView>
</LinearLayout>
and you can create multiple dimensions.xml in separate folders targeting different devices (e.g. 'values-xhdpi/dimensions.xml' or 'values-w360dp-port-xhdpi/dimensions.xml') and alter the value of 'webview_height'.
Just create a different layout for each density and place it in appropriate folder
See this from android docs:
http://developer.android.com/guide/practices/screens_support.html#qualifiers
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
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density

Android: How to access different layouts depending of screen density in XML

I am wondering how to get different XML layout files to be read depending of density of the screen.
Now when I enter a layout file and set to so that it has a couple of buttons and have them set to a specific position, the position changes when I change to another screen density in the emulator. This is what should be happening what i understand- but How do I get the program to use different layouts depending of densities?
I have been reading on android dev. page on how to develop for different screens, but I didn't get the examples which were there.
I have been using dp/dip on sizes so no need to suggest that =)
you can do it by creating a layout for each standart screen sizes,
you can find here how to do it.
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
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
it means you should create sub-directory named as ("layout", "layout-small","layout-large","layout-xlarge","layout-xlarge-land") in res folder.
than create a main xml for each one with the same name.
By placing the layout XML files in different folders you can get Android to load the correct one depending on the screen density. For example, if you layout is called "main.xml":
Placing the file in /res/layout-ldpi/main.xml will mean it is used only in low density (or above)
Placing the file in /res/layout-mdpi/main.xml will mean it is used only in medium density (or above)
Placing the file in /res/layout-hdpi/main.xml will mean it is used only in high density (or above)
Placing the file in /res/layout-xhdpi/main.xml will mean it is used only in extra-high density
You can do the same sort of thing with all resources- drawables, strings, dimensions etc. (e.g. drawable-mdpi, or values-en [values only to be used in an English locale]). You can find the whole list of different device set-ups supported by this system at http://developer.android.com/guide/topics/resources/providing-resources.html (Table 2: Configuration Qualifier names)

Categories

Resources