-1

I want to set height and width for framelayout , if give values in java code using setlayoutparams its showing very small box, but in XML its showing properly.

This is my java code

 FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
 ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(300, 300);
 frameLayout2.setLayoutParams(lp);

this is my xml: Note:Constraint layout is my parent

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LightPlaceFragment"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp"
    android:background="@color/colorAccent">



    <FrameLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.882"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/l_width"
        app:layout_constraintVertical_bias="0.162"
        android:id="@+id/frameLayout2">


        <include
            android:id="@+id/other"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:visibility="visible"
            layout="@layout/sample1" />

    </FrameLayout>



</android.support.constraint.ConstraintLayout>

values given in xml so its showing properly: Result using XML

Values in given java code so its showing very smallbox Result using Java

How can I change the framelayout width and height programatically?

Please help me.

Simon Black
  • 913
  • 8
  • 20
Kalaivani R
  • 327
  • 1
  • 4
  • 11

4 Answers4

1

Try this

FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
ConstraintLayout.LayoutParams oldParams = (ConstraintLayout.LayoutParams) frameLayout2.getLayoutParams();
oldParams.width=400;
oldParams.height=500;
frameLayout2.setLayoutParams(oldParams);

OR this

FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
ConstraintLayout.LayoutParams oldParams= new ConstraintLayout.LayoutParams(300,300);
frameLayout2.setLayoutParams(oldParams);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

Happened with me as well.

After spending so much time, I got to know that on setting height and width programmatically it takes in PX (pixel) not in dp which we set in XML.

So you need to convert dimension into PX from dp first using display factor like this.

val scale: Float = resources.displayMetrics.density
val resizedInPx = (widthInDp * scale + 0.5f).toInt() // dp to px 

Where widthInDp is the desired dp you want to set like 300

Usage

val params: ViewGroup.LayoutParams = yourlayout.layoutParams
params.width = (widthInDp * scale + 0.5f).toInt() // dp to px
params.height = (heightInDp * scale + 0.5f).toInt() // dp to px 
layout!!.setLayoutParams(params)
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

Use this

 FrameLayout frameLayout2= (FrameLayout) light.findViewById(R.id.frameLayout2);
 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(300, 300);
 frameLayout2.setLayoutParams(lp);
Ramkumar.M
  • 681
  • 6
  • 21
0

(KOTLIN) If you need to update the width and height only, you can use the below method:

 private fun resizeView(view: View) {
    val size = resources.getDimension(R.dimen.view_size).toInt()
    view.layoutParams.width = size
    view.layoutParams.height = size

    //or (but be careful because this will change all the constraints you added in the xml file) 
    val newLayoutParams = ConstraintLayout.LayoutParams(size, size)
    view.layoutParams = newLayoutParams
}
MohamedHarmoush
  • 1,033
  • 11
  • 17