0

I dynamically add a button to GridLayout every time pushed "Add button." drawble is applied properly, but the style attribute is not applied.

So I wrote the code by referring to this post, but it didn't work out the way I wanted it to.
https://stackoverflow.com/questions/4738309/how-can-i-change-a-button-style-dynamically-in-android
So when I searched, I found that setTextAppearance only can chanege with related attributes of text. Is there any way to change the margin and size?

And I tried this method to programmatically change the size, but the same result came out.

int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
            button.setHeight(size);
            button.setWidth(size);



I want to know how to change button style dynamically or How to set all cells in GridLayout to the same size.



My current code

GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);

Button button = new Button(this);
button.setBackgroundResource(R.drawable.color_button_shape);
button.setTextAppearance(R.style.color_button);

GradientDrawable dynamicColor = (GradientDrawable) button.getBackground().getCurrent();
dynamicColor.setColor(Color.parseColor(name));

gridLayout.addView(button);

GridLayout

<GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0">
</GridLayout>

drawble(color_button_shape)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:shape="rectangle">
<corners
    android:topLeftRadius="6dp"
    android:topRightRadius="6dp"
    android:bottomLeftRadius="6dp"
    android:bottomRightRadius="6dp" />
</shape>

style

<style name="color_button">
    <item name="android:layout_width">40dp</item>
    <item name="android:layout_height">40dp</item>
    <item name="android:layout_marginStart">10dp</item>
    <item name="android:layout_marginTop">9dp</item>
</style>

current state

I want (Please ignore the color, just refer to the size and the margin.)

2 Answers2

0

try this, it may solve - Change Grid_layout coding

    <GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:padding="8dp"
    android:columnCount="3"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0">
    </GridLayout>
  • Thank you for your answer. But I followed your answer, more than one button was not added, and the button was too small for what I wanted. (size does not change even if the style properties are changed, the style properties do not seem to be applied.) – Yujeong Kim Jul 28 '20 at 03:56
0

You can set size and margin for button like this:

    Button button = new Button(this);
    GridLayout.LayoutParams layoutParams=new GridLayout.LayoutParams();
    layoutParams.setMargins(16,16,16,16);
    layoutParams.width=size;
    layoutParams.height=size;
    button.setLayoutParams(layoutParams);
Uuu Uuu
  • 1,282
  • 2
  • 10
  • 18