Android Studio ImageView - android

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?

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);

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.

Related

Changing android dynamic image size programmatically

I'm trying to resize a dynamic image in android but I can't, maybe someone can help me. I have searched in the web but didn't find anything useful...
This is the code I'm using to create and place the image:
TableRow row = new TableRow(this);
row.setOnClickListener(this);
ImageView iv = new ImageView(this);
iv.setImageBitmap(getImage());
TableRow.LayoutParams lp = new TableRow.LayoutParams(10, 10);
iv.setLayoutParams(lp);
row.addView(iv);
A possible solution, based on your snippet:
//...
ImageView iv = new ImageView(this);
iv.setScaleType(ScaleType.CENTER_INSIDE);
iv.setImageBitmap(getImage());
TableRow.LayoutParams lp = new TableRow.LayoutParams(10, 10);
row.addView(iv, lp);

Adding images to one line in layout with ratio

I am doing an android app that have images which are located besides each other,
every image has its own width, I want to add these images in one line to fit the screen width,
with the same ratio for every one
I wrote code to add this images but still to know how to set the width programmatically
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
LinearLayout my_root = (LinearLayout) findViewById(R.id.root_layout);
LinearLayout child = new LinearLayout(this);
child.setOrientation(LinearLayout.HORIZONTAL);
child.addView(view);
child.addView(view1);
child.addView(view2);
my_root.addView(child);
only image 1 and 2 appear but the third didn't appear because the width of screen finished
Any help !!
Thank you :)
You must need to use weight parameter to do this.
Try below code:
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
view1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
view2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Use Layout Parameters to set height and Width of views at runtime.
like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
view .setLayoutParams(layoutParams);

Setting the size of an Imageview in a dynamic Linearlayout

I am dynamically creating a linearlayout and adding some imageviews and textviews to it.
Code
ScrollView sv = new ScrollView(this);
LinearLayout llay = new LinearLayout(this);
for(int i =0;i<ns;i++){
TextView tv = new TextView(this);
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LinearLayout.LayoutParams(100,100,50));
llay.addView(tv);
llay.addView(iv);
}
On running the application
1st device : The imageviews are of appropriate size.
2nd device : the imageviews are too small.
Is there any way, I can get away with setting the size of the imageview explicity so that it becomes device independent?
Try This....
If your app for diff size density use display height/width instead of fix value.
Display display = getWindowManager().getDefaultDisplay();
int height = display.getHeight()/10;
int width = display.getWidth()/5;
ScrollView sv = new ScrollView(this);
LinearLayout llay = new LinearLayout(this);
for(int i =0;i<ns;i++){
TextView tv = new TextView(this);
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LinearLayout.LayoutParams(width,height,0));
llay.addView(tv);
llay.addView(iv);
}
It's recommend to use dp instead of px.
For more information about dp vs px you can visit Support multiple screens
In your case, you set the value in pixels. If you want to work with dp you can use the following:
float x = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());

Dynamically add ImageView to TableRow

I have a TableLayout in my XML, and I'd like to dynamically add a TableRow with an ImageView to that TableLayout. What I have so far is this:
TableRow tr = new TableRow(this);
ImageView imgView = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(lp);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.icon_test));
tr.addView(imgView);
tlCollection.addView(tr);
What am I doing wrong? If I want to add a Textview to the same TableRow, it works, but adding an ImageView doesn't work..
The code for adding a TextView:
TextView myTextView = new TextView(this);
myTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
myTextView.setText("Test");
tr.addView(myTextView);
Any idea?
I fixed it, using an LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
TableRow row = (TableRow)inflater.inflate(R.layout.collection_row, tlCollection, false);
//fill textview
TextView content = (TextView)row.findViewById(R.id.txtCollection);
content.setText("Test");
//fill imageview
ImageView myImgView = ImageView)row.findViewById(R.id.imgCollection);
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.my_icon));
//add row to tablelayout
tlCollection.addView(row);
I think it has to do with the layout params. You're setting both dimensions to WRAP_CONTENT but the image view doesn't have "content" per se, it has a source drawable. Try playing around with adjustViewBounds, setScaleType, and the layout params. Notice that in your example of being able to add a TextView, you're setting the width to FILL_PARENT?
The only example I can find quickly in my open projects of adding an ImageView dynamically in this way was a case in which I was using FILL_PARENT in both directions. That might not work for you since I'm not doing it exactly the same, but it's worth playing around with these settings as well as the two others mentioned above.
try this
ImageView imgView = new ImageView(this);
imgView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
tr.addView(imgView);
I Had the same problem now its fixed using the below code
CandFlag =new ImageView(this);
CandFlag.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
CandFlag.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.flag));
LayoutParams layoutParams = (LayoutParams) CandFlag.getLayoutParams();
layoutParams.width = 75;
layoutParams.height = 75;
CandFlag.setLayoutParams(layoutParams);
Tablerow.addView(CandFlag);

Setting relative layout parameters

So I'm trying to add an imageview to my current xml design - and its working decently. Now my main problem is that I cant seem to find a way to set the attributes for the image like where it needs to be displayed etc.
RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.blue_1);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i);
setContentView(mRelativeLayout);
I tried messing around with setlayoutparams but got absolutely no clue what to do with it.
You actually have to use the class LayoutParams to do that efficiently and easily :
RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
params.leftMargin = 25;
params.topMargin = 25;
i.setImageResource(R.drawable.icon);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i, params);
This works for me and put my icon in the top left corner of the screen with the specified margin to the sides of the screen.
Does that help?

Categories

Resources