Dave
Dave

Reputation: 1492

I can't align editText in Android

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

<TextView
    android:id="@+id/tv_layout"
    android:text="@string/calc"
    android:textSize="@dimen/big_text_size"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/first_number"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"/>

<EditText
    android:id="@+id/second_number"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"/>

<EditText
    android:id="@+id/ris"
    android:text="="
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"/>

<Button
    android:id="@+id/btn_sum"
    android:text="+"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/btn_sub"
    android:text="-"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 </LinearLayout>

I would that editTexts with id "first_number", "second_number" and "ris" were on the same line and the two buttons on another line. I tried to do this using:

android:layout_width="0dp" and android:layout_weight="1"

But I didn't have success.

How can I do that? Thanks.

Upvotes: 0

Views: 62

Answers (2)

Pallav
Pallav

Reputation: 165

Try to put all the edit texts inside another Linear layout with horizontal orientation and the two buttons inside another Linear layout with horizontal orientation.

<?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="vertical">
 <TextView
    android:id="@+id/tv_layout"
    android:text="@string/calc"
    android:textSize="@dimen/big_text_size"
    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="1">

        <EditText
    android:id="@+id/first_number"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"/>

<EditText
    android:id="@+id/second_number"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"/>

<EditText
    android:id="@+id/ris"
    android:text="="
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"/>
    </LinearLayout>

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

        <Button
            android:id="@+id/btn_sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="+" />

        <Button
            android:id="@+id/btn_sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="-" />
    </LinearLayout>

</LinearLayout>

Upvotes: 0

mrtn
mrtn

Reputation: 889

Something like this is what you want to do:

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

    <TextView
        android:id="@+id/tv_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/calc"
        android:textSize="@dimen/big_text_size" />

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

        <EditText
            android:id="@+id/first_number"
            android:layout_width="0dp"
            android:layout_weight="0.3"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:inputType="number" />

        <EditText
            android:id="@+id/second_number"
            android:layout_width="0dp"
            android:layout_weight="0.3"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:inputType="number" />

        <EditText
            android:id="@+id/ris"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:inputType="number"
            android:text="=" />

    </LinearLayout>

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

        <Button
            android:id="@+id/btn_sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="+" />

        <Button
            android:id="@+id/btn_sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="-" />
    </LinearLayout>

</LinearLayout>

Since I don't know how wide the edittext's should be I set them to 0.3 of the maximal width. You have to figure out what values are the best for you.

Upvotes: 1

Related Questions