Home > Software engineering >  LinearLayout elements sizes XML
LinearLayout elements sizes XML

Time:03-17

I can make something like this in my layout but I want to put the ok button at right. But when I try to make this I have this instead of.

here is my code(second image).

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00cc33"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    
    <LinearLayout
        android:background="#0033cc"
        android:layout_width="fill_parent"
        android:layout_height="95dip"
        android:orientation="horizontal">

        <EditText
            android:text="Enter text"
            android:layout_width="match_parent"
            android:layout_height="95dip"/>

        <Button
            android:text="OK"
            android:layout_height="95dip"
            android:layout_width="95dip"/>
        
    </LinearLayout>
    
</LinearLayout>

I hope it's possible to make this using only XML?

CodePudding user response:

Maybe, you need to use RelativeLayoutand specify attributes related to disposition of elements. Try to check here: https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams

CodePudding user response:

I think the constraint layout is the best if you use xml. The 0 dp with constraints can help a lot

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <Button
        android:id="@ id/button"
        android:layout_width="95dp"
        android:layout_height="95dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Ok" />

    <EditText
        android:id="@ id/editTextTextPersonName"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:ems="10"
        app:layout_constraintTop_toTopOf="@id/button"
        app:layout_constraintStart_toEndOf="@id/button"
        app:layout_constraintBottom_toBottomOf="@id/button"
        app:layout_constraintEnd_toEndOf="parent"
        android:inputType="textPersonName"
        android:text="Name" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • Related