How to set LayoutParams for ImageView
You need to specify which LayoutParams you need to use, it must be
based on parent layout which can be anything
LinearLayout.LayoutParams or RelativeLayout.LayoutParams or FrameLayout.LayoutParams.
RelativeLayout imageLayout = new RelativeLayout(this);
ImageView imageview = new ImageView(this);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setImageResource(R.drawable.nature);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageLayout.addView(iv, lp);
or
imageview.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
imageLayout.addView(iv);
Why isn't setLayoutParams changing the size of my ImageView?
Since you're casting snellen
's LayoutParams
to ConstraintLayout.LayoutParams
, it seems likely that your view is the child of a ConstraintLayout
.
If that is the case, it is possible that the problem is that your view's dimensions are defined by its set of constraints, and so changing params.width
and params.height
will have no effect.
Check your view's constraints and make sure they are not determining the view's dimensions.
Set other layout parameters imageview
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
lp.bottomMargin = ...
lp.leftMargin = ...
lp.rightMargin = ...
lp.topMargin = ...
imageView.setLayoutParams(lp);
setLayoutParams resets ImageView position
Perhaps you should use it this way:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.width = (int) (200 * scale + 0.5f);
layoutParams.height = (int) (250 * scale + 0.5f);
imageView.setLayoutParams(layoutParams); //I think this part might not be needed.
changing height and width of imageview in android
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.
Related Topics
Why Getspeed() Always Return 0 on Android
Firebase Ontokenrefresh() Is Not Called
Google Maps Fragment Returning Null Inside a Fragment
Android How to Replace the Deprecated Tabhost
Save Data and Change Orientation
Android Camera Android.Hardware.Camera Deprecated
Send Touch Events to a Device via Adb
How to Order My SQLite Database in Descending Order, for an Android App
Image Share Intent Works for Gmail But Crashes Fb and Twitter
Error:Connectionresult{Statuscode=Internal_Error, Resolution=Null}
Replacing Default Phone App on Android 6 and 7 with Incallservice
Stop Asynctask Doinbackground Method
Android Fragments: When to Use Hide/Show or Add/Remove/Replace
Explain the Meaning of Span Flags Like Span_Exclusive_Exclusive