Position Button in the center of a Linearlayout with layout_gravity

Asked

Viewed 2,244 times

3

I’d like to know why I can’t center the button, I’d like to center vertically and horizontally, but it seems to center only center horizontally in the 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>

3 answers

2


Contrary to popular belief, a Linearlayout, match_parent does not make it occupy all the Parent. The old name, fill_parent, in that sense, it was even worse.

So being and wanting to use the attribute android:layout_gravity 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>

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"/>
  • Good! At the time I answered I did not remember this alternative.

-1

You can do it using Relativelayout. But if you really want to do with Linearlayout you can use android:layout_marginTop to center, but this approach may give 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.