Search code examples
androidlayouttextviewandroid-gridview

Having trouble in GridView Layout


I have this Layout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context="com.example.anandvardhan.albumview040416.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/grid_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:numColumns="2"
              android:gravity="center"
              android:background="#fff">
    </GridView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

But now I am getting a preview like this:

But now I am getting a preview like this:

So how to select an item at position 1, because it must be beneath the toolbar.

And one more thing.

If my GridView is initially empty then it must show a TextView in centre of the screen stating "It's Empty". How to set it dynamically.

I will be very thankful if anyone could help.


Solution

  • Tell me if you have any question,

        <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        tools:context="com.example.anandvardhan.albumview040416.MainActivity">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    
        <android.support.design.widget.CoordinatorLayout
    
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            >
    
            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
            </android.support.design.widget.AppBarLayout>
    
            <GridView android:id="@+id/grid_view"
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="#fff"
                      android:gravity="center"
                      android:numColumns="2">
            </GridView>
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="@dimen/fab_margin"
                android:src="@android:drawable/ic_dialog_email"/>
    
        </android.support.design.widget.CoordinatorLayout>
    
    </LinearLayout>