In this sample calculator app, I just migrate from old Kotlin synthetics "kotlin-android-extensions" to view binding, but I see the color of buttons app:backgroundTint
didn't apply when I run I see the default button color
The supposed layout
after running
The xml of activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/gray"
>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/gray"
android:gravity="end"
android:paddingTop="50dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:text="0"
android:textColor="@color/white"
android:textSize="50sp"
android:layout_weight="1"
android:paddingEnd="5dp"
/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/acButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:text="AC"
android:onClick="buClear"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
/>
<Button
android:id="@+id/buMinPlus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:onClick="buNumberClick"
android:text="+/-"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback" />
<Button
android:id="@+id/buPercent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:text="%"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback" />
<Button
android:id="@+id/buDiv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"
android:onClick="buttonOp"
android:textSize="25sp"
app:backgroundTint="@color/orange" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/bu7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:text="7"
android:onClick="buNumberClick"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback" />
<Button
android:id="@+id/bu8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:text="8"
android:onClick="buNumberClick"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
/>
<Button
android:id="@+id/bu9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:text="9"
android:onClick="buNumberClick"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
/>
<Button
android:id="@+id/buMul"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="*"
android:textSize="25sp"
android:onClick="buttonOp"
app:backgroundTint="@color/orange"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/bu4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:onClick="buNumberClick"
android:text="4" />
<Button
android:id="@+id/bu5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:onClick="buNumberClick"
android:text="5" />
<Button
android:id="@+id/bu6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:onClick="buNumberClick"
android:text="6" />
<Button
android:id="@+id/buMin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="25sp"
app:backgroundTint="@color/orange"
android:onClick="buttonOp"
android:text="-" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/bu1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:onClick="buNumberClick"
android:text="1" />
<Button
android:id="@+id/bu2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:onClick="buNumberClick"
android:text="2" />
<Button
android:id="@+id/bu3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:onClick="buNumberClick"
android:text="3" />
<Button
android:id="@+id/buPlus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="25sp"
app:backgroundTint="@color/orange"
android:onClick="buttonOp"
android:text="+" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="horizontal">
<Button
android:id="@+id/bu0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:onClick="buNumberClick"
android:text="0" />
<Button
android:id="@+id/buDot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/whiteback"
android:onClick="buNumberClick"
android:text="." />
<Button
android:id="@+id/buEqual"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/gray"
android:textSize="25sp"
app:backgroundTint="@color/orange"
android:onClick="theResultOfOp"
android:text="=" />
</LinearLayout>
</LinearLayout>
build.gradle(app)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.calculator"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}