androidxmlandroid-layoutimagebutton

Scaling the Image button in an included layout


I have a layout with purely some numeric keys and include this in another layout. My problem is that these keys do scale in width when I change orientation or place other elements next to it.

my includable keys xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="3">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_7"
            android:text="@string/numeric_7"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_8"
            android:text="@string/numeric_8"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_9"
            android:text="@string/numeric_9"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>


</LinearLayout>

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="3">


    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_4"
            android:text="@string/numeric_4"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_5"
            android:text="@string/numeric_5"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_6"
            android:text="@string/numeric_6"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

</LinearLayout>

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="3">


    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_1"
            android:text="@string/numeric_1"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_2"
            android:text="@string/numeric_2"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_3"
            android:text="@string/numeric_3"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

</LinearLayout>

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="3">


    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_x"
            android:text="@string/numeric_x"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_0"
            android:text="@string/numeric_0"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_numeric_dot"
            android:text="@string/numeric_dot"
            android:textSize="@dimen/textSize_numeric_keys"
            android:textColor="@color/numeric_button_text_color"
            android:background="@drawable/btn_register_main"
            android:layout_margin="@dimen/main_buttons_margin"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"/>

</LinearLayout>

the XML I am including it in but does not resize the height of the buttons, it just hides the lower keys for which there is no space

<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="@color/background_color"
          android:orientation="vertical"
          android:weightSum="3"
          tools:context="Fragments.RegisterFragments.DrawerRegisterFragment">

<ImageView android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:src="@drawable/bomb_icon"/>

<include layout="@layout/includable_view_numeric_keys"
         android:layout_weight="2"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"/>

Solution

  • oke, i found the problem, i should have made the linear layouts that hold the buttons jave a hight of 0dp, and let the layoutweight take care of it. here is my new xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:weightSum="4"
              android:layout_height="wrap_content">
    
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:orientation="horizontal"
                  android:weightSum="3">
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_7"
                android:text="@string/numeric_7"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_8"
                android:text="@string/numeric_8"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_9"
                android:text="@string/numeric_9"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
    
    </LinearLayout>
    
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:orientation="horizontal"
                  android:weightSum="3">
    
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_4"
                android:text="@string/numeric_4"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_5"
                android:text="@string/numeric_5"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_6"
                android:text="@string/numeric_6"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
    </LinearLayout>
    
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:orientation="horizontal"
                  android:weightSum="3">
    
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_1"
                android:text="@string/numeric_1"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_2"
                android:text="@string/numeric_2"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_3"
                android:text="@string/numeric_3"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
    </LinearLayout>
    
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:orientation="horizontal"
                  android:weightSum="3">
    
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_x"
                android:text="@string/numeric_x"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_0"
                android:text="@string/numeric_0"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_numeric_dot"
                android:text="@string/numeric_dot"
                android:textSize="@dimen/textSize_numeric_keys"
                android:textColor="@color/numeric_button_text_color"
                android:background="@drawable/btn_register_main"
                android:layout_margin="@dimen/main_buttons_margin"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"/>
    
    </LinearLayout>
    
    
    </LinearLayout>