Android: ImageView's Location Changes on different sizes screen - android

I have five image views in my layout and I should put every image view on specific location on the background. When running my app on different screen sizes, Locations of image views are shifted up.
I create the following folders.res/layout- smallres/layout-normalres/layout-largeres/layout-xlargeHere you will find my layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/body"
android:layout_below="#+id/header"
android:background="#drawable/upn">
<ImageView
android:layout_width="45dip"
android:layout_height="20dip"
android:id="#+id/U1"
android:layout_gravity="center"
android:background="#f40c0d"
android:padding="1dip"
android:layout_marginRight="26dip"
android:layout_marginEnd="26dip"
android:layout_marginBottom="96dip"
android:layout_above="#+id/U5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="45dip"
android:layout_height="20dip"
android:id="#+id/U3"
android:layout_gravity="center"
android:background="#f40c0d"
android:padding="1dip"
android:layout_marginRight="63dip"
android:layout_marginEnd="63dip"
android:layout_above="#+id/U1"
android:layout_toLeftOf="#+id/U1"
android:layout_toStartOf="#+id/U1"
android:layout_marginBottom="22dip" />
<ImageView
android:layout_width="45dip"
android:layout_height="20dip"
android:id="#+id/U2"
android:layout_gravity="center"
android:background="#f40c0d"
android:padding="1dip"
android:layout_marginRight="5dip"
android:layout_marginEnd="5dip"
android:layout_marginBottom="103dip"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/U3"
android:layout_alignEnd="#+id/U3"
/>
<ImageView
android:layout_width="45dip"
android:layout_height="20dip"
android:id="#+id/U4"
android:layout_gravity="center"
android:background="#f40c0d"
android:padding="1dip"
android:layout_above="#+id/U2"
android:layout_alignLeft="#+id/U5"
android:layout_alignStart="#+id/U5"
android:layout_marginBottom="18dip"
/>
<ImageView
android:layout_width="45dip"
android:layout_height="20dip"
android:id="#+id/U5"
android:layout_gravity="center"
android:background="#f40c0d"
android:padding="1dip"
android:layout_marginLeft="68dip"
android:layout_marginStart="68dp"
android:layout_above="#+id/U2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="43dp"
/>
</RelativeLayout>
</RelativeLayout>

res/layout/your_layout.xml //layout for normal screen size("default")
res/layout-large/your_layout.xml //layout for large screen size
res/layout-xlarge/your_layout.xml //layout for extra-large screen size
res/layout-xlarge-land/your_layout.xml //layout for extra-large in landscape orientation
and in each folder ..the a your_layout.xml with different image positions
eg. res/layout_large/your_layout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/body"
android:layout_below="#+id/header"
android:background="#drawable/upn">
<ImageView
android:id="#+id/my_iv"
.....
/>
</RelativeLayout>
maybe you have to add this lines in your manifest file
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
//More informations:
http://developer.android.com/guide/practices/screens_support.html#support
Provide different layouts for different screen sizes
By default, Android resizes your application layout to fit the current device screen. In most cases, this works fine. In other cases, your UI might not look as good and might need adjustments for different screen sizes. For example, on a larger screen, you might want to adjust the position and size of some elements to take advantage of the additional screen space, or on a smaller screen, you might need to adjust sizes so that everything can fit on the screen.
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/.
Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you should instead use the sw<N>dp configuration qualifier to define the smallest available width required by your layout resources. For example, if your multi-pane tablet layout requires at least 600dp of screen width, you should place it in layout-sw600dp/. Using the new techniques for declaring layout resources is discussed further in the section about Declaring Tablet Layouts for Android 3.2.

Related

How can i make an app to fit all screen sizes using relative layout?

How can i make an app to fit all screen sizes using relative layout?
At the moment i am using constant values for the button positions and sizes and it looks good only on HTC One S. That's the device i am using for development.
I want the buttons to be on the appropriate positions on every display.
I have one background in the app, that's the HUD you can see it here:
The buttons and the LED must be in the exact locations inside the holes. And it looks like this:
The layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/hud"
android:orientation="horizontal"
android:splitMotionEvents="false"
android:windowEnableSplitTouch="false"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/connect_button"
android:layout_width="122sp"
android:layout_height="122sp"
android:layout_marginLeft="338dp"
android:layout_marginTop="54dp"
android:background="#drawable/connect_inactive"
android:contentDescription="#string/connect_button_description" />
<ImageButton
android:id="#+id/disconnect_button"
android:layout_width="122sp"
android:layout_height="122sp"
android:layout_marginLeft="338dp"
android:layout_marginTop="195dp"
android:background="#drawable/disconnect_inactive"
android:contentDescription="#string/disconnect_button_description" />
<ImageButton
android:id="#+id/throttleup_button"
android:layout_width="122sp"
android:layout_height="122sp"
android:layout_marginLeft="483dp"
android:layout_marginTop="54dp"
android:background="#drawable/throttleup_inactive"
android:contentDescription="#string/throttleup_button_description" />
<ImageButton
android:id="#+id/throttledown_button"
android:layout_width="122sp"
android:layout_height="122sp"
android:layout_marginLeft="483dp"
android:layout_marginTop="195dp"
android:background="#drawable/throttledown_inactive"
android:contentDescription="#string/throttledown_button_description" />
<ImageButton
android:id="#+id/moveforward_button"
android:layout_width="170sp"
android:layout_height="160sp"
android:layout_marginLeft="79dp"
android:layout_marginTop="30dp"
android:background="#drawable/control_forward_inactive"
android:contentDescription="#string/moveforward_button_description" />
<ImageButton
android:id="#+id/movebackward_button"
android:layout_width="170sp"
android:layout_height="160sp"
android:layout_marginLeft="79dp"
android:layout_marginTop="153dp"
android:background="#drawable/control_backward_inactive"
android:contentDescription="#string/movebackward_button_description" />
<ImageButton
android:id="#+id/moveleft_button"
android:layout_width="170sp"
android:layout_height="160sp"
android:layout_marginLeft="17dp"
android:layout_marginTop="92dp"
android:background="#drawable/control_left_inactive"
android:contentDescription="#string/moveleft_button_description" />
<ImageButton
android:id="#+id/moveright_button"
android:layout_width="170sp"
android:layout_height="160sp"
android:layout_marginLeft="141dp"
android:layout_marginTop="92dp"
android:background="#drawable/control_right_inactive"
android:contentDescription="#string/moveright_button_description" />
<ImageView
android:id="#+id/led"
android:layout_width="45sp"
android:layout_height="45sp"
android:layout_marginLeft="297dp"
android:layout_marginTop="280dp"
android:background="#drawable/led_inactive"
android:contentDescription="#string/led_description" />
</RelativeLayout>
Instead of hard coding the values, use the dimen files for this purpose.
Specify different folders for values,like values-large etc, and create dimension files and put specific values according to screen size.
Use weight for all Layouts instead of giving constant values, Because weight is a type of % for screen and in manifest by default support screen view is true.
Create a Multiple Screen size layout folders like this
layout-small , layout-large , layout-xlarge so u copy your layout XML files in to these folder and now u can edit the specified width and size of the boxes .
These links will help you to create application for multiple screens :
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/guide/practices/screens_support.html
These are android's official documents on implementing support for multiple screen sizes.
http://developer.android.com/guide/practices/screens_support.html
please look at following link
http://developer.android.com/training/multiscreen/screensizes.html

Android app: text getting cropped on smaller devices

We are developing an android app. The text displaying properly on 5" devices. But getting cropped on smaller devices (4", 3.5"). why is it happening? Any help welcome. Please see screenshots below. "Credit" "Debit" and "Net" displayed instead of "Credit Card" "Debit Card" and "Net Banking" in screenshot below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<include android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/sdk_header"
/>
<RelativeLayout
android:id="#+id/topLogoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_marginLeft="0dp"
android:layout_marginBottom="-11dp"
android:background="#e5e5e5"
android:paddingTop="10dp"
android:paddingBottom="20dp">
<TextView
android:id="#+id/youtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="right"
android:layout_marginTop="5dp"
android:text="You"
android:textSize="40sp"
android:textColor="#727272"
android:layout_marginLeft="10dp" />
<RelativeLayout
android:id="#+id/arrowLayout"
android:layout_toRightOf="#+id/youtxt"
android:layout_toLeftOf="#+id/companyLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/txt_amount"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Rs. 10"
android:textColor="#727272"
android:textSize="18sp" />
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:src="#drawable/arrow"
android:layout_below="#+id/txt_amount" />
</RelativeLayout>
<ImageView
android:id="#+id/companyLogo"
android:layout_marginTop="5dp"
android:layout_marginRight="30dp"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:src="#drawable/wallet_logo"
android:scaleType="fitXY" />
</RelativeLayout>
<ImageView
android:id="#+id/orange_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/orange_line"
/>
<com.viewpagerindicator.TabPageIndicator
android:layout_marginTop="0dp"
android:id="#+id/indicator" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<android.support.v4.view.ViewPager android:id="#+id/pager" android:layout_width="wrap_content" android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</LinearLayout>
This is a complex topic on which a book could be written (and probably has). I'll focus on the only problem I can see: text size.
It seems that your fonts are too big on the small screen. This affects what appear to be tabs and buttons, although the EditText has the problem too (it just doesn't overflow).
The useful way of measuring screen size is the sw-dp (shortest width device independent pixel). The dp density is adjusted so that it is around 160dpi on all devices. For a 5" tablet that will be around a 3" width and it should be an sw480dp device. The 3"-3.5" screen has around a 2" width and should be an sw320dp device.
To make this work at the simplest level, you need to place some text size settings in your value-sw240dp folder that are about 3/4 of the text size for your default sw480dp device.
For example, this might appear in your dimen file in that folder.
<dimen name="view_field_text_size">14sp</dimen>
I can't give you a cookbook because I have no info about your other use of styles or dimensions. Perhaps in a layout you might have:
<EditText
<android:textSize="#dimen/view_field_text_size"
/>
It's better to put it in a style, but that's outside the scope here.
Different devices may have different pixel density, screen size, Orientation, Resolution and Density-independent pixel (dp).
Solution -
Define different layout files for different screen sizes.
Ensuring your layout can be adequately resized to fit the screen
Providing appropriate UI layout according to screen configuration
Ensuring the correct layout is applied to the correct screen
By default, Android resizes your application layout to fit the current device screen. In most cases, this works fine. In other cases, your UI might not look as good and might need adjustments for different screen sizes. For example, on a larger screen, you might want to adjust the position and size of some elements to take advantage of the additional screen space, or on a smaller screen, you might need to adjust sizes so that everything can fit on the screen.
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/.
Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you should instead use the swdp configuration qualifier to define the smallest available width required by your layout resources. For example, if your multi-pane tablet layout requires at least 600dp of screen width, you should place it in layout-sw600dp/. Using the new techniques for declaring layout resources is discussed further in the section about Declaring Tablet Layouts for Android 3.2.
Look - Multiple Screen Support and Support Screen training docs.
you just need to create different folders related to density or screen size like
Option 1.
values-large
values-small
values-normal
For more explanation check this link...
Multiple Screen Support
Option 2.
mTextView.setTextSize(16 * getResources().getDisplayMetrics().density);
this will give TextSize depending on density..

Widget distance modifyed on different screen device

I've read the android support multiple screen tutorial.
when i try to see how my layout fit on different screen, something gone wrong. In my case i have only one tipe of image (970x174) for test copyed into all drawable folder.
By see my layout on different screen, in most cases, layout is not as i would. I paste code of my layout (that it fits well into nexus 4 screens)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/TEMPORARY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="128dp"
android:src="#drawable/button" />
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="64dp"
android:src="#drawable/button" />
<ImageView
android:id="#+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/button" />
</RelativeLayout>
Maybe i should do a different density image for each screen type?
RelativeLayout doesn't have a orientation tag, so remove it.
Since you put the same image on all the drawable types, the image will have a fixed pixel size, which will decrease the size of the image on the screen as the screen density increases. But you used dp as unit on the layout, so the 128dp will increase in size as the density increases, to make the size on screen appear the same on different devices.
So if you want to have the same layout with the same size on screen regardless of screen density, delete the other copies of the image and leave only one drawable folder, and android will handle scaling for you on different screen density phones.

How to setup the ImageButton to match screen for different device?

I add the picture for 48*48 , 72*72 and 96*96 to mdpi , ldpi and hdpi respective.
And add the following code in AndroidManifest.xml
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
First Question:
The app will capture the suitable picture by itself when I do the above operation ?
Second Question:
But how to setup the Button in the xml file ?
If app will capture the suitable picture by itself , so I have set the width and the height to match_parent like the following code?
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#android:drawable/btn1" />
Thanks in advance.
-------------------------------EDIT----------------------------
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:background="#000000"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/FileButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:src="#drawable/file_viewer"/>
<ImageButton
android:id="#+id/recordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:src="#drawable/record" />
<ImageButton
android:id="#+id/photo_record_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:src="#drawable/recordmode"/>
</RelativeLayout>
</LinearLayout>
I modify the code like the above.
And I install the APP to different size of device , for 4.7 inch and 7 inch.
But it seem use the same size of picture.
and I change from android:background to android:src.
It like the following picture
Does there has any wrong ??
Yes, Android will select the right image resource at runtime depending on the screen density. However, you should set layout_width and layout_height to wrap_content instead of match_parent.
The app will automatically get resources from the appropriate folder based on the device specs. Secondly, even though your image view is set to match parent, the image wont fill the parent because you have set the src property and not the background. If you want the image of imageView to fill the parent, change The image button declaration to this:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:drawable/btn1" />
and if you want it to take the original size, do it like this:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn1" />

android density independence

I created a layout with the prebased (480x800) density. I have been using this since I started learning android (9 mths) and now it was time to test my app on other phones. I tested it on three phones with 480x800 resolution and was fine, until I tested it on one with 320x480 and 240x320. I have used px-s as width and height, paddingTop etc. everywhere.
I checked the app in the emulator (created different avd-s for different resolutions) and I cannot see the whole layout, as it is bigger than the screen (testing it only in eclipse). It has 4 images with "wrap_content" width and height settings.
So I checked the android documentation. I have not created other layouts or anything else, but replaced the px-s with dp-s. It is the same.
I created smaller buttons (see below) with 190x60px resolution and put them into the ldpi folder, but there was no big advance. Maybe because the textsizes of the textviews are the same and the 2 textviews takes 1/3 of the place of the display in case of the 240x320 resolution (while only 1/6 in case of the 480x800). (So the texts look huge in the small resolution compared to the large resolution)
Please tell me what should I do to make the layout look in this 320x480 resolution like in the 480x800.
Size of gradientbg: this is an .xml file for a shape, so no physical size.
Size of buttons (images): 380x150px hdpi (or 190x60px in ldpi folder)
<?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"
android:background="#drawable/gradientbg"
>
<TextView
android:id="#+id/TextView00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E30000"
android:textColor="#FFFFFF"
android:gravity="center_horizontal|center_vertical"
android:textSize="26dp"
android:height="40dp"
android:textStyle="bold"
android:text="Main menu"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App"
android:textSize="30dp"
android:textStyle="bold"
android:paddingTop="20dp"
android:layout_gravity="center_horizontal"
android:textColor="#FFB300"
/>
<Button android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mainbutton_1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
/>
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mainbutton_2"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp"
/>
<Button android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mainbutton_3"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp"
/>
<Button android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mainbutton_4"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp"
/>
</LinearLayout>
When you normally define layouts in dp units, you ensure that the layout stays the same on devices in the same density bucket. But when you try it on a tablet (xlarge) or a small screen, it won't scale right. This tool is made to have your app work with the whole range of devices (small/normal/large/xlarge). It scales your layout xml-files from the baseline density you were originally designing for.
http://onemanmobile.blogspot.com/2012/04/how-to-scale-your-android-layouts-to.html

Categories

Resources