How to change the height of an image_view at runtime - android

I tried IMAGEVIEW.getLayoutParams().height = 200. But it is not changing correctly. Is there any other possible way, please give suggestion.

use this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
yourImageView.setLayoutParams(layoutParams);

_image_view.getLayoutParams().height = 30;
Hope this helps.
Very Important.
If you're setting the height after the layout has already been 'laid out',
make sure you also call this :
_image_view.requestLayout()

ImageView mImageView = (ImageView)findViewById(R.id.image_view);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100); // Paramters are (width, height) in pixels
mImageView.setLayoutParams(layoutParams);
If you want multiple resolution compatibility,
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(getResources().getDimension(R.dimen.image_with)),
(int) getResources().getDimension(R.dimen.image_height));
dimens.xml under resource
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="image_with">100dp</dimen>
<dimen name="image_height">100dp</dimen>
</resources>

If your imageview already shows the image,
Call:
image_view.requestLayout();

At First Check Set ImageView width and height programmatically
You can try with DisplayMetrics Logic .Its more perfect .
A structure describing general information about a display, such as
its size, density, and font scaling.
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
image_view.getLayoutParams().height= (int) (DeviceTotalHeight*5.25/100); // You can change from here
Another Way
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 200);
IMAGEVIEW.setLayoutParams(layoutParams);

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!

How to programmatically set the width of the LinearLayout?

Is the source of the slider and the lesson link:
http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp")
How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.
To set the LinearLayout or TextView width to 1/3 of the device screen:
first of all get the device screen width:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams and set it to the Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
LinearLayout linear = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);
The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:
(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
The above answers are correct. I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object.
Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 100,
/*weight*/ 1.0f
);
YOUR_LinearLayout.setLayoutParams(param);
The easy way to do that, set value of layoutParams, and please notice this size is not in DP.
view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();

android RelativeLayout change height programmatically

I try change layout height programmatically. I have two buttons. In one button I change my layout position and second button I try to receive save position witch I was first time.
<RelativeLayout
android:id="#+id/popaplayout"
android:layout_width="fill_parent"
android:layout_height="200dp" >
This is a my layout
and in first button I wrote
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
parms.addRule(RelativeLayout.BELOW, R.id.movies_title_layout);
scrollpopap.setLayoutParams(parms);
scrollpopap.setScrollingEnabled(true);
and second button
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 300);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
In xml file I wrote height 200dp and programmatically 300 and there are different height. How I can wrote save height in programmatically?
if anyone knows solution please help me
thanks
First convert 200 dp into pixels -
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
Then set programatically,
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
Convert DP to PX like this.
final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (100 * scale + 0.5f); // replace 100 with your dimensions
Set Some layout_gravity of relative layout. and Change height and width by java code.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = px;
rl.getLayoutParams().width = px;
If you intending to change RelativeLayout height, then this help.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
Use this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
For Kotlin Users who want to set Height & Width from Dimens file, use this:
relativeLayout.layoutParams.width = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.width)
relativeLayout.layoutParams.height = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.height)

How to scale imageview image to specific size?

I created an imageview image, but I want to scale it to be no greater than a fifth of the width and a sixth of the height of the android device's screen size.
Here I get the width and height of the screen:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
here I create my imageview:
ImageView imageView = new ImageView(this);
imageView.setImageResource(icons[0]);
and here I try to scale my imageview:
imageView.getDrawable().setBounds(0, 0, width/5, height/6);
This last part is the stickler. After typing this in I get no errors and my program runs normally - but the image is not scaled. Basically that last line of code seems to have no effect and I have no idea why, any pointers?
Rest of my code:
imageView.setAdjustViewBounds(true);
int mh = imageView.getDrawable().getIntrinsicHeight();
int mw = imageView.getDrawable().getIntrinsicWidth();
imageView.setX(width/2 - Math.round(width/5));
imageView.setY(height/2 - Math.round(height/6) - mActionBarSize);
relativeLayout.addView(imageView);
setContentView(relativeLayout);
do this :
imageView.getLayoutParams().height = (int) Math.round(height/6);
imageView.getLayoutParams().width = (int) Math.round(width/5);
imageView.requestLayout(); // If you're setting the height/width after the layout has already been 'laid out'
see this link to learn more about requestLayout():
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
But ususally the view is calling it automatically, you don't have to care about that... (So for me I use it to force the view to do what I want to do)
Use this
ImageView imgView = (ImageView) findViewById(R.id.pageImage);
imgView.getLayoutParams().height = 100;
imgView.getLayoutParams().width = 100;

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