android RelativeLayout change height programmatically
Asked Answered
H

5

32

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

Humid answered 26/9, 2014 at 11:2 Comment(0)
W
48

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);
Waltman answered 26/9, 2014 at 11:14 Comment(1)
If i have a child inside RelativeLayout with match_parent in width and modify the width from RelativeLayout, how can update the width of child?Derangement
O
32

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;
Outbalance answered 26/9, 2014 at 11:6 Comment(0)
X
8

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;
Xanthus answered 20/11, 2016 at 0:15 Comment(0)
C
6

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;
Cq answered 26/9, 2014 at 11:8 Comment(0)
D
0

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)
Dissimilar answered 15/6, 2022 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.