user2881604
user2881604

Reputation: 2370

How to add buttons to grid view?- Android

I am developing a simple android application and i want to add two buttons to grid view. one button should be in the top of the grid view and other one be in bottom. But the i had a problem grid view not display properly. please help me to correct this ,

this is my xml file,

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3b3b3b" >

<LinearLayout  android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="10dp"
        android:text="Back"/>

    <GridView android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth" >  

    </GridView>

    <Button android:id="@+id/btnCapturePicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Camera"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"/>


</LinearLayout>

this is the java file,

public class HorsePictureAlbum extends Activity{

Button btnCapturePicture;
Button btnBack;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_album);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });

    btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
    btnBack = (Button) findViewById(R.id.btnBack);

    btnBack.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    btnCapturePicture.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent camara = new Intent(getApplicationContext(), Camera.class);
            startActivity(camara);
        }
    });
}

}

Upvotes: 0

Views: 1382

Answers (2)

Ramesh Thangaraj
Ramesh Thangaraj

Reputation: 343

Hi Check the below code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btnBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_margin="10dp"
            android:text="Back" />

        <GridView
            android:id="@+id/grid_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:columnWidth="90dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" >
        </GridView>

        <Button
            android:id="@+id/btnCapturePicture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:text="Camera" />
    </LinearLayout>

</LinearLayout>

Upvotes: 2

App Kart
App Kart

Reputation: 926

<?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" >

    <Button
        android:id="@+id/up_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Up button" />

    <GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="50dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/up_button"
        android:layout_above="@+id/down_button">
    </GridView>

    <Button
        android:id="@+id/down_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Down button"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

Upvotes: 0

Related Questions