Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Align view center to bottom of other view

Tags:

A picture tells more than a lengthy speech : enter image description here

I want to align vertically the center of the red part with the middle of the black part. I have no constraint of container (RelativeLayout, FrameLayout, LinearLayout oO ).

I tried a View with height of 0dp aligned to the bottom of the black view and alignBaseline of red view to it, but it doesn't work...

Thanks for your help !

like image 208
JosselinTD Avatar asked Jul 29 '15 09:07

JosselinTD


People also ask

How do I center a view in another view?

If your views in RelativeLayout follow these steps: 1- wrap your view that wants to be aligned in the center of target view in Framelayout. 2- move all layout attributes to FrameLayout. 3- set layout_align_top and layout_align_bottom (or start and end if horizontal) to target view.

How do I fix the bottom layout on my Android?

You can set the layout_height="0dp" of your header, footer and ScrollView and define a layout_weight . Just play around with the values until you find out which works best. The resulting heights of header and footer would dynamically change with the screensize.

How do I center a view in relative layout Android?

Below that, the layout_height=0 and layout_weight=1 attributes on the RelativeLayout cause it to take up all the remaining space. You can then center the button in the RelativeLayout . You can play with padding on the button to get it to the size you want.

How to align views at the bottom of the screen in Android?

How to align views at the bottom of the screen in Android? This example demonstrate about How to align views at the bottom of the screen in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.java

How do you position a view against another view in XML?

The value for each layout property is either a boolean to enable a layout position relative to the parent RelativeLayout or an ID that references another view in the layout against which the view should be positioned. In your XML layout, dependencies against other views in the layout can be declared in any order.

What is relative layout in Android?

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.

How to center a view along the edge of a view?

This is also possible using the ConstraintLayout. Similar to how aligning the start/end of a view to the parent centers it within the parent, we can use that concept to center along the edge of a View. app:layout_constraintTop_toBottomOf="@id/top_view" app:layout_constraintBottom_toBottomOf="@id/top_view"


2 Answers

This is also possible using the ConstraintLayout. Similar to how aligning the start/end of a view to the parent centers it within the parent, we can use that concept to center along the edge of a View.

The key to this layout is two constraints on our bottom view:

app:layout_constraintTop_toBottomOf="@id/top_view" app:layout_constraintBottom_toBottomOf="@id/top_view" 

By constraining both the top/bottom of the lower view to the bottom of the upper view, the layout will adjust to center it along that bottom. Here is the full code and screenshot of the blueprint:

<?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/top_view"         android:layout_width="0dp"         android:layout_height="0dp"         android:background="@color/green"         app:layout_constraintDimensionRatio="1:1"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />      <View         android:id="@+id/bottom_view"         android:layout_width="58dp"         android:layout_height="58dp"         android:background="@color/red"         app:layout_constraintBottom_toBottomOf="@id/top_view"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@id/top_view" />  </android.support.constraint.ConstraintLayout> 

enter image description here

like image 132
AdamMc331 Avatar answered Sep 20 '22 03:09

AdamMc331


Android now supports layout anchors with the CoordinatorLayout:

<android.support.design.widget.CoordinatorLayout 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/black_view"         android:layout_width="match_parent" android:layout_height="@dimen/black_height"/>      <View android:id="@+id/red_view"         android:layout_width="@dimen/red_width" android:layout_height="@dimen/red_height"         app:layout_anchor="@id/black_view" app:layout_anchorGravity="bottom|center"/>  </android.support.design.widget.CoordinatorLayout> 

If you change the size of the views in your Java code, the anchor will persist as well.

like image 36
Bryan Avatar answered Sep 22 '22 03:09

Bryan