Set zero height to constraintLayout child programmatically - android

How can I set the height of a ConstraintLayout child to be 0dp?
When I use layoutParams and do the following, ConstraintLayout thinks 0dp = match_parent
val layoutParams = view.layoutParams.apply {
this.height = newHeight
}
view.layoutParams = layoutParams
So if newHeight is 0 here, it thinks it's match_parent
I would like to avoid checking for 0 and if true set it to a negative value

Related

ConstraintLayout: how to divide screen in two equal parts of screen height programmatically

How to divide the screen in two equal part wrt height with constraint layout programmatically?
For this, you need to set the height to zero then set the percent accordingly. For e.g.,
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) viewTop.getLayoutParams();
lp.height = 0;
lp.matchConstraintPercentHeight = (float) 0.5;
ConstraintLayout.LayoutParams lp2 = (ConstraintLayout.LayoutParams)viewBottom.getLayoutParams();
lp2.height = 0;
lp2.matchConstraintPercentHeight = (float) 0.5;
viewTop.setLayoutParams(lp);
viewBottom.setLayoutParams(lp2);

Android calculate height of child View When overflow

I have nested LinearLayouts where Parent height is 90 but the child view may have different heights according to the text set inside it and may overflow its parent. I Want to animate Parent to final Height of child but both methods of getHeight() on child and getMeasuredHeight() on parent, return 90 not 400.
parent.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
targetHeight = parent.getMeasuredHeight();
//returns 90
and also
View child=((ViewGroup) parent).getChildAt(0);
int targetHeight=child.getHeight();
//returns 90
Try this code. Hope it'll work.
ViewGroup.LayoutParams params = yourView.getLayoutParams();
int height = params.height;
LayoutParams paramsChild = child.getLayoutParams();
LayoutParams paramsParent = parent.getLayoutParams();
paramsParent.height = paramsChild.height;
parent.setLayoutParams(paramsParent);

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

Changing RelativeLayout - the height changes but not width

I have a relative layout that will expand / collapse - so I increase/decrease the height when a user taps on it.
I'm using this code
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.height = intialExpandedHeight; // or initialHeight - based on the expand or collapse effect
params.width = screenWidht + 150; // or a screenWidht value when collapsed
view.setLayoutParams(params);
view.requestLayout();
The height of the layout changes and it works the way I want it to - the only problem is that the width is not changing. (it's the same every time)
Could you please tell me what seems to be the problem with my code? Why does the height changes but not width?

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
}

Categories

Resources