Ast
Ast

Reputation: 371

Strange behavior with image button in a relative layout

I am experiencing a strange behavior with the alignment of some ImageButtons within a relative layout. I have three image buttons, which together make up a circle. the following is the preview in Android Studio Android studio preview

everything works well in Android studio and in the emulator, with every screen resolutions. but when I test in a physical device the results are different and not good. the following is the result: device result

as you can see the button "add" is stretched horizzontally, but I can't understand why... the images have the same dimensions 200x200 px and the upper one is 400x200 px The following is the code of the layout, please help me!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:backgroundTint="#00FFFFFF"
android:backgroundTintMode="add"
android:padding="0dp"
tools:context=".dashboard">


<TextView
    android:id="@+id/nome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="5sp"
    android:text="@string/utente"
    android:textSize="18sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/nome"
    android:layout_marginStart="5sp"
    android:gravity="center"
    android:text="@string/pazientiInCoda"
    android:textSize="18sp"
    android:textStyle="italic" />

<TextView
    android:id="@+id/txtCoda"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/nome"
    android:layout_centerHorizontal="true"
    android:layout_marginStart="5dp"
    android:layout_toEndOf="@id/textView2"
    android:gravity="center"
    android:text="0"
    android:textAlignment="center"
    android:textColor="@color/colorAccent"
    android:textIsSelectable="false"
    android:textSize="18sp"
    android:textStyle="italic" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:padding="0dp"
    >

    <!--   divano   -->
    <ImageButton
        android:id="@+id/btnSala"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:onClick="vaiInSala"
        android:padding="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/btncoda_green"
        android:contentDescription="@string/sala"
         />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnSala"
        android:layout_centerHorizontal="true">
        <!--   add   -->
        <ImageButton
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/addToCue"
            android:onClick="scanQR"
            android:padding="0dp"
            android:scaleType="fitXY"
            android:src="@drawable/btnaddcoda_green"
             />

        <!--   msg   -->
        <ImageButton
            android:id="@+id/btnDoc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/button"
            android:onClick="vaiAiDocumenti"
            android:padding="0dp"
            android:scaleType="fitXY"
            android:src="@drawable/btnmsg_green"
            android:contentDescription="@string/messaggi"
             />
    </RelativeLayout>
</RelativeLayout>

Upvotes: 0

Views: 153

Answers (1)

Omid.N
Omid.N

Reputation: 874

Replace the two ImageButtons in a LinearLayout so that you can allocate each of them half the screen (Look at android:layout_weight and android:weightSum):

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnSala"
    android:orientation="horizontal"
    android:weightSum="2">
    <!--   add   -->
    <ImageButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/addToCue"
        android:onClick="scanQR"
        android:padding="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/btnaddcoda_green"
        android:layout_weight="1" />

    <!--   msg   -->
    <ImageButton
        android:id="@+id/btnDoc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/button"
        android:onClick="vaiAiDocumenti"
        android:padding="0dp"
        android:scaleType="fitXY"
        android:src="@drawable/btnmsg_green"
        android:contentDescription="@string/messaggi"
        android:layout_weight="1"/>
</LinearLayout>

Upvotes: 1

Related Questions