androidandroid-drawablelayerdrawable

Multiple images single view mispositioned Android


I am following the answer given here Array of images stacked next to each other, using LayerDrawable.(Displaying multiple image in single imageview) . My images are 38x38 sized.

This is my code:

Drawable[] layers = new Drawable[2];
layers[0]=cActivity.getResources().getDrawable(R.drawable.white_pawn);
layers[1]=cActivity.getResources().getDrawable(R.drawable.black_pawn);

LayerDrawable pawns = new LayerDrawable(layers);
pawns.setLayerInset(0, 0, 0, 42, 0);
pawns.setLayerInset(1, 42, 0, 42, 0);
((ImageView)v.findViewById(R.id.baseImage)).setImageDrawable(pawns);

But that's what happens: layers misplaced

Any help?


Solution

  • The only difference with the question you are referring is that you are not setting the gravity for drawables. IIRC Drawables by default try to fill the the layer.

        layers[0].setGravity(Gravity.LEFT);
        layers[1].setGravity(Gravity.LEFT);