Search code examples
javaandroidandroid-layoutimageviewandroid-linearlayout

Remove space between childrens in a LinearLayout


I'm developing an Android game for fun and I can't remove the space between my ImageViews in my LinearLayouts. There isn't a defined number of ImageViews in these layouts, that's why I don't deal with XML files. So I've created every ImageViews in my Java code and I've added them in my Layout. But there is blank spaces between each of them : Spaces between ImageViews (Blue/red/green squares are ImageViews). I started with a GridLayout to do that but I can't remove that blank space so I try with some LinearLayouts (Horizontal) into other LinearLayout (Vertical).

I've tried a lot of thing like setMargin and Padding to 0, create a LayoutParams to remove them, etc .. but it didn't work.

Thank you !

EDIT : here's my code

iv = new ImageView[cm.getNbRow()][cm.getNbColumn()];    //cm is the map object

LinearLayout[] linearTab = new LinearLayout[cm.getNbColumn()];
for(int i=0; i<cm.getNbRow(); i++) {
    linearTab[i] = new LinearLayout(this);
    linearTab[i].setOrientation(LinearLayout.HORIZONTAL);
}

for(int i=0; i<cm.getNbRow(); i++) {
    for(int ii = 0; ii < cm.getNbColumn(); ii++) {
        if(cm.getMap()[i][ii] == 1) {
            iv[i][ii] = new ImageView(this);
            iv[i][ii].setImageResource(R.drawable.wall);
        } else if(cm.getMap()[i][ii] == 2) {
            p = new Player(i, ii, this, cm);
            iv[i][ii] = new ImageView(this);
            iv[i][ii].setImageResource(R.drawable.player);
        } else if(cm.getMap()[i][ii] == 3) {
            iv[i][ii] = new ImageView(this);
            iv[i][ii].setImageResource(R.drawable.stop);
        } else {
            iv[i][ii] = new ImageView(this);
            iv[i][ii].setImageResource(R.drawable.path);
        }
        linearTab[i].addView(iv[i][ii]);        //Horizontal LinearLayout
    }
    ll.addView(linearTab[i]);                   //Vertical LinearLayout
}

EDIT 2 : Android Studio adds transparents borders to the ImageViews (don't know why tho) but I've solved it by editing the generated image and coloring the transparents parts.


Solution

  • When you import an image, you need to make sure that the option "Trim" is set to "yes" and "Padding" is set to 0%. That should ensure that the image doesn't have a transparent region around it.