changing height and width of imageview in android - android

in my application on button click i want to change size of imageview.when i click on one button i want to increase size of imageview by 5(each width and height).and on clicking of other button i want to decrease the size.i tried this.
how can i do this?below is the code.its not working
public void increase(View v)
{
int height=imageView.getHeight();
int width=imageView.getWidth();
imageView.getLayoutParams().height = height+5;
int h=imageView.getHeight();
System.out.println("hhhhhhhhhh,,,,,,,,,,"+h);
System.out.println("width n hight..."+width+"and"+height);
}

UPDATE:
Do this:
int height=imageView.getHeight();
int width=imageView.getWidth();
imageView.setLayoutParams(new LinearLayout.LayoutParams(height, width));
Suggesting your ImageView is located inside a LinearLayout you need to do this:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
params.setHeight(XX);
params.setWidth(XX);
imageView.setLayoutParams(params);
I don't know if the syntax is perfectly correct, but basically this is how you do it, don't forget that you need to update your UI, in order to see any changes.

I use the following code for it:
your_image_view.getLayoutParams().height = 20;
Hope it helps u..
you can try the following alternate approach.. see if it works:
LayoutParams params = (LayoutParams) imageView.getLayoutParams();
params.width = 120;
params.height = 120;
imageView.setLayoutParams(params);
Although both the codes are almost same...

Related

How to change the imageview size dynamically in android for a particular image?

Actually I am using Relative layout for image view. This image view is for receiving images from the internet to the application. The thing I need is can I change the image size dynamically for a particular images (say jpeg) and another size for attachments(docx,pdf), etc.
I used if condition for images and another if for attachments. Now how to set image size for receiving images and another image size for receiving attachments. I need to do dynamically. Note I am using only one image view for both images and attachments.
<RelativeLayout>
<ImageView>
id=iv
width=wrap content
height=wrap content
</ImageView>
</RelativeLayout>
for class file
if("image/png"){
iv.setImageResource(R.drawable.abc);
or
code to get images from web
}
else if(mimetype("application/pdf")
{
iv.setImageResource(R.drawable.abc)
}
this is the model
Help me out!
/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
imageView.requestLayout();
imageView.getLayoutParams().height = height;
imageView.getLayoutParams().width = width;
reference : https://android--code.blogspot.com/2015/08/android-imageview-set-width-and-height.html
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = x;
lp.height = y;
imageView.setLayoutParams(lp)
try this,
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(width,height);
ImageView ivImage=(ImageView)findViewById(R.id.image);
ivImage.setLayoutParams(params);
Try this code to change image aspect ratio dynamically,
private void setPicDimensions() {
Point screenDimensions = Utils.getScreenDimensions();
width = screenDimensions.x;
height = Utils.getHeightFromAspectRatio(width, 16, 9);// dynamically set aspect ratio value here its 16:9
}
// in your image view set layout params dynamically whereever you need to change
//Declare height and width as int and iv_pic as imageview
LayoutParams layoutParams = iv_offer_pic.getLayoutParams();
layoutParams.height = height;
layoutParams.width = width;
iv_pic.setLayoutParams(layoutParams);
most important is:
<imageview android:scaleType="fitXY" />
<RelativeLayout>
<ImageView>
id=iv
android:scaleType="fitXY"
width=wrap content
height=wrap content
</ImageView>
</RelativeLayout>
for class file
if("image/png"){
iv.setImageResource(R.drawable.abc);
or
code to get images from web
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = 100;
lp.height = 100;
imageView.setLayoutParams(lp)
}
else if(mimetype("application/pdf")
{RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = 50;
lp.height = 50;
imageView.setLayoutParams(lp)
iv.setImageResource(R.drawable.abc)
}
this is the model
Help me out!

RelativeLayout.LayoutParams.WRAP_CONTENT make ImageView smaller in loop

I add ImageView to layout in runtime, But ImageView is smaller after every loop. This is my code:
layout = (RelativeLayout)findViewById(R.id.layout_main);
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight(
for(int i=0; i< fruit.size();i++) {
int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
getApplicationContext().getPackageName());
int h = random.nextInt(height);
int w = random.nextInt(width);
Logger.debug(TAG, "drawable/"+fruit.get(i).file+":"+id+":X="+w+":Y="+h);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(id);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(w,h,0,0);
imageView.setLayoutParams(layoutParams);
layout.addView(imageView);
And this is my result:
Please help me why and how to fix it?
Thank you very much
More information:
If I replace code
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(w,h,0,0);
imageView.setLayoutParams(layoutParams);
By
imageView.setY(h);
imageView.setX(w);
Result will
may be this create problem
layoutParams.setMargins(w,h,0,0);
setmargin put margin as
setMargins(int left, int top, int right, int bottom)
I believe it's because of the Random class. You are already bounding the random number. You can shift the generated number, if you don't want it to approach to zero.
int h = random.nextInt(height) + height;
Or you can change the seed if you are providing any. Please see the link below.
How the random numbers are calculated.

Resizing an imageview's width/height greater or lesser upon a button click?

Would this work for changing an imageview's size? Doing something such as:
ImageView image = (ImageView) findViewById(R.id.image);
relativeLayout.addView(image);
...
Then upon button click have this happen:
image.getLayoutParams().width = image.getLayoutParams().width - 10;
I click my button but nothing seems to happen. Why? What's the correct way to go about manually resizing an imageview?
getLayoutParams() is a method. So, you're going to have to add opening and closing parentheses when you call it. Try this instead
image.getLayoutParams().width = image.getLayoutParams().width - 10;
Or simply
image.getLayoutParams().width -= 10;
Edit: Try it like this
ImageView image = (ImageView) findViewById(R.id.image);
LayoutParams params = (LayoutParams) image.getLayoutParams();
params.width = params.width - 10;
image.setLayoutParams(params);

finding the height and width of the dynamically created Imageview ?

LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView
mainimage = new ImageView(HLActivity.this);
mainimage.setLayoutParams(params);
loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
I already tried mainimage.getwidth() and mainimage.getHeight();
Now i want to findout the orignalwidth and height of the ImageView after
place the image in the imageview. can you please help me.
getWidth() and getHeight() are the indeed ones you are looking for, but you are trying to ask for the width and the height when the view is not measured yet. To fix this, we post a message to the ImageView, which will get executed after the measurement is done.
LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// mark it as final, so it can be accessed in the runnable.
final imageView mainimage = new ImageView(HLActivity.this);
mainimage.setLayoutParams(params);
mainimage.post(new Runnable() {
#Override
public void run() {
// get the width and the height
int width = mainimage.getWidth();
int height = mainimage.getHeight();
// do what you want to do with the sizes.
loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
}
});

How to set Imageview height and width in activity android

Hi I wanted to change height and width property of image view inside my activity
I tried it in following way but its not working for me ...
View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);
But I am not able to change height and width of imageview src. Is there any mistake I am doing?
How to do this?
Need help...
Thank you...
In this piece of code, I am creating a new instance of an ImageView at runtime and setting dimensions to it:
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
This will set the dimensions of the ImageView. They will, however, be in px. Use the code from here if you need dp values: https://stackoverflow.com/a/9563438/450534
UPDATED:
To change the dimensions of an existing ImageView that has already been defined in the XML, use this:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
try some thing like this...
LayoutParams params = new LayoutParams(100, 100);
parantlayout.addView(coupon_img, params);
I think this will help you.
I think you are not adding the changed image to the layout..
LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);
image.setLayoutParams(
new LinearLayout.LayoutParams(
bmp.getWidth(),
bmp.getHeight()));
ll.addView(image);// Then add the image to linear layout

Categories

Resources