0

The Layout after running the programI made a GridView with those 2 class and I want to remove space between images in the screenshot, How can remove these white spaces?

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
/>

$ImageAdapter Class:

 public class ImageAdapter extends BaseAdapter{

    Context context;

    public ImageAdapter(Context context){
        this.context=context;

    }
    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView imageView;
        if (view == null){
            imageView=new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(120,120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(3,3,3,3);
        }else {
            imageView = (ImageView) view;
        }

        imageView.setImageResource(images[i]);
        return imageView;
    }

    // images
    int [] images={
            R.drawable.one, R.drawable.two,
            R.drawable.three, R.drawable.four,
            /*R.drawable.fastfive, R.drawable.fastsix,
            R.drawable.fastseven*/
    };
}

$Movies Class:

public class MoviesActivity extends AppCompatActivity {

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

        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
    }
}
  • I'm sure you'll get an answer but I would suggest you to user `recyclerview` instead. – Paresh P. Dec 13 '16 at 13:33
  • use [this](http://stackoverflow.com/questions/15261088/gridview-with-two-columns-and-auto-resized-images) link as reference,it will work. – Nithin Dec 14 '16 at 10:48

3 Answers3

0

Instead of setting height width to 120, 120 you should implement like below. Because app will going to run on many different resolution. So its an Ultimate Solution.

Width of ImageView to be match_parent and height will be (width of device / 2). It will display square image.

Example

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
width = (width / 2);
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT,120));

Apply this Solution inside getView method.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
0

If you will combine android:numColumns="auto_fit" and android:stretchMode="columnWidth" will give you a layout that adjusts for different screen sizes. Much better than fixing the number of columns. See below.

you can also use android:numColumns="auto_fit" instead of android:numColumns="3" here to set according to screens.

<GridView
      android:id="@+id/GVHomepage"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:numColumns="3" 
      android:verticalSpacing="1dp"
      android:horizontalSpacing="1dp"
      android:stretchMode="columnWidth"
      android:background="#edf2f8" />
Deepak Sachdeva
  • 950
  • 8
  • 16
  • add your xml in your post. – Deepak Sachdeva Dec 13 '16 at 14:18
  • Create your custom xml and don't use imageView=new ImageView(context); imageView.setLayoutParams(new GridView.LayoutParams(120,120)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(3,3,3,3); if use want to use above code then use it width device height and width. try this link : https://www.learn2crack.com/2014/01/android-custom-gridview.html – Deepak Sachdeva Dec 13 '16 at 14:39
0

An alternative is to create a RecyclerView with GridLayoutManager set to it.

Something like this

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

public class MoviesActivity extends AppCompatActivity {

    private static final int SPAN_COUNT = 2;

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

        RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
        rv.setLayoutManager(new GridLayoutManager(this, SPAN_COUNT));
        rv.setAdapter(new ImageAdapter(this));
    }
}

Adapter items view holder layout

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

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>

</RelativeLayout>

this will create a list with grids, with a span count of 2.

malmling
  • 2,398
  • 4
  • 19
  • 33