1

I am trying to create button C programmatically, but can not get the size below 231 pixels. The XML Buttons have 157 pixels width.

How do I make the button same size as the others?

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    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=".MainActivity">

    <TextView
        android:id="@+id/label_enter_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        android:layout_marginTop="50dp"
        android:text="@string/enter_code_label"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/code_1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:inputType="textCapCharacters"
        android:maxLength="2"
        android:text="A1"
        android:textAlignment="center"
        android:textSize="@dimen/text_big"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="@+id/label_enter_code"
        app:layout_constraintTop_toBottomOf="@id/label_enter_code" />

    <EditText
        android:id="@+id/code_2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/text_big"
        android:inputType="textCapCharacters"
        android:maxLength="2"
        android:text="B2"
        android:textAlignment="center"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="@id/code_1"
        app:layout_constraintStart_toEndOf="@id/code_1" />

    <EditText
        android:id="@+id/code_3"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        android:inputType="textCapCharacters"
        android:maxLength="2"
        android:text="C3"
        android:textAlignment="center"
        android:textSize="@dimen/text_big"
        app:layout_constraintBottom_toBottomOf="@id/code_1"
        app:layout_constraintStart_toEndOf="@id/code_2" />

    <Button
        android:id="@+id/button_a"
        android:layout_width="@dimen/button_size"
        android:layout_height="@dimen/button_size"
        android:layout_marginTop="30dp"
        android:text="A"
        android:textSize="@dimen/text_big"
        app:layout_constraintStart_toStartOf="@+id/code_1"
        app:layout_constraintTop_toBottomOf="@+id/code_1" />

    <Button
        android:id="@+id/button_b"
        android:layout_width="@dimen/button_size"
        android:layout_height="@dimen/button_size"
        android:text="B"
        android:textSize="@dimen/text_big"
        app:layout_constraintStart_toEndOf="@id/button_a"
        app:layout_constraintBottom_toBottomOf="@id/button_a" />

    <!--Button // This is what I want to achieve in code.
        android:id="@+id/button_c"
        android:layout_width="@dimen/button_size"
        android:layout_height="@dimen/button_size"
        android:text="C"
        android:textSize="@dimen/text_big"
        app:layout_constraintStart_toEndOf="@id/button_b"
        app:layout_constraintBottom_toBottomOf="@id/button_a" />

</androidx.constraintlayout.widget.ConstraintLayout>

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val buttonSize = resources.getDimension(R.dimen.button_size).toInt()
        val textSize  = resources.getDimension(R.dimen.text_big)

        val layout = main_layout

        var letter = 67
        val buttonB = button_b

        val button = Button(this)
        val txt = letter.toChar().toString()
        button.id = View.generateViewId()
        button.text = "C"
        button.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
        button.width = 157 // or start with 400, then click on C will reduce size gradually
        // button.setPadding(0,0,0,0)
        button.height = buttonSize
        button.gravity = Gravity.CENTER
        // button.minWidth = 0 // will set button width to 231, regardless other settings
        layout.addView(button)
        button.setOnClickListener {
            buttonB.width = buttonB.width + 10
            label_enter_code.text = "W: ${button.width}, H: ${button.height}"
            button.width = button.width - 10
//            val marginLayoutParams = button.layoutParams as MarginLayoutParams
//            marginLayoutParams.setMargins(0,0,0,0)
        }

        var constraintSet = ConstraintSet()
        constraintSet.clone(layout)
        constraintSet.connect(button.id, ConstraintSet.START, button_b.id, ConstraintSet.END )
        constraintSet.connect(button.id, ConstraintSet.BOTTOM, button_a.id, ConstraintSet.BOTTOM)
        constraintSet.applyTo(main_layout)

    }
}

Solution:

Instead of button.width/height I use now:

button.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
button.layoutParams = ViewGroup.LayoutParams(buttonSize,buttonSize)

I did not do in first place, because val lp = button.layoutParams resulted in Null.

Gunnar Bernstein
  • 6,074
  • 2
  • 45
  • 67
  • Does this answer your question? [Set ImageView width and height programmatically?](https://stackoverflow.com/questions/3144940/set-imageview-width-and-height-programmatically) – momvart Mar 18 '20 at 22:34

1 Answers1

1

You should set layout parameters instead of width and height.

button.layoutParams = ViewGroup.LayoutParams(157, ViewGroup.LayoutParams.WRAP_CONTENT)

or equivalently when you are adding your view to the parent:

layout.addView(button, ViewGroup.LayoutParams(157, ViewGroup.LayoutParams.WRAP_CONTENT))
momvart
  • 1,737
  • 1
  • 20
  • 32