1

I know that it may be a silly question...but I'm new to android...I have an gridview layout in my project and I used "android developer" code for writing the code...so the xml code of layout is like this :

   <?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

now I want to add a button to the gridview but I can't do it with drag and drop...I've tried to write the xml code of adding button but I've got this error : "Error in an XML file: aborting build." can anyone help me with this problem ?

1
  • 1
    Do you want a button in each cell, or above it, below it? Commented Jul 14, 2013 at 10:55

1 Answer 1

1

If you want the Button to be below the GridView, use something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridFriends"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clipChildren="true"
        android:columnWidth="100dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:scrollbars="none"
        android:stretchMode="columnWidth" >
    </GridView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingLeft="9dp"
        android:paddingRight="9dp"
        android:paddingTop="5dp" >

        <ImageButton
            android:id="@+id/imgbtnDemo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:background="@null"
            android:gravity="center"
            android:src="@drawable/ic_contact_picture" >
        </ImageButton>
    </LinearLayout>

</LinearLayout>

If you want a Button in every cell in the GridView, you will have to use a Custom GridView that uses an Adapter. An exmaple XML snippet for the custom cell in the GridView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" >

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imgProfilePicture"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@null" />

        <ImageButton
            android:id="@+id/imgbtnDemo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:background="@null"
            android:gravity="center"
            android:src="@drawable/ic_contact_picture" >
        </ImageButton>
    </FrameLayout>

</RelativeLayout>

NOTE: In the XMLS snippets in the post, I am using ImageButton's. Change them and any necessary attributes to that of a Button. It is merely an illustration. You should be able to connect the dots though. ;-)

if you are familiar with the concept of custom ListViews, with a few modifications, you will be able to implement a custom GridView too. If you are not familiar with custom ListViews or GridViews, follow this tutorial to see how to create a custom GridView: http://www.coderzheaven.com/2012/02/29/custom-gridview-in-android-a-simple-example/.

Or use this Google Search for more tutorials on custom GridView's.

And this is a link to an answer of mine on SO. It has the complete solution. Use what logic suits your purpose.

Sign up to request clarification or add additional context in comments.

1 Comment

@eng.m.a: I am glad it was. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.