How to dynamically change the dimensions of a view

32 views Asked by At

I have a view called TextCell that's 100sp wide and 35sp tall and a vertical Linear Layout that's 50sp wide where when i press a button it adds an x amount of TextCells to the layout.

text_cell.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foregroundGravity="center"
        tools:ignore="MissingConstraints">

        <View
            android:id="@+id/black_frame"
            android:layout_width="100sp"
            android:layout_height="35sp"
            android:background="@color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/color_box"
            app:layout_constraintBottom_toBottomOf="@+id/black_frame"
            app:layout_constraintEnd_toEndOf="@+id/black_frame"
            app:layout_constraintStart_toStartOf="@+id/black_frame"
            app:layout_constraintTop_toTopOf="@+id/black_frame"
            android:layout_width="98sp"
            android:layout_height="33sp"
            android:background="#ffffff"
            android:gravity="center"
            android:textColor="@color/black"
            android:text=""
            android:autoSizeTextType="uniform"
            android:lines="1"
            android:padding="5sp"
            android:textStyle="bold"
            android:textAlignment="center"/>

    </androidx.constraintlayout.widget.ConstraintLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

my TextCell.java class:

package com.example.teacherplanner;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TextCell extends RelativeLayout {

    public TextCell(Context context,String text,String color) {
        super(context);
        init(context,text,color);
    }
    public TextCell(Context context,String text,String color, AttributeSet attrs) {
        super(context, attrs);
        init(context,text,color);
    }
    private void init(Context context, String text, String color) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.text_cell,this,true);

        TextView TextInside = findViewById(R.id.color_box);

        TextInside.setText(text);
        TextInside.setBackgroundColor(Color.parseColor(color));
    }
}

how do i set to be 50sp wide when it's added to the Layout without setting it to 50sp in xml file bc i use it elsewhere and i need it as it is

i tried with LayoutParams:

TextCell timestamp = new TextCell(WeekEditing.this,"test","#FFD54F");
timestamp.setLayoutParams(new ViewGroup.LayoutParams(
                          ViewGroup.LayoutParams.MATCH_PARENT,
                          ViewGroup.LayoutParams.WRAP_CONTENT
));
TimeStamps.addView(timestamp);

and

TextCell timestamp = new TextCell(WeekEditing.this,"test","#FFD54F");
int widthInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 50,  getResources().getDisplayMetrics());

int heightInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 35, getResources().getDisplayMetrics());

ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(widthInPixels, heightInPixels);
timestamp.setLayoutParams(layoutParams);
TimeStamps.addView(timestamp);

but it stays at 100sp width

1

There are 1 answers

0
solitude On

I see that you use the LayoutParams it should work when you are trying to modify your width and height of a view dynamically but I also notice that you have set the width and height in your text_cell.xml so your dimensions were overridden by your XML when your layout is inflated.

You have 2 options here:

1st is to adjust your dimension programmatically after inflating the layout.

private void init(Context context, String text, String color) {
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.text_cell, this, true);

TextView TextInside = findViewById(R.id.color_box);
View blackFrame = findViewById(R.id.black_frame);

// SP to px
int newWidthInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 50, getResources().getDisplayMetrics());
int newHeightInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 35, getResources().getDisplayMetrics());

// new dimensions
blackFrame.getLayoutParams().width = newWidthInPx;
blackFrame.getLayoutParams().height = newHeightInPx;
blackFrame.requestLayout(); 

TextInside.setText(text);
TextInside.setBackgroundColor(Color.parseColor(color)); }

2nd is to modify your dimension before you add it to your parent.

TextCell timestamp = new TextCell(WeekEditing.this,"test","#FFD54F");
int newWidthInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 50, getResources().getDisplayMetrics());
timestamp.getLayoutParams().width = newWidthInPx;
TimeStamps.addView(timestamp);

now try it if this works out.