-3

I'm making an app where I dynamically make tablerows. Each tablerow includes 3 ImageViews and one TextView, with textview there's not any problem, I simyply use textview.setGravity(Gravity.Center_Vertical), but there's a problem with ImageViews. How to center them (Vertically and Horizontally) ?

Here's the picture wchich shows what I want to achieve: enter image description here

here's the class wchich adss everything to TableLayout:

public class Obiekt {
    int moje;
    TableRow tr = new TableRow(MainActivity.this);
    ImageView iv = new ImageView(MainActivity.this);
    ImageView sett = new ImageView(MainActivity.this);
    ImageView rin = new ImageView(MainActivity.this);

    public Obiekt(int obraz, int obraz2, int obraz3, int dzwiek, String tekst) {
        moje = sound.load(MainActivity.this, dzwiek, 1);
        iv.setImageResource(obraz);
        //iv.setLayoutParams(rowparams);
        tr.addView(iv);
        // TEXTVIEW settings
        TextView tv = new TextView(MainActivity.this);
        tv.setText(tekst);
        tv.setTypeface(tf);
        tv.setTextSize(rozmiar);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setTextColor(Color.parseColor("#FFFFFF"));
        tr.addView(tv);
        // ImageView RIGHT
        sett.setImageResource(obraz2);
        tr.addView(sett);
        // ImageView RING
        rin.setImageResource(obraz3);
        tr.addView(rin);
        //tr.setLayoutParams(rowparams1);
        tl.addView(tr);
        View wiew = new View(MainActivity.this);
        wiew.setBackgroundResource(R.drawable.divide);
        tl.addView(wiew);
        tv.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                play();
            }
        });
    }

    public void hide() {
        tr.setVisibility(10);
    }

    public void size() {
        iv.getLayoutParams().height=rozmiar;
        iv.getLayoutParams().width=rozmiar;
        sett.getLayoutParams().width=rozmiar;
        sett.getLayoutParams().height=rozmiar;
        rin.getLayoutParams().height=rozmiar;
        rin.getLayoutParams().width=rozmiar;
    }

    public void play() {
        sound.play(moje, 1, 1, 0, 0, 1);
    }
}
Androider
  • 435
  • 1
  • 9
  • 22

3 Answers3

1

Set your imageviews "SCALE TYPE" to FitXY like for example your imageview is imageView than imageView.setScaleType(ScaleType.FIT_XY);

Parin Parikh
  • 385
  • 1
  • 6
0

try like this

ImageView imgView=new ImageView(this);
imgView.setImageResource(R.drawable.yourimage);
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
0

Have you tried adding the ImageView inside a RelativeLayout?

Set the RelativeLayout to have LayoutParams that match the space you want to draw within (the full size of the cell), and then when adding the ImageView to the RelativeLayout, add it with RelativeLayout.center_in_parent

biddulph.r
  • 5,226
  • 3
  • 32
  • 47