How to place button next to two text fields

Asked

Viewed 1,426 times

0

I want to put the button next to Edit text, I’ve made several attempts with layout_toleftof and Weight layout but the button ends up disappearing, or is in the same place. My xml is like this:

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:digits="1234567890.,"
    android:ems="10"
    android:id="@+id/editTextComercial"
    android:hint="Diametro Comercial"
    android:maxLines="1" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:inputType="numberDecimal"
    android:digits="1234567890.,"
    android:ems="10"
    android:id="@+id/editTextVC"
    android:hint="Velocidade de Corte"
    android:maxLines="1" />

<Button
    android:text="Calcular "
    android:layout_marginTop="5dp"
    android:layout_height="wrap_content"
    android:id="@+id/buttonCalc"
    android:layout_width="match_parent"/>

<TextView
    android:text="TextView"
    android:visibility="invisible"
    android:gravity="center"
    android:textSize="16dp"
    android:textStyle="bold"
    android:layout_marginTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textViewResultRPM" />

botão embaixo dos campos de texto

  • Use Linearlayout

1 answer

2


If you want to do it this way:

Layou img

I have two solutions, one with Linear another with relative layour, follows the Linear, in it I created other layouts inside, one with vertical orientation to put the Edit text , and another horizontal to separate this layout from the Edit text with the button by Weight.

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1"
        >
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:digits="1234567890.,"
            android:ems="10"
            android:id="@+id/editTextComercial"
            android:hint="Diametro Comercial"

            android:maxLines="1" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:inputType="numberDecimal"
            android:digits="1234567890.,"
            android:ems="10"
            android:id="@+id/editTextVC"
            android:hint="Velocidade de Corte"
            android:maxLines="1" />

    </LinearLayout>
    <Button
        android:text="Calcular "
        android:layout_marginTop="5dp"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/buttonCalc"
        android:layout_weight="2"

        />
</LinearLayout>




<TextView
    android:text="TextView"
    android:visibility="invisible"
    android:gravity="center"
    android:textSize="16dp"
    android:textStyle="bold"
    android:layout_marginTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textViewResultRPM" />

In relative layout it was a little more complicated, however the solution is simpler, just add: android:layout_toEndOf="@id/editTextComercial" android:layout_alignBottom="@id/editTextVC" to be able to align the button at the end of the width and bottom of Edit text , of course a value should be added in the width of Edit text.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:digits="1234567890.,"
    android:ems="10"
    android:id="@+id/editTextComercial"
    android:hint="Diametro Comercial"
    android:maxLines="1"
    android:layout_marginTop="10dp"
    android:layout_alignParentStart="true" />

<TextView
    android:text="TextView"
    android:visibility="invisible"
    android:gravity="center"
    android:textSize="16dp"
    android:textStyle="bold"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:id="@+id/textViewResultRPM"
    android:layout_below="@+id/editTextVC"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:digits="1234567890.,"
    android:ems="10"
    android:id="@+id/editTextVC"
    android:hint="Velocidade de Corte"
    android:maxLines="1"
    android:layout_below="@+id/editTextComercial"
    android:layout_alignParentStart="true" />

<Button
    android:text="Calcular "
    android:layout_height="wrap_content"
    android:id="@+id/buttonCalc"
    android:layout_width="match_parent"
    android:layout_toEndOf="@id/editTextComercial"
    android:layout_alignBottom="@id/editTextVC"
    android:layout_alignTop="@+id/editTextComercial" />

  • 1

    worked, was using linear, but n thought about it of creating several linear one inside the other, I need to study more xml. vlw

  • Anything, we’re there to help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.