Thursday, March 5, 2020

Android kotlin: Material button remove padding example

This class supplies updated Material styles for the button in the constructor. The widget will display the correct default Material styles without the use of the style flag.
All attributes from Material Button are supported. Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.
At MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button7.minWidth = 0
        button7.minimumWidth = 0
        button7.minHeight = 0
        button7.minimumHeight = 0
        button8.minWidth = 0
        button8.minimumWidth = 0
        button8.minHeight = 0
        button8.minimumHeight = 0
    }
}
At activity_main.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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:text="Material Button"
        app:backgroundTint="#195905"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Material Button"
        app:backgroundTint="#0A7E8C"
        app:layout_constraintBottom_toBottomOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button3" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Material Button"
        app:backgroundTint="#D92121"
        app:layout_constraintStart_toStartOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Material Button"
        app:backgroundTint="#880085"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintStart_toEndOf="@+id/button" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="64dp"
        android:text="Material Button"
        app:backgroundTint="#74C365"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:paddingEnd="5dp"
        android:paddingStart="5dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Material Button"
        app:backgroundTint="#6050DC"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:paddingEnd="5dp"
        android:paddingStart="5dp"
        app:layout_constraintBottom_toBottomOf="@+id/button5"
        app:layout_constraintStart_toEndOf="@+id/button5" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Material Button"
        app:backgroundTint="#C04000"
        app:layout_constraintStart_toStartOf="@+id/button5"
        app:layout_constraintTop_toBottomOf="@+id/button5" />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Material Button"
        app:backgroundTint="#6C2E1F"
        app:layout_constraintBottom_toBottomOf="@+id/button7"
        app:layout_constraintStart_toEndOf="@+id/button7"
        app:layout_constraintTop_toBottomOf="@+id/button6" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result Material button remove padding example:
Material button remove padding example

No comments:

Post a Comment