I have an XML
layout with an EditText
on top and then a lot of buttons and a bunch of other views after the keyboard. The root element is ConstraintLayout
.
<ConstraintLayout ...>
<EditText .../>
<!-- Here is the list of buttons -->
<!-- For placement constraints purposes, I may have id's in the buttons -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
... <!-- About 26 of them -->
<Button .../>
</ConstraintLayout>
Hard-coding the buttons ids and the findViewById(resId)
will leave a lot of boilerplates and doesn't seem to be the best way.
How do I get this much of buttons in an array of Button[]
in the ActivityName.java
? What would be the best way to do that?
EDIT:
Below is a single actual button which I'm am using in my XML. I think it will be hard to programmatically add the button to the ConstraintLayout
. If it is possible, please share the way of doing it programmatically.
<Button
android:id="@+id/keyboard_button_Q"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:minWidth="0dp"
style="?android:attr/buttonBarButtonStyle"
android:padding="0dp"
android:text="Q"
app:layout_constraintEnd_toStartOf="@id/keyboard_button_W"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />