Android: Set minimum height of a view programmatically - android

I'm trying to set minimum height for an imageview programmatically but had no luck so far. My code is below
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 160, getResources().getDisplayMetrics());
int minHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
LayoutParams params = new LayoutParams(width,LayoutParams.MATCH_PARENT);
SmartImageView siv = new SmartImageView(getActivity());
siv.setMinimumHeight(minHeight);
siv.setLayoutParams(sivParams);
siv.setAdjustViewBounds(true);
siv.setScaleType(ScaleType.FIT_XY);
siv.setImageUrl(url);
ll.addView(siv);
I also tried to put the setMinimumWidth method after the setLayoutParams method but that didn't worked either. Please help
Thanks

The answer by #Ashok Damani is incorrect as pointed out by #Gustavo Baiocchi Costa in the comments:
this sets the height or width not the minimum width or height
The correct method to set the minimum height of a view in Android:
Property access way(in Kotlin):
yourView.minimumHeight = minHeight
Setter method way:
yourView.setMinimumHeight(minHeight);
Android Documentation says:
Sets the minimum height of the view. It is not guaranteed the view
will be able to achieve this minimum height (for example, if its
parent layout constrains it with less available height).

try this--->
siv.getLayoutParams().height = minHeight;
or u can also try--->
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width , minHeight);
siv.setLayoutParams(layoutParams);

Related

Set CardView height programmatically

I am new to Android dev and I'm having trouble trying to set the min height of a it.gmariotti.cardslib.library.view.CardView programmatically. I create a new instance of a CardView and set all the xml elements in code, but it does not affect the cardView. I do not inflate the cardview from xml.
CardView catCard = new CardView(getActivity());
catCard.setBackgroundColor(Color.green(0));
catCard.setMinimumHeight(10);
catCard.setBottom(0);
catCard.setExpanded(false);
CardView extends FrameLayout so you should be able to set LayoutParams. Try something like this :
CardView.LayoutParams layoutParams = (CardView.LayoutParams)
catCard.getLayoutParams();
layoutParams.height = 10;
, dont forget that setting Width is also required.
Or create new LayoutParams like this (not tested) :
CardView catCard = new CardView(getApplicationContext());
// sets width to wrap content and height to 10 dp ->
catCard.setLayoutParams(new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT, 10));
catCard.setMinimumHeight(10);
myCardView.setLayoutParams(new RelativeLayout.LayoutParams(20, 20));
it depends that your cardview is a child of which layout, in this case it is under Relative Layout
My Solution with margin and it worked perfectly with me
val resources = context.resources // get resources from context
val width: Int = resources.displayMetrics.widthPixels / 3 //get width
val px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, resources.displayMetrics).toInt() //set margin
val layoutParams = ViewGroup.MarginLayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT)
layoutParams.marginStart = px
layoutParams.marginEnd = px
cardView.layoutParams = layoutParams

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

How to find out the custom dialog box width and height at runtime android

Hi i am creating one custom dialog box. It's working fine. I am allocated to the width is fill_parent and height is wrap_contentHow to find out their width and height at runtime.
For any view, you can use getHeight() and getWidth() methods to get height and width information.
See this for more information.
Try this;
AlertDialog.Builder a = new AlertDialog.Builder(this);
View v = a.create().getCurrentFocus();
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int height = v.getMeasuredHeight();
int width = v.getMeasuredWidth();

re-setting a TextView height programmatically

I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView
android:id="#+id/text_l"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="145dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" >
</TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l);
text.setHeight(70);
but nothing changed.
You should change it via LayoutParams:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
In Kotlin with DP to pixels translation
changeTextHeight.setOnClickListener { view ->
// random height for testing
val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10
// set Height in pixels
hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
//refresh layout
hello.requestLayout()
}
Convert DP to pixels, see this post:
fun convertDpToPixel(dp: Float, context: Context): Int {
return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
the sample way for this if you want to use dimen
first, you should set size in dimen XML file.
<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>
and
text_l.getLayoutParams().height =
getResources().getDimensionPixelSize(R.dimen.text_height);
text_l.getLayoutParams().width =
getResources().getDimensionPixelSize(R.dimen.text_width);
Or
if you want to set just int (for example we wanna set 50dp height and 100dp width)
text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}

Android Changing image size depending on Screen Size?

So I need to change the size of an image depending on the area of the screen. The image will have to be half of the screen height, because otherwise it overlaps some text.
So Height= 1/2 Screen Height.
Width = Height*Aspect Ratio (Just trying to keep the aspect ratio the same)
I found something that was:
Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();
But how would I change image height in java? or even XML if possible? I can't seem to find a working answer.
You can do this with LayoutParams in code. Unfortunately there's no way to specify percentages through XML (not directly, you can mess around with weights, but that's not always going to help, and it won't keep your aspect ratio), but this should work for you:
//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.rootlayout);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.image);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getDrawable().getIntrinsicWidth();
int orgHeight = image.getDrawable().getIntrinsicHeight();
//double check my math, this should be right, though
int newWidth = Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.addView(image);
Might be overcomplicated, maybe there's an easier way? This is what I'd first try, though.

Categories

Resources