John R
John R

Reputation: 2064

How to show 3x3 gridview of images in android

I am taking help from this link: Source to create image gallery. I want 3x3 sized image gallery. But I dont know how to do this. Please help me.

Xml Code-

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

Activity code-

   public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      GridView gridview = (GridView) findViewById(R.id.gridview);
      gridview.setAdapter(new ImageAdapter(this));
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}

Upvotes: 0

Views: 4171

Answers (1)

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

add following code on XML:

android:numColumns="3"

or use following code on java code:

gridView.setNumColumns(3);

if you want have fix row in your GridView, you need dynamically set the layout params to your ImageView. see This for getting your answer

Upvotes: 1

Related Questions