my android app runs on my mobile device correctly.
but the size of elements dose not change in tablet or another device with different resolution.
they are shown small .
i think my app should be able to fit to different resolution.but i do not know how can i do?
i write some my cod below.
their size do not change.
<View
android:id="#+id/view_top"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_below="#+id/view_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
/>
<Spinner
android:id="#+id/spinner_first"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_top"
android:layout_alignRight="#+id/view_top"
android:layout_toLeftOf="#+id/button1"
/>
<EditText
android:id="#+id/edt_number"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/spinner_first"
android:layout_toLeftOf="#+id/spinner_first"
android:ems="10"
android:inputType="phone" >
</EditText>
Use dimension resources:
For example:
android:layout_width="250dp"
This must be changed to:
android:layout_width="#dimen/mycustomwidth"
Want to know how to define custom width for each device resolution?
Go and thoroughly learn about "dimens.xml" file and much more from Android official guide Supporting Multiple Screens.
you must create dimensions files
in each file you put values used in ech device .
for example you create values-xlarge in it create dimen.xml file .
values-hdpi, values-xhdpi ...
Related
When I see on the XML layout file in the android studio I can see the padding is there but on the device, I can not see it. I am not adding padding programmatically on that button. I only set the background of the button programmatically.
In Android Studio
In Device
Here is my button code
<Button
android:id="#+id/LoadBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:minWidth="0dp"
android:layout_marginTop="16dp"
android:background="#color/colorAccent"
android:foreground="?android:attr/selectableItemBackground"
android:text=" LOAD MAP "
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold"
android:paddingStart="7dp"
android:paddingEnd="7dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:layout_constraintStart_toStartOf="#+id/fileTypeRadioGroup"
app:layout_constraintTop_toBottomOf="#+id/fileTypeRadioGroup" />
Here is the java code that I am doing with that button.
loadBtn = findViewById(R.id.LoadBtn);
loadBtn.setEnabled(false);
loadBtn.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.disabled_button_background));
loadBtn.setEnabled(true);
loadBtn.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.color.colorAccent));
Try setting the text of the button to dp, instead of sp. Sp will scale the text based on the device's preferences, while dp will not. It looks based on the example that they are just differently sized texts.
It could be that inside your android studio you are watching a display of one phone but when you run it you actually run it on another phone with a totally different screen resolution.
So you may see it as you would like to in your preview but this screen preview and your phone screen are different, and because different phones got different screen sizes and pixel densities you are seeing this differently.
When using a fixed size (7dp) value for padding for example for larger screens you will need to use more than 7dp if you want it to look like the preview or less if you are using a smaller screen than what you have in your android studio preview.
A good solution could be to use sdp library:
From the library github page:
An android lib that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size. It can help Android developers with supporting multiple screens.
How to use:
You will first need to implement this:
dependencies {
implementation 'com.intuit.sdp:sdp-android:1.0.6'
}
And now on your layout file simply add the wanted padding:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Text"
android:textSize="30sp"
android:textStyle="bold"
android:padding="#dimen/_18sdp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</Button>
And it will look like this:
Something extra
If you want to make your texts scale in size as well according to the screen size (as I just showed for dp) you can use
AutosizingTextViews or you can even use
ssp library
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
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
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.
I've recently developed an android game using Eclipse. I'm really a newbie at this so forgive me if what I will ask is too simple. >.<
The application runs and looks perfectly in the Android emulator installed in Eclipse. However, when we tried to install it in a Samsung Galaxy tab which obviously has a bigger screen than the emulator, the layout gets messed up. Buttons are not in their right order etc. I do have screens using XML layouts and some are simple canvases with sprites. Any idea how I could retain the original layout in the emulator to the tablet? I've tried using RelativeLayout instead of LinearLayout but it still doesn't work.
Any help would be appreciated. Thank you!
One XML file code and it's corresponding screen from emulator shown below.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/MenuLayout" >
<Button android:id="#+id/btnNew"
android:layout_width="140px"
android:layout_height="65px"
android:layout_marginTop="160px"
android:background="#drawable/newgame"/>
<Button android:id="#+id/btnInst"
android:layout_width="140px"
android:layout_height="65px"
android:background="#drawable/instructions" />
<Button android:id="#+id/btnCredits"
android:layout_width="140px"
android:layout_height="65px"
android:background="#drawable/credits"/>
<Button android:id="#+id/btnExit"
android:layout_width="140px"
android:layout_height="65px"
android:background="#drawable/exit"/> </LinearLayout>
You need to use dpi and not pixels.
Here's a good resource.
http://developer.android.com/guide/practices/screens_support.html
As others have mentioned, you should be using "dp" instead of "px". These are device independent pixels and will scale to match different screen resolutions.
You should also follow this guide on how to use a folder structure to support different screen resolutions and sizes.
Cheers
Based on your edited question, and on the comments, here is a way to change your layout to a relative one:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/MenuLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/btnNew"
android:layout_width="140dip"
android:layout_height="65dip"
android:layout_marginTop="160dip"
android:background="#drawable/newgame"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btnInst" />
<Button android:id="#+id/btnInst"
android:layout_width="140dip"
android:layout_height="65dip"
android:background="#drawable/instructions"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btnCredits"/>
<Button android:id="#+id/btnCredits"
android:layout_width="140dip"
android:layout_height="65dip"
android:background="#drawable/credits"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btnExit"/>
<Button android:id="#+id/btnExit"
android:layout_width="140dip"
android:layout_height="65dip"
android:background="#drawable/exit"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dip"/>
</RelativeLayout>
Though, if you want a perfect solution, you need to design the buttons and background pictures in 3 sizes, and put them in the respective folders drawable-hdpi, drawable-mdpi and drawable-ldpi(and for tablets, from 2.2, you can also have drawable-xhdpi!). Read carefully this article and test your layout in emulator for as many screens as possible (from ADT 10.0 it is really easy to preview your layout for different configurations directly from Graphical Layout tab in xml).
Also, android:layout_marginTop="160dip" and android:layout_marginBottom="25dip" are not really useful in a Relative Layout. I would suggest you to try a 9-patch picture for your background, with only the wood board stretchable for the buttons (though I am not sure it's working like that). Alternatively, I would have a separate picture with Main Menu, that I'd place above the buttons, and keep in the background only the trees and everything else.
itz better to provide diffrent layout for diffrent screen size . i also have same issue when developing a game.nw its done.
u may have provided layout ,so u just provide
layout-large,small,and xlarge.and vice verse to ur layouts u must provide drawable folders like hdpi,mdpi,ldpi,xdpi folders and u must save diffrent sized images 58*58,75*75,100*100 for following then u check it u will get it.