Thursday, March 5, 2020

android kotlin - Material button remove padding example

MainActivity.kt

package com.example.jetpack

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // programmatically remove padding from material button
        button7.minWidth = 0
        button7.minimumWidth = 0
        button7.minHeight = 0
        button7.minimumHeight = 0

        button8.minWidth = 0
        button8.minimumWidth = 0
        button8.minHeight = 0
        button8.minimumHeight = 0
    }
}
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>
More android examples