Diogo Garcia
Diogo Garcia

Reputation: 594

Android Studio ImageView

I'm trying to create, resize and add an imageView and I've been looking for a solution to this all over but nothing works properly, every time I set any kind of Param the image doesn't appear anymore or the app crashes, I'm using Android Studio 1.0.1 and this are the methods I've tried:

This one the image disapear

TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);

LinearLayout.LayoutParams imgPar = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
imgPar.setMargins(5,5,5,5);

imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);

This one the image disapear

TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);

FrameLayout.LayoutParams imgPar = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
imgPar.setMargins(5,5,5,5);

imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);

This one the app crashes

TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);

ViewGroup.LayoutParams imgPar = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
//imgPar.setMargins(5,5,5,5); //not possible to set margin

imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);

This one the app crashes

TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);

imageView.getLayoutParams().width = 50;
imageView.getLayoutParams().height = 50;

tableInfoCont.addView(imageView);

That is it, tryed all this methods... Any idea what am I missing here?

Upvotes: 0

Views: 4667

Answers (2)

Tor_Gash
Tor_Gash

Reputation: 164

Take into consideration that in such expressions you specify height and width in pixels, not in dp. Your picture may disappear just because it's wider or higher than your tablerow is (check your tablerow's layout parameters). Set your image's width and height to match_parent and check up how it feels.

Try using imageView.requestLayout() after you commit some changes in LayoutParams. This makes the View re-read the layout for changes.

Upvotes: 0

throws_exceptions_at_you
throws_exceptions_at_you

Reputation: 1746

Keep in mind that is discouraged to create Layout in code.

Also try to let a simple ImageView show up first before you go with a TableView.

Try this code :

TableLayout tableLayout = new TableLayout(this);
TableRow tableRow = new TableRow(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.simpleAvatar);
tableRow.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
tableRow.addView(imageView);
tableLayout.addView(tableRow);
setContentView(tableLayout);

Upvotes: 1

Related Questions