Praveen Kumar
Praveen Kumar

Reputation: 308

Horizontal and Vertical center to the parent layout

I am creating a user profile screen in Android. In this screen have user profile wallpaper and user profile image. Here the grey background is profile wallpaper and red background is going to profile image. I need to set the red bg layout to the horizontal and vertical center of the grey bg layout..

enter image description here

<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlActivityNotificationsAndProfile">
<include layout="@layout/layout_tool_bar"/>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/profile_wallpaper_container_height"
    android:id="@+id/rlProfileWallpaper"
    android:layout_below="@+id/toolbar">
    <LinearLayout
        android:layout_width="match_parent"
        android:background="#646464"
        android:orientation="vertical"
        android:layout_height="@dimen/profile_wallpaper_height">
        <LinearLayout
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:background="#A52525">
        </LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/tools"
        android:id="@+id/fabEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/base_ten_dp"
        android:layout_marginEnd="@dimen/base_ten_dp"
        android:layout_marginStart="@dimen/base_ten_dp"
        android:scaleType="center"
        android:src="@drawable/add"
        app:elevation="@dimen/priority_1_elevation"
        app:fabSize="1"/>
</RelativeLayout>
</LinearLayout>

Upvotes: 0

Views: 522

Answers (1)

EtherPaul
EtherPaul

Reputation: 423

You can just set your parent LinearLayout's gravity to center (just add 1 line):

<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlActivityNotificationsAndProfile">
<include layout="@layout/layout_tool_bar"/>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/profile_wallpaper_container_height"
    android:id="@+id/rlProfileWallpaper"
    android:layout_below="@+id/toolbar">
    <LinearLayout
        android:layout_width="match_parent"
        android:background="#646464"
        android:orientation="vertical"
        android:layout_height="@dimen/profile_wallpaper_height"
        android:gravity="center">
        <LinearLayout
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:background="#A52525">
        </LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/tools"
        android:id="@+id/fabEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/base_ten_dp"
        android:layout_marginEnd="@dimen/base_ten_dp"
        android:layout_marginStart="@dimen/base_ten_dp"
        android:scaleType="center"
        android:src="@drawable/add"
        app:elevation="@dimen/priority_1_elevation"
        app:fabSize="1"/>
</RelativeLayout>
</LinearLayout>

Upvotes: 1

Related Questions