0

I try to align and justify all content of this RelativeLayout in the center.

MainActivity.xml

<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"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/no_internet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:visibility="gone"
    android:background="#e8eaf6"
    android:gravity="center"
    >

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/no_wifi"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#404852"
        android:gravity="center"
        android:textSize="25dp"
        android:text="Internet indisponible"
        />

</LinearLayout>

But after many tries, I have this preview: Preview

Can someone help me please to justify and align the image and the text in center?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Possible duplicate of [Can you center a Button in RelativeLayout?](https://stackoverflow.com/questions/3748255/can-you-center-a-button-in-relativelayout) – Andre Classen Nov 23 '19 at 18:09

3 Answers3

2

You should center the LinearLayout inside the RelativeLayout with the attribute:

android:layout_centerInParent="true"

and set the layout_width and layout_height of the LinearLayout to wrap_content

<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"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/no_internet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        android:background="#e8eaf6"
        android:layout_centerInParent="true"
        android:gravity="center" >

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/no_wifi" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#404852"
            android:gravity="center"
            android:textSize="25dp"
            android:text="Internet indisponible" />

    </LinearLayout>
</RelativeLayout>
forpas
  • 160,666
  • 10
  • 38
  • 76
  • Thank you @forpas, that's what i was looking for. But even I set the **layout_width** and **layout_height** of the **LinearLayout** to **match_parent**, that works fine. – Mohamed Fall Nov 23 '19 at 19:20
0

You have set your LinearLayout to match_parent. So the child views inside LinearLayout will be aligned according to the rules of LinearLayout.

What you should do is to let LinearLayout wrap its contents and center your entire LinearLayout using android:layout_centerInParent="true" android:layout_centerHorizontal="true" android:layout_centerVertical="true"

Pankaj Sati
  • 2,441
  • 1
  • 14
  • 21
0

Just set gravity center in relative layout and wrap content width and height in linear layout

Farhan Fahim
  • 137
  • 2
  • 7