XML constraint layout: Elements placed on top of each other, fix? - android

My XML code looks like this:
<?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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BottomNavActivity">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#CCB0F0"
android:text="Map"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/idCardView" app:layout_constraintEnd_toEndOf="parent"/>
<android.support.v7.widget.CardView
android:id="#+id/idCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="5dp"
app:cardCornerRadius="4dp" app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="5dp" android:layout_marginBottom="332dp"
app:layout_constraintTop_toBottomOf="#+id/message"
app:layout_constraintBottom_toTopOf="#+id/container">
<fragment android:id="#+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
/>
</android.support.v7.widget.CardView>
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/container"
app:layout_constraintTop_toBottomOf="#+id/idCardView"
app:layout_constraintBottom_toTopOf="#+id/navigation" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="1.0">
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation_menu"/>
</android.support.constraint.ConstraintLayout>
I want the TextView on top, followed by the CardView, followed by a ConstraintLayout, followed by the bottomNavigationView. Somehow the elements are stacked on top of each other like this:
I made sure that every element are constrained to the top or bottom of the next element respectively. Still they end up on top of each other. Is there a better way or a fix to this problem?

In card view you added two constraint on top: remove app:layout_constraintTop_toTopOf="parent" and will work.

You don't need to constraint TextView to Carview, only CardView to TextView.
If TextView width is match_parent, you don't need to set right constraint. Only top and left.
remove CarView to parent top constraint
you don't need to constraint CardView to Container, only container to CardView
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#CCB0F0"
android:text="Map"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<android.support.v7.widget.CardView
android:id="#+id/idCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="5dp"
app:cardCornerRadius="4dp"
tools:layout_editor_absoluteX="5dp"
android:layout_marginBottom="332dp"
app:layout_constraintTop_toBottomOf="#+id/message"
app:layout_constraintLeft_toLeftOf="parent"
>
<fragment android:id="#+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
/>

Related

androidx Recycler View match constraint (0dp) with wrap content behaviour

I have simple recycler view here, what I want is:
when list is short: stick the button below the recycler view
when list is long: stick the button bottom of screen yet recycler view is wrapping properly and able to scroll till bottom
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_user_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toTopOf="#id/btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_bias="0.0"
tools:itemCount="50"/>
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:text="example"
android:background="#00ffff"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#id/rv_user_address"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
When is wrap_content:
<androidx.recyclerview.widget.RecyclerView
android:layout_height="wrap_content"
...
short-list can stick button below properly but button is off screen when list is long
When is constraint:0dp:
<androidx.recyclerview.widget.RecyclerView
android:layout_height="0dp"
...
long list is correct behavior but short-list not stick button below list
I am out of idea. Thanks for helping.
Just add this line:
app:layout_constrainedHeight="true"
to your Recyclerview as:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_user_address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="20dp"
app:layout_constrainedHeight="true" <-- Add this line
app:layout_constraintBottom_toTopOf="#id/btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_bias="0.0"
tools:itemCount="50"/>
Try this out. First align button at the end of the layout then add app:layout_constrainedHeight="true" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toTopOf="#id/btn" in our recycler view. This will make your recycler view to cover all the layout except the button which is at the bottom of the layout. Still if you don't anything you can ask me.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_user_address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#id/btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed"
tools:itemCount="100" />
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#00ffff"
android:gravity="center"
android:orientation="horizontal"
android:text="example"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Change your constraint layout to linear layout.
Add weight-sum to linear layout.
give the desired weights to your recycler view and button.
<?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="match_parent"
android:orientation="vertical"
android:weightSum="10">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_user_address"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
...
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
...
/>
</LinearLayout>
Just make sure the sum of all weights does not exceed your weight-sum.
In the current case. Recycler view will have 90% of parent, while 10% is used by button. Feel free to play around with these number to achieve the desired results.

Android: ScrollView containing two nested ConstraintLayouts, cant fill screen horizontally

I am having an issue with the following XML:
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp">
<View
android:id="#+id/block"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#419E9E9E"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp"
app:layout_constraintTop_toBottomOf="#id/block"></LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/holder"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<TextView
android:id="#+id/holderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="12sp"
android:gravity="center_vertical"
android:maxLines="4"
android:text="Hello World"
android:textColor="#color/colorPrimaryDark"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/holder" />
<Button
android:id="#+id/Button"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Vertically, the layout does what I want: it puts the last layout and fills the screen.
However, when the device is flipped horizontally, the bottom portion (the nested constraintlayout) disappears due to the constraint on itself of app:layout_constraintBottom_toBottomOf="parent"
"How do I fill the screen with a second layout while still having it scroll horizontally ?"
I have attached pics and a demo repository to isolate the problem.
Link: https://github.com/taesookim0412/StackOverflow_Question_Android_NestedConstraintLayouts_ScrollView
I've found a solution. Instead of minHeight, you can use
app:layout_constraintHeight_min="100dp"

How do i center the items of a Recycler View?

i have managed to make a RecyclerView list but am unable to center the text inside. Below are the codes:
RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginEnd="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Item:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/listItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/caviar_dreams"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:paddingBottom="15dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried all properties i could find,alignText, gravity, alignLayout etc
Just change Item layout root element width property from wrap_content to match_parent, so each row item takes full RecyclerView's width and child text would be able to be centered on that width:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" // this is the change you have to make
android:layout_height="wrap_content">
<TextView
android:id="#+id/listItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/caviar_dreams"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:paddingBottom="15dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
In the item layout just use:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>
</LinearLayout>

View with minHeight in ConstraintLayout

I have a ConstraintLayout inside a NestedScrollView. The ConstraintLayout contains a bunch of views but the last View can have a dynamic height to fill up the bottom space if there is any but it also needs to be a minimum height if there is not enough space.
For arguments sake, here is an example.
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_min="1500dp"
android:background="#color/red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
As you can see I have put the ConstraintLayout version in but it doesn't work. Obviously the values are ridiculously large but this is just for testing.
If I don't set fillViewport="true" on the NestedScrollView then ConstraintLayout has a height of 0. When I do set the fillViewport, the ConstraintLayout does not scroll but just fills the screen.
How can I set the View so that it expands to the bottom of the ConstraintLayout which should be as big as the Viewport but if my View is not of the minHeight then we allow scrolling?
I am using version 1.0.2 of the ConstraintLayout library.
What I expect to see is the being all the way to the bottom of the parent but if that size is less than 1500dp then the view scrolls.
I have entered 1500dp like so android:layout_height="1500dp" and the view scrolls accordingly.
UPDATE 1
Seems to be once I put the layout within a FragmentViewPager. The app:layout_constraintHeight_min property isn't respected and it only matches the height of the viewport.
I also tried taking the NestedScrollView out of the fragment and putting the ViewPager inside it but again didn't work.
Add this attribute to the view you'd like to have stretch:
app:layout_constraintHeight_default="spread"
I made a tiny app to demonstrate. No java logic to speak of, but here's the layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#caf">
<TextView
android:id="#+id/one"
android:layout_width="0dp"
android:layout_height="48dp"
android:gravity="center"
android:text="hello world"
android:background="#fff"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#+id/two"/>
<TextView
android:id="#+id/two"
android:layout_width="0dp"
android:layout_height="48dp"
android:gravity="center"
android:text="hello world"
android:background="#eee"
app:layout_constraintTop_toBottomOf="#+id/one"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#+id/three"/>
<TextView
android:id="#+id/three"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="hello world"
android:background="#ddd"
app:layout_constraintHeight_default="spread"
app:layout_constraintHeight_min="300dp"
app:layout_constraintTop_toBottomOf="#+id/two"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
The bottom view stretches to fill the viewport when it is smaller than the remaining available space, and scrolling is impossible:
The bottom view maintains a fixed height when it is larger than the remaining available space, which makes scrolling possible:
I am using com.android.support.constraint:constraint-layout:1.0.2 and this works for me:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/gradient"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_min="1500dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
As the first thing is we have to specify the fixed height to each text view or used wrap content as another option.Secondary inside constraint layout the property app:layout_constraintHeight_default="spread" helps the last view to get the remaining complete space left and if no space left then automatically synced to scroll view.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#caf"
android:padding="16dp">
<TextView
android:id="#+id/one"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="#fff"
android:gravity="center"
android:text="hello world"
app:layout_constraintBottom_toTopOf="#+id/two"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/two"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="#eee"
android:gravity="center"
android:text="hello world"
app:layout_constraintBottom_toTopOf="#+id/three"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/one" />
<TextView
android:id="#+id/three"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="#eee"
android:gravity="center"
android:text="hello world"
app:layout_constraintBottom_toTopOf="#+id/four"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/two" />
<TextView
android:id="#+id/four"
android:layout_width="0dp"
anroid:layout_height="48dp"
android:background="#eee"
android:gravity="center"
android:text="hello world"
app:layout_constraintBottom_toTopOf="#+id/five"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/three" />
<TextView
android:id="#+id/five"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ddd"
android:gravity="center"
android:text="hello world"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_default="spread"
app:layout_constraintHeight_min="300dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/three" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>

How to bring all the content of the layout at centre in ConstraintLayout

I am making a login screen where I have 2 EditText and 2 Buttons. I have placed them in vertical way one below each other. But I want to bring all the content at centre in my layout.
Below is my activity_login.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/loginLayout"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/emailLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/passwordLayout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/loginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/ic_action_user"
android:hint="#string/email"
android:textAppearance="#style/TextLabel" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/passwordLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emailLayout"
app:layout_constraintBottom_toBottomOf="#+id/logi">
<android.support.design.widget.TextInputEditText
android:id="#+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/ic_action_password"
android:hint="#string/password"
android:textAppearance="#style/TextLabel" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/loginSubmit"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/login"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/passwordLayout" />
<Button
android:id="#+id/registerText"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/register"
android:theme="#style/RegistrationButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/loginSubmit" />
</android.support.constraint.ConstraintLayout>
Here is the image of the layout:
I am not able to bring those views at the centre.
First make sure you use an up to date version of the ConstraintLayout (at the time of writing 1.0.2). You can achieve vertical centering by giving the top most element (emailLayout) the app:layout_constraintVertical_chainStyle="packed" attribute.
Besides that you need to make sure that all elements are connected like a chain in the code. I.e. the upper most view (emailLayout) is under constraint of the parent on top and under constraint of the next sibling (passwordLayout) on the bottom.
The second view (passwordLayout) needs to be under constraint of the sibling before it at the top (emailLayout) and under constraint of the next sibling (loginSubmit) at the bottom and so on...
The last view (registerText) has a top constraint to the sibling before as well but has a bottom constraint to the bottom of the parent.
Hint: android:orientation="vertical" is useless in a ConstraintLayout. You can leave this out.
EDIT:
Here a minimum code example of vertical centering with the ConstraintLayout:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ff0000"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/view2"/>
<View
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#00ff00"
app:layout_constraintTop_toBottomOf="#+id/view1"
app:layout_constraintBottom_toTopOf="#+id/view3"/>
<View
android:id="#+id/view3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#0000ff"
app:layout_constraintTop_toBottomOf="#+id/view2"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
Resulting in:
I am using packed chaining in my ConstraintLayout. So, yours should be like this:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/loginLayout"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/emailLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/passwordLayout"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_chainStyle="packed">
<android.support.design.widget.TextInputEditText
android:id="#+id/loginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/ic_action_user"
android:hint="#string/email"
android:textAppearance="#style/TextLabel" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/passwordLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/emailLayout"
app:layout_constraintBottom_toTopOf="#+id/loginSubmit"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/ic_action_password"
android:hint="#string/password"
android:textAppearance="#style/TextLabel" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/loginSubmit"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/login"
app:layout_constraintTop_toBottomOf="#+id/passwordLayout"
app:layout_constraintBottom_toTopOf="#+id/registerText"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="#+id/registerText"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/register"
android:theme="#style/RegistrationButton"
app:layout_constraintTop_toBottomOf="#+id/loginSubmit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
The resultant layout should be as follows:

Categories

Resources