0

This issue gave me a painful headache. Basically I have a GridView-based Gallery app. Everything is working fine. But I want to have equal spacing, that is horizontal and vertical, between the images, so it's easier to distinguish one from another.

This is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.gallery.MainActivity">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:columnWidth="80dp"
        android:scrollingCache="true"
        android:smoothScrollbar="true"
        android:horizontalSpacing="3dp"
        android:verticalSpacing="3dp"
        android:padding="0dp"
        android:clipToPadding="false"
        android:clipChildren="true"
        android:numColumns="auto_fit"
        android:adjustViewBounds="true"
        android:gravity="center"
        android:stretchMode="columnWidth"/>

</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private GridView gridView;


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

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

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getApplicationContext(), FullScreenAnimationAdapter.class);
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }
}

And the ImageAdapter's GetView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(ctx);
    imageView.setImageResource((int)getItem(position));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(240, 240));
    return imageView;
}

So I have both horizontalSpacing and verticalSpacing set to 3dp. Yet my application looks like this:

enter image description here

All the images are 1920x1080 and 96 dpi.

Is there something I am doing wrong? Should I change some properties in the xml file, or maybe I should use a different ScaleType for my ImageViews?

dabljues
  • 1,663
  • 3
  • 14
  • 30
  • 2
    Try change: `imageView.setLayoutParams(new GridView.LayoutParams(240, 240));` to `imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, 240));` Hope that helps! – i_A_mok Apr 07 '19 at 15:14
  • God damn, @I_A_Mok, it works! This should be an accepted answer! – dabljues Apr 07 '19 at 15:42

2 Answers2

0

Use a LinearLayout with layout_weight attribute.

sudo code to help you with the idea.

<LinearLayout
layout_orientation="vertical">

<LinearLayout
orientation="horizontal">

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />
</LinearLayout>


<LinearLayout
orientation="horizontal">

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />

<ImageView
layout_weight="1" />
</LinearLayout>

</LinearLayout>
dcanh121
  • 4,665
  • 11
  • 37
  • 84
  • ImageViews are added dynamically, so I don't know if I can do that. First Android app btw, so I'm just asking – dabljues Apr 04 '19 at 19:59
  • You can still use LinearLayout and add views dynamically. Make sure you set LayoutParams and add it to the LinearLayout. – dcanh121 Apr 04 '19 at 20:00
  • Okay, so I can change RelativeLayout to LinearLayout. But if I do only that, it doesn't help – dabljues Apr 04 '19 at 20:05
  • layout_weight attribute is available only in LinearLayout. If you really need this attribute, you need to make the change. – dcanh121 Apr 04 '19 at 20:07
  • I did it. But then how can I specify layout_weight for ImageViews if I don't have them in .xml file? – dabljues Apr 04 '19 at 20:10
  • You would create ImageView in your java code. https://stackoverflow.com/a/4641117 – dcanh121 Apr 04 '19 at 20:13
  • Okay, now the spacing is equal, but the images are displayed like this: https://imgur.com/a/crmG9Zg – dabljues Apr 04 '19 at 20:17
  • Don't use LayoutParams.MATCH_PARENT, instead use LayoutParams.WRAP_CONTENT, or use dp value. – dcanh121 Apr 04 '19 at 20:19
  • I can only pass two ints for like width and height and then a float for weight. Are those implicitly treated as dp and not as pixels or something? – dabljues Apr 04 '19 at 20:27
  • Did you try using WRAP_CONTENT? Yes, they are in px. You should convert them to dp https://stackoverflow.com/a/6327095 – dcanh121 Apr 04 '19 at 20:27
  • I did WRAP_CONTENT and it was no difference between MATCH_PARENT. Now I have something like this (you can see the changed code on the left hand side): https://imgur.com/a/AhD1vLm. Which is kind of strange, cuz now it's better I guess, but it's stil not equal spacing :( – dabljues Apr 04 '19 at 20:32
  • https://gist.github.com/dabljues/1d50c45fad2c145fdccf1b1f3bdd26ed. Those FullScreen files (.java and .xml) I guess are irrelevant, but I've included them as well just in case – dabljues Apr 04 '19 at 20:39
0

Try below mentiond way:

In Activity java:

for set space:
  mGridView.setHorizontalSpacing(4);
  mGridView.setVerticalSpacing(4);

for get Spacing:

 mGridView.getHorizontalSpacing()
 mGridView.getVerticalSpacing()


In XML
<GridView
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"     
android:verticalSpacing="10dp">      
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49