Home > Mobile >  Buttons is not showing properly in XML android
Buttons is not showing properly in XML android

Time:11-17

Hey I am working on an application Where I have two Buttons "Call Now" and "Chat Now". Generally We only show one button "Chat Now". If User Provide check "show call button" option then we will show both buttons. Otherwise only Chat Now Button will show. But the problem is in design if user enable call button then it will look like this

like this.

How it should look

but the problem is that when i show the both it looks good but when i show only the Chat Now button it look weird while it should be. like this

How it is looking

XML CODE

<LinearLayout
    android:id="@ id/callNowLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:layout_marginTop="300dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@ id/callNow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:backgroundTint="#FF5722"
        android:padding="10dp"
        android:text="Call Now"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


    <Button
        android:id="@ id/chatNow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:padding="10dp"
        android:text="Chat Now"
        android:backgroundTint="#FF5722"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@ id/callNow" />


</LinearLayout>

CodePudding user response:

Try this XML and use if else conditions as per your code. As per conditions use visibility programmatically.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ id/callNowLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="300dp"
    android:orientation="horizontal"
    android:weightSum="2">

    <Button
        android:id="@ id/callNow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:backgroundTint="#FF5722"
        android:text="Call Now"
        android:textAllCaps="false"
        android:textColor="@color/white" />


    <Button
        android:id="@ id/chatNow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:backgroundTint="#FF5722"
        android:text="Chat Now"
        android:textAllCaps="false"
        android:textColor="@color/white" />


</LinearLayout>

CodePudding user response:

Try this

<LinearLayout
    android:id="@ id/callNowLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:layout_marginTop="300dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@ id/callNow"
         android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:backgroundTint="#FF5722"
        android:padding="10dp"
        android:text="Call Now"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


    <Button
        android:id="@ id/chatNow"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:padding="10dp"
        android:text="Chat Now"
        android:backgroundTint="#FF5722"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@ id/callNow" />


</LinearLayout>
  • Related