Position Button in the center of a LinearLayout with layout_gravity

2

I would like to know why I can not center the button, I would like to center vertically and horizontally, but it seems to me center only centered horizontally on vertical layout and vice versa.

<?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"
    tools:context="com.example.android.sayhello.ActivityPrincipal">

    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:text="ok"
        android:layout_gravity="center"
        />

</LinearLayout>
    
asked by anonymous 03.02.2018 / 15:09

3 answers

2

Contrary to what you might think, in a LinearLayout, match_parent does not make it occupy all parent . The old name, fill_parent , in that sense, was even worse.

Therefore, and wanting to use the android:layout_gravity attribute, you must include the Button between two "auxiliary" views with a layout_weight to indicate that they must occupy all available space above and below the 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">

    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:text="ok"
        android:layout_gravity="center_horizontal"
        />
    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

However this effect is easier to achieve using a RelativeLayout with android:layout_centerInParent="true" :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_centerInParent="true"
        android:text="ok" />
</RelativeLayout>
    
03.02.2018 / 17:50
1

To leave the button centered on the screen, you can put a gravity on the parent:

<?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:gravity="center"
  tools:context="com.example.android.sayhello.ActivityPrincipal">

<Button
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:text="ok"/>
    
05.03.2018 / 17:28
-1

You can do this using RelativeLayout. But if you really want to do with LinearLayout you can use android: layout_marginTop to center, but this approach can be a problem in the future.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginTop="218dp"
        android:text="ok" />
</RelativeLayout>
    
03.02.2018 / 15:48