1

I'm new to android and the positioning elements are way different than as it is for web.
So What I want to do is this:

Login: ______ Password: _____ 

What I can get is this, by setting the linearLayout to vertical orientation:

Login: __ Password: __ 

Or this, with horizontal orientation:

Login:_____Password:_____ 

How may I mix it and achieve what I need?

Current code:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="wrap_content" android:orientation="horizontal" android:id="@+id/mainActivity" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Login:" android:textSize="14pt"/> <EditText android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/loginText"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Senha:" android:textSize="14pt"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/passText"/> </LinearLayout> 
1

5 Answers 5

2

Nest your linearlayouts. Have a vertical top level linear layout, with 2 horizontal linear layouts inside, each of which does one line of your output.

Sign up to request clarification or add additional context in comments.

1 Comment

Extremely normal. Layouts within layouts are common, and necessary for many designs. If you go too deep its a bad idea, but 2 or 3 levels is fine. If you find yourself at 5 or 6, you may want to move to something else.
1

First of all you should use sp instead of pt for android:textSize.

You can achieve your expected layout using Nested LinearLayout as follows:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/mainActivity" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Login:" android:textSize="14pt" /> <EditText android:id="@+id/loginText" android:layout_width="wrap_content" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Senha:" android:textSize="14pt" /> <EditText android:id="@+id/passText" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> 

However a good practice would be to use ConstraintLayout, which reduces the view hierarchy, as follows:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:id="@+id/mainActivity" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login:" android:textSize="14pt" app:layout_constraintBottom_toBottomOf="@+id/loginText" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/loginText" android:layout_width="wrap_content" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/textView2" app:layout_constraintEnd_toEndOf="@+id/textView2" app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Senha:" android:textSize="14pt" app:layout_constraintBaseline_toBaselineOf="@+id/passText" app:layout_constraintStart_toStartOf="@+id/textView" /> <EditText android:id="@+id/passText" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView2" /> </android.support.constraint.ConstraintLayout> 

2 Comments

The good practice would be to use ConstraintLayout. Also use sp instead of pt for textSize
@PlayHardGoPro check the udpated answer
1

try this:

you can use linear layout for each block like text view and edit text and set linear layout orientation horizontal

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/linear_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Username" android:paddingLeft="10dp" android:textColor="@color/black" android:textSize="18dp" /> <EditText android:id="@+id/user_et" android:layout_width="wrap_content" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Password:" android:paddingLeft="10dp" android:textSize="18dp" android:textColor="@color/black"/> <EditText android:id="@+id/password_et" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@color/black" android:textColor="@color/white" android:text="Login" android:textSize="18dp" android:textAllCaps="false" android:layout_marginTop="10dp"/> 

Comments

1

It's best practice if you can use ConstraintLayout to decrease layout hierarchy. Otherwise, you can use nested LinearLayout.

Comments

1

Usually on mobile you do not need to use label like in web. setHint on EditText should be enough to make the purpose of the fields clear for your users.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="wrap_content" android:orientation="horizontal" android:id="@+id/mainActivity" tools:context=".MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="match_parent" android:hint="Username" android:inputType="textEmailAddress" android:id="@+id/usernameEditText"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" android:id="@+id/passwordEditText"/> </LinearLayout> 

You can also use the material text field if you want something like a label to be always there, https://material.io/design/components/text-fields.html

<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/usernameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:hint="Username"/> </android.support.design.widget.TextInputLayout> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.