MainActivity.kt
package com.example.jetpack
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
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)
// set toolbar as support action bar
setSupportActionBar(toolbar)
supportActionBar?.apply {
title = "Kotlin - Toolbar Add Elevation"
// we can add toolbar elevation programmatically
// default elevation 4dp
elevation = 4.toDp(this@MainActivity).toFloat()
}
}
}
// extension method to convert values to dp
fun Int.toDp(context: Context):Int = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,this.toFloat(),context.resources.displayMetrics
).toInt()
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"
android:background="#C5C4BB"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:elevation="4dp"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

- android kotlin - ActionBar title text size
- android kotlin - ActionBar title color
- android kotlin - ActionBar title click listener
- android kotlin - ActionBar title center
- android kotlin - Popup menu example
- android kotlin - Context menu example
- android kotlin - Menu item text color
- android kotlin - CalendarView set minDate maxdate
- android kotlin - Toolbar menu example
- android kotlin - Toolbar back button color
- android kotlin - Toolbar back button example
- android kotlin - Toolbar add button right
- android kotlin - Change indeterminate progress bar color
- android kotlin - Change circular progress bar color
- android kotlin - Custom circular progress bar