0

I'm trying to have 3 ImageButtons at the bottom of my app(1/4 of the screen's height and 1/3 of the screen's width)and I wanted the imageButtons to be able to resize depending on the device, without hardcoding the sizes. I have tried putting the three ImageButtons inside a linear layout and use layout_weight=1 for all three of them, but the Images didn't actually scale down, it just cropped a portion of the image to fit all three ImageButtons in there. Thanks so much!

Jay Xu
  • 13
  • 1
  • 5

2 Answers2

0

yes you can first find the screen width and height as follow-

    int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
 int height = getApplicationContext().getResources().getDisplayMetrics().heightPixels; 

now convert your pixels in dp using this tutorial

then set your button height and width dynamically as-

 ViewGroup.LayoutParams params = myButton.getLayoutParams();
//Button new width
params.width = 400;

myButton.setLayoutParams(params);
Community
  • 1
  • 1
sud
  • 505
  • 1
  • 4
  • 12
  • Thank you! I tried this, but the Activity would unexpectedly stop...I executed those before the setContentView on the mainActivity, onCreate() method... – Jay Xu Nov 30 '15 at 04:38
0

Try this

<?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:background="#FFFFFF"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/RLMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCFFFFFF"
    android:orientation="vertical"
    android:weightSum="4" >

    <LinearLayout
        android:id="@+id/LL1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal" >

        <!-- //...Your Other Layout Views here -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="9" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:src="#FF0000" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:src="#FF0000" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:src="#FF0000" />
    </LinearLayout>
</LinearLayout>