Wun
Wun

Reputation: 6391

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

enter image description here

Does there has any wrong ??

Upvotes: 0

Views: 405

Answers (2)

Sayed Jalil Hassan
Sayed Jalil Hassan

Reputation: 2595

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

Upvotes: 0

Mike Ortiz
Mike Ortiz

Reputation: 4041

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.

Upvotes: 1

Related Questions