So my initial layout was like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/margin_padding_size_small">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_cod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/margin_padding_size_small"
android:paddingEnd="@dimen/margin_padding_size_small"
android:text="@string/cash_on_delivery_label"
app:cornerRadius="@dimen/card_corner_radius_medium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn_card"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/cash_on_delivery_label" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_card"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/margin_padding_size_small"
android:paddingEnd="@dimen/margin_padding_size_small"
android:text="@string/card_label"
app:cornerRadius="@dimen/card_corner_radius_medium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn_qr_scan"
app:layout_constraintStart_toEndOf="@+id/btn_cod"
app:strokeColor="@color/colorAccent"
tools:text="@string/card_label" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_qr_scan"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/margin_padding_size_small"
android:paddingEnd="@dimen/margin_padding_size_small"
android:text="@string/qr_scan_label"
app:cornerRadius="@dimen/card_corner_radius_medium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn_card"
app:strokeColor="@color/colorAccent"
tools:text="@string/qr_scan_label" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
And I made an assumption pretty early that I will be setting the style
attribute for the buttons based on the response I receive from the server. But Android Framework does not have a setStyle()
method.
So I had to find some other way to do it. I searched and found Paris library which does exactly allow us to set style but it currently does not work on MaterialComponents.
I tried to use MaterialButtonToggleGroup but including the dependency messes up with the DataBinding completely and shows errors in other modules of the app.
My current approach is to dynamically create all three buttons and populate them in the ConstraintLayout which will be my root layout.
How would I programmatically populate these three buttons with contraints and chain.
I have been trying:
val constraintSet = ConstraintSet()
// constraintSet.clone(holder) // do I need this?
//holder is the id of the constraint layout from xml
val codButton = MaterialButton(this, null, R.attr.materialButtonStyle)
codButton.text = resources.getString(R.string.cash_on_delivery_label)
codButton.id = generateViewId()
codButton.setCornerRadiusResource(R.dimen.card_corner_radius_medium)
holder.addView(codButton)
val cardButton = MaterialButton(this, null, R.attr.borderlessButtonStyle)
cardButton.text = resources.getString(R.string.card_label)
cardButton.id = generateViewId()
cardButton.setCornerRadiusResource(R.dimen.card_corner_radius_medium)
cardButton.setStrokeColorResource(R.color.colorAccent)
holder.addView(cardButton)
val qrButton = MaterialButton(this, null, R.attr.borderlessButtonStyle)
qrButton.text = resources.getString(R.string.qr_scan_label)
qrButton.id = generateViewId()
qrButton.setCornerRadiusResource(R.dimen.card_corner_radius_medium)
qrButton.setStrokeColorResource(R.color.colorAccent)
holder.addView(qrButton)
//constraintSet.connect() calls?
constraintSet.applyTo(holder)
Please tell me the best approach to achieve this. The requirement is that for any response from the server, two of the three buttons shall be set as outlined button from material components and one as regular button from material components.
Another question is: I am not able to use R.attr.materialButtonOutlinedStyle
(unResolved symbol) but I am able to use R.attr.materialButtonStyle
; Why is that?