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

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" />

Related

Android: ImageView's Location Changes on different sizes screen

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.

Android ImageButton Resolution Problems

I want to have two buttons at the top of my program taking users to different activities. I'm having a lot of trouble with the formatting.
How can I make it so that the buttons will stretch proportionally based on the screen size? Right now, they will look OK for one screen size, then I will switch to a different one and it will appear all smushed or stretched. I've tried all of the different ScaleTypes and none seem to make a difference. I also went though and proportionally saved all of the images to the correct sizes regardimg xhdpi, hdpi, etc using Shubhayu's answer.
Here's my code so far:
<LinearLayout 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/brushed_metal_background_dark"
tools:context=".HomeActivity" >
<ImageButton
android:id="#+id/incidentsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/incident_bar2"
android:contentDescription="Incidents"
android:onClick="chooseIncident"
android:scaleType="center" />
<ImageButton
android:id="#+id/operationalPeriodsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/operationalperiod_bar2"
android:contentDescription="Operational Periods"
android:onClick="chooseOperationalPeriod"
android:scaleType="fitCenter" />
Change android:background to android:src that will keep the aspect ratio. Use android:scaleType="centerInside" to fit whole image inside button area and optionally use android:adjustViewBounds=true to remove empty spaces. Example:
<ImageButton
android:id="#+id/incidentsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="Incidents"
android:onClick="chooseIncident"
android:src="#drawable/incident_bar2"
android:scaleType="centerInside"
android:adjustViewBounds="true"/>
<ImageButton
android:id="#+id/operationalPeriodsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="Operational Periods"
android:onClick="chooseOperationalPeriod"
android:src="#drawable/operationalperiod_bar2"
android:scaleType="centerInside"
android:adjustViewBounds="true"/>
I am guessing that you are trying to size the buttons evenly on the top of the screen. If that's the case then you should set android:layout_width="0dp".
Just use android:layout_width = "wrap_content"

Overlapping image not properly displayed on all formats

I'm building a graph which consists of 2 images: 1 background image and one image containing the actual data points. The data points image show be aligned at the top of the background image.
The layout looks like this:
<RelativeLayout
android:id="#+id/graphImageWrap"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/graphImageBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:contentDescription="#string/stroom_grafiek"
/>
<ImageView
android:id="#+id/graphImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/graphImageBackground"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:contentDescription="#string/stroom_grafiek"
/>
</RelativeLayout>
This works well on a HDPI device:
But on a MDPI device it doesn't align properly:
I hope someone can explain what the problem is.
You may want to use a FrameLayout instead, and make sure the images are the same size.

Android Multiple Screen Support Problem

I Have used my all my images in drawable-hdpi folder . why cannot my layout properly outcome my emulator with different sizes. I hava faced this error according to pictures.
I have also include following code in manifest file.
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="false"
/>
Here android:anyDensity="false" check both false and true but could not any change, and also use Resizable: true and android:smallScreens="false" also used but could not get any change.
my all images are 96dpi resolution and main background image size 480*800 for normal screen of hdpi(WVGA800). so what is the my problem please find out and help me. Thanks in advance.
Edited:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:background="#drawable/normalbg"
android:gravity="top">
<ImageView android:id="#+id/group1" android:background="#drawable/groupone"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_marginRight="5dip" android:layout_marginTop="138dip"/>
<ImageView android:id="#+id/group2" android:background="#drawable/grouptwo"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_below="#id/group1"
android:layout_marginRight="5dip"/>
<ImageView android:id="#+id/group3" android:background="#drawable/groupthree"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_below="#id/group2"
android:layout_marginRight="5dip"/>
<ImageView android:id="#+id/group4" android:background="#drawable/groupfour"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_below="#id/group3" android:layout_marginRight="7dip"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:baselineAligned="true">
<ImageView android:id="#+id/backToLevel" android:background="#drawable/back"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_below="#id/group4"
android:layout_gravity="center_horizontal" android:layout_marginLeft="116dip" android:layout_marginBottom="3dip"/>
</LinearLayout>
</RelativeLayout>
I used drawable folder all images then get the all images. I used this all layout in Relative layout rather than linear, and Make the all it properties. I skip the margin , below and above properties. First on alignparentcenter is true, then below or above the that centered image. Its work fine then the above my layout.
I assume you youse RelativeLayout to show the back button on the buttom. Like this, it may hover over the menu buttons if there isnĀ“t enough space for it.
Consider adding the back button into the menu buttons' LinearLayout.
To resize the image, consider using a 9-Patch Drawable.

problem in setting image over a background image android

i have image-view in my xml for setting the image over a background image at a specified place of back ground image i set it for one emulator and it works but when i launch other emulator of different size image-view change it corresponding position with respect to background image so how to set image view over a background image so that imageview not changes its position for any size of screen
i am providing my code ...thanks in advance..
here is my splash.xml file ,button and imageview changes its position w.r.t. background image..for diffrent size screen emulator
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/TheSplashLayout"
android:layout_gravity="center"
android:background="#drawable/splashmodified"
>
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
android:id="#+id/SplashImageView"
android:layout_gravity="center"
android:layout_marginTop="120sp"
android:layout_marginLeft="55sp"
/>
<Button
android:text="SUBMIT"
android:id="#+id/submitt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75px"
android:layout_marginTop="300px"
/>
</RelativeLayout>
Did not understand the question, you might consider uploading some images and giving us links to them. Only thing I can tell is that you must avoid using px when indicating any dimensions. This will make your views look different on every device, that you probably don't want.
use dip instead of px and sp.
Your xml will be
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/TheSplashLayout"
android:layout_gravity="center"
android:background="#drawable/splashmodified"
>
<ImageView
android:layout_width="60dip"
android:layout_height="60dip"
android:id="#+id/SplashImageView"
android:layout_marginTop="120dip"
android:layout_marginLeft="55dip"
android:background="#drawable/a_thumb"
/>
<Button
android:text="SUBMIT"
android:id="#+id/submitt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dip"
android:layout_marginTop="300dip"
/>
</RelativeLayout>
Thanks
Deepak

Categories

Resources