0

In android, I want to add an empty message in the middle of screen when my GridView is empty. But the layout below makes the text in the middle-top of the screen. How can I make the text in the middle also in vertical direction ?

GridView gridview = (GridView) findViewById(R.id.gridview);        
TextView emptyText = (TextView)findViewById(R.id.empty);
gridview.setEmptyView(emptyText);



   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/gridview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:columnWidth="100dp"
       android:numColumns="auto_fit"
       android:verticalSpacing="10dp"
       android:horizontalSpacing="10dp"
       android:stretchMode="columnWidth"
        android:paddingTop="?android:attr/actionBarSize"
       android:gravity="center"

       />


        <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:layout_gravity="center"

        android:gravity="center"
        android:text="No project files found"
        android:textColor="#FFFFFF" />


    </LinearLayout>
J. Doe
  • 75
  • 1
  • 9

4 Answers4

2

Make the parent view as RelativeLayout and then set centerInParent="true" for TextView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey">

    <TextView
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
lgdroid57
  • 699
  • 14
  • 29
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23
0

If you are unable to make the parent view a Relative layout, try this:

<TextView
    android:id="@+id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="No project files found"
    android:textColor="#FFFFFF" />
Bill
  • 4,506
  • 2
  • 18
  • 29
0

Try this:

<TextView
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="No project files found"
        android:textColor="#FFFFFF" />
Sagar Jogadia
  • 1,330
  • 1
  • 16
  • 25
0

I think the best way to place the TextView center vertical is to make the parent layout a RelativeLayout and place the TextView in a LinearLayout centered vertical to the LinearLayout which is centered to the parent layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnWidth="100dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:paddingTop="?android:attr/actionBarSize"
    android:gravity="center"

    />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_centerInParent="true">
    <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="No project files found"
        android:textColor="#FFFFFF" />
</LinearLayout>
</RelativeLayout>
Franklyn
  • 325
  • 2
  • 8
  • You added extra complexity by adding LinearLayout as root of TextView – Krrishnaaaa Jul 15 '16 at 20:20
  • without the LinearLayout, i don't think it is possible to set the TextView center vertical. The LinearLayout was used to Hold the TextView center in parent and to make it possible to set TextView center vertical. – Franklyn Jul 15 '16 at 20:24
  • Instead of doing that, you can add the centerInParent attribute to the TextView – Krrishnaaaa Jul 15 '16 at 20:29
  • oh i think i understand the J.Doe now, the TextView should be centered of the parent view not centered vertical of the parent view. Good one @Krrishnaaaa – Franklyn Jul 15 '16 at 20:31