user3668129 user3668129 - 3 months ago 10
Android Question

Why cant I see the all 3 ImageButtons on the same row?

I'm trying to create a layout (which I could include in other layouts), which contains 3 Image buttons (back, menu, forward).

Those 3 Image buttons should be on the same line (because later I would include this layout to other layouts in the bottom of each layout)

I don't understand what I'm doing wrong, I can't see the all 3 Buttons, and they are not in the same row (same horizontal line)

<?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="horizontal">



<ImageButton
android:id="@+id/buttomMenuBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:background="@drawable/back_button"
android:onClick="onClickButtomMenu"/>

<ImageButton
android:id="@+id/buttomMenuMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu"
android:background="@drawable/HomButton"
android:onClick="onClickButtomMenu"/>

<ImageButton
android:id="@+id/buttomMenuForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forward"
android:background="@drawable/forward_button"
android:onClick="onClickButtomMenu"/>

</LinearLayout>


enter image description here

TWL TWL
Answer

it won't fit because it looks like some of your images are way too big, but we can proportionally weight it so it'll fit.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3">

    <ImageButton
        android:id="@+id/buttomMenuBack"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Back"
        android:background="@drawable/back_button"
        android:onClick="onClickButtomMenu"/>

    <ImageButton
        android:id="@+id/buttomMenuMenu"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Menu"
        android:background="@drawable/HomButton"
        android:onClick="onClickButtomMenu"/>

    <ImageButton
        android:id="@+id/buttomMenuForward"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Forward"
        android:background="@drawable/forward_button"
        android:onClick="onClickButtomMenu"/>

</LinearLayout>

using weightSum and layout_weight, 3 and 1, we ensure they all each take 1/3 of the space in your linearlayout (oh, and layout_width is 0 because layout_weight overrides it)