Should I make different layout (xml files) for different android mobiles - android

Should I create different layout folders to store my xml layout files. To support my application over different screen sizes?
I developed an application and when adding drawables it automatically creates different sizes like xdpi ldpi and more but layouts xml file are not auto created to support different screen sizes. should I do it? and also i will edit manifest file to support different sizes by using support-screen tag. and is this all?? And will it also support my landscape or portrait mode. Please confirm me. I am new to stack and android development.
Edit:
I believe different layout files in different folders.. will just be a copy of each other with only folder name change ad shown in code
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
This is my layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context="com.example.root.meeransunday.MainActivity">
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="90dp"
android:text="Send Mobile"
android:drawableLeft="#mipmap/sms"
android:layout_alignParentBottom="true"
android:layout_marginRight="-1dp"
android:layout_marginLeft="-3dp"
android:layout_marginBottom="-4dp"
android:onClick="message"/>
<Button
android:id="#+id/button2"
android:layout_width="200dp"
android:layout_height="90dp"
android:text="QR Code"
android:drawableLeft="#mipmap/qr"
android:layout_marginLeft="190dp"
android:layout_marginRight="-20dp"
android:layout_marginBottom="-4dp"
android:layout_alignParentBottom="true"
android:onClick="scan"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:text=" My Account Balance"
android:textColor="#0D47A1"
/>
<TextView
android:text="PKR 1527.87"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:drawableLeft="#mipmap/money"
android:textSize="35sp"
android:id="#+id/textView2"
/>
</RelativeLayout>
Manifest file:
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"
android:resizeable="true"/>
but it does not work on 4 inch screen.

If the layout is the same, there's no need to create multiple layout files, you should use different dimens files to adjust the size of your elements.
If you want your application to look different on a hdpi device than it looks on a xxxhdpi or if you have a mobile and a tablet version of the screen, then you should use multiple layout files.

This should be helpful : https://developer.android.com/guide/practices/screens_support.html
Anyway I think that you should use max 2 layouts xmls (1 for horizontal view, 1 for vertical view) for activity.

Actually creating different xml layouts for different android devices it's depends on requirement. It's always good to have only one xml layout for all type of devices to avoid redundant code, But we can go for creating xml's for all type devices, android has that feature.

No need to design all the Layouts,If you are making app for Phone as well as Tablets then you can design 2 Layouts. Otherwise design only these
1. Portrait
2. Landscape

Related

How to dynamically adjust text size and margins for different screen sizes? [duplicate]

This question already has answers here:
How to define dimens.xml for every different screen size in android?
(10 answers)
Closed 4 years ago.
I am developing an android app using the ConstraintLayout. It shows properly on different screen sizes, however, I would like for the textviews to increase in size and spacing between the elements to take advantage of larger screen real estate on bigger screen phones.
I've read that you can create different XML layouts for different densities or sizes (xxhdpi, xhdpi), but I thought one of the main advantages of ConstraintLayout over relative layouts was that it would adjust spacing/size automatically. Can you do this with constraint layout, or do you need to create individual xml layouts for each screen size/density? Thanks!
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="19dp"
android:backgroundTint="#color/colorAccent"
app:layout_constraintBottom_toTopOf="#+id/inView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/lbsView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/weightView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="27dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="14dp"
android:text="Weight:"
android:textSize="26sp"
app:layout_constraintEnd_toStartOf="#+id/weight"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
This is a sample of my constraint layout.
You can different size for your tex using values-[smallestWidth].
SW : The fundamental size of a screen, as indicated by the shortest dimension of the available screen area. Specifically, the device's smallestWidth is the shortest of the screen's available height and width (you may also think of it as the "smallest possible width" for the screen). You can use this qualifier to ensure that, regardless of the screen's current orientation, your application's has at least dps of width available for it UI.
Example:
Let's confider 3 type different layout , small , medium , large
values : <dimen name="text_size">10sp</dimen>
values-sw375 : <dimen name="text_size">11sp</dimen>
values-sw600 : <dimen name="text_size">18sp</dimen>
Put your margin values in dimens.xml, from there you can adjust size in different screen size like values-hdpi, values-xhdpi`,..
In values-hdpi/dimens.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<dimen name="spinner_margin_top">16dp</dimen>
</resources>
In values-xhdpi/dimens.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<dimen name="spinner_margin_top">20dp</dimen>
</resources>
And in your xml file:
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spinner_margin_top"
.... />
As #Farid said.. you can use dimens for different-2 size of devices.
This question is also helpful for you to understand dimens.
If you are not want to declare dimens for every size then you can use this libraries sdp & ssp.

How to make layout for diffrent screen sizes using Relative Layout for Android

I have coded the whole app and designed all the layouts in Relative Layout, but now I want to make it compatible with all the screen sizes.
I would recommend using the percent relative layout
It offers the following attributes which you can use to set dimensions accordingly.
layout_widthPercent
layout_heightPercent
layout_marginPercent
layout_marginLeftPercent
etc.
In most cases Android will automatically resize your layout to fit the screen. However, if you want to ensure that elements will be placed the way you define them to be then use different layouts for different layouts for different screen size. The configuration qualifiers you can use to provide size-specific resources are small, normal, large, and xlarge. For example, layouts for an extra-large screen should go in layout-xlarge/. Android documentation
Try assigning weights to UI components. Works like charm, not matter what screen size is
example
<LinearLayout
android:weightSum="2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:text="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="1"
android:text="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Android layout issue for high end devices

Am a newbie to Android UI...
I got the layout below which is basically a image view, a image button (that is hidden until a certain logic is met) and another image view at the bottom.
This layout works fine in ldpi devices but look bad on other devices... I also had to resize the image by changing its height so it influence the quality of the image.
How can I resolve this by having a standard view that will work?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/welcomeTutorialImage"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:contentDescription="#string/welcome_tutorial"
android:src="#drawable/welcome_tutorial_browse" />
<ImageButton
android:id="#+id/welcome_tutorial_start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/welcomeTutorialImage"
android:layout_centerInParent="true"
android:layout_centerVertical="false"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:contentDescription="#null"
android:src="#drawable/btn_startsavingstatic" />
<ImageView
android:id="#+id/welcomeTutorialProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/welcome_tutorial_start_btn"
android:layout_centerInParent="true"
android:contentDescription="#string/welcome_tutorial"
android:src="#drawable/welcome_tutorial_bar1" />
</RelativeLayout>
You might want to look at android:fitXY property of ImageView. Also, post screenshots that could explain the issue further. There might be a need to have different sized images for different screen sizes/resolutions (ldpi, mdpi, hdpi, xhdpi)
most of the time, you have to adjust the layout and the image resolution for your application to make it flexible with other devices by placing them to the right folder like:
-drawable
-drawable-1280x752
-drawable-800x480
-drawable-hdpi
-drawable-ldpi
-drawable-mdpi etc.
-layout
-layout-land-1280x752
-layout-800x480
-layout-land
etc.
by placing your layout and image to there designated folder, you have to change each layout and image sizes that will much the requirement of each folder.
and if you need further reference you can check this out
http://developer.android.com/guide/practices/screens_support.html

Layout for tablets

The attached layout which works correctly for a phone (Samsung Infuse), but it does not show
layout correctly when the app is uploaded to a tablet (Nexus 7). The button sizing is incorrect. How can I fix this?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/"
style="#string/menu_settings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/gecko25"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:textAlignment="center"
android:visibility="visible" >
<Button
android:id="#+id/link1_btn"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:minHeight="58dip"
android:minWidth="98dip"
android:text="About"
android:textStyle="bold" />
<Button
android:id="#+id/link2_btn"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:minHeight="58dip"
android:minWidth="98dip"
android:text="Experience"
android:textStyle="bold" />
<Button
android:id="#+id/link3_btn"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:minHeight="58dip"
android:minWidth="98dp"
android:selectAllOnFocus="false"
android:text="Bulacards"
android:textStyle="bold" />
</LinearLayout>
Make different layouts for different densities. ie, place the same xml file in layout-hdpi,layout-xhdpi,layout-tvdpi
To create those folders in res/layout follow these steps:
Right click on layout folder --> new-->other-->Andorid xml layout-->next-->give the filename(must be same in all densities)-->AvailableQulaifiers tab select density--> move it to chosen qualifiers with the help of arrow in between--> there select for what density (medium high tv)--->finish
Like for example:
Nexus S and Nexus one ---> hdpi
Galaxy Nexus ----> xhdpi
Nexus 7 ---> tvdpi
and give heights and widths for the Button.. best practice is to set them to wrapcontent , if for your scenario it requires height follow the above steps. and give height and width such that different for phone and tablets which comes under different densities
It's not recommended that you use tvdpi for Nexus 7 screens.
See this:
http://android-developers.blogspot.com/2012/07/getting-your-app-ready-for-jelly-bean.html
Probably the best way to detect 7" tablets (like the Nexus 7) is to use the qualifier:
layout-sw600dp
And the qualifier for 10" tablets:
layout-sw720dp
You might want to try specifying the button sizes in sp rather than dp as they may need to bigger or smaller if the user changes the default font size but wrap_content and/or use weights are usually best.

Font size for different density devices

When I tested the font size with different density settings using the Eclipse Emulator, I get different size fonts even though I am using "dp" to specific the size. I would have thought the size may decrease with higher density, but I found the higher the density, the bigger the font is. I tried using "in" and "mm" and the same behavior occurs, in that the higher density, the bigger the fonts. This is based on android:minSdkVersion="10".
Is there any way I can set the font sizes to scale to the same size for the different density?
Here is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_gravity="center"
android:textColor="#color/orange"
android:textStyle="bold"
android:text="THIS IS A TEST"
android:background="#color/white"/>
</LinearLayout>
Unfortunately I cannot post the images of the font size for the three Emulator settings due to lack of reputation.
Thank you.
I had the same problem... And did't found anything, but making folders:layout-small...large, xlarge, and folders with drawable images for high, mid and small densities with resolutios of images according to it... So, everything works well on different droids with different screens... For every display size there is layout with different meanings of dp...((
Use sp instead of dp
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="center"
android:textColor="#color/orange"
android:textStyle="bold"
android:text="THIS IS A TEST"
android:background="#color/white"/>
</LinearLayout>
I guess one way is this:
When you make new layout file there is next button in eclipse. Press that and there is a list where to pick layout's density. You can pick x-high density, high density, medium density etc.. When you press finish it makes layout folder like "layout-hdpi" (depends what density you pick). And then make text fit good with that density.
Hope that helps. :)

Categories

Resources