Hello! In this article, we will explore in detail how to create beautiful and functional user interfaces (UI) using Kotlin for Android app development, from the basics to intermediate level, especially utilizing Google’s Material Library.
1. What is Material Design?
Material Design is a design language announced by Google in 2014, created to enhance user experience (UX) and develop appealing apps. Material Design is consistent and provides intuitive UI elements, giving users a familiar feeling.
Key elements include color, typography, shadows, animations, etc., which provide information hierarchy and intuitive interactions.
2. Installing Material Components
First, to use Material Design components, you need to add the necessary libraries to your project. Add the following dependencies to your build.gradle
file.
dependencies {
implementation 'com.google.android.material:material:1.6.1'
}
3. Constructing Basic Layout
The starting point of an Android app is typically a layout file named activity_main.xml
. Let’s construct a basic layout using Material components.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</com.google.android.material.appbar.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Material Design for Android App Development"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the button"/>
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
4. Using Material Button and Text Field
Next, let’s create a form to gather input from the user using Material Button and Text Field.
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
Handling User Input
Now, let’s write code to handle the input when the user clicks the button after entering text.
class MainActivity : AppCompatActivity() {
private lateinit var editName: TextInputEditText
private lateinit var btnSubmit: MaterialButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editName = findViewById(R.id.edit_name)
btnSubmit = findViewById(R.id.btn_submit)
btnSubmit.setOnClickListener {
val name = editName.text.toString()
Toast.makeText(this, "Entered name: $name", Toast.LENGTH_SHORT).show()
}
}
}
5. Implementing Material Snippet Dialog
Let’s use a dialog to get additional information from the user. Here’s how to implement a Material dialog.
private fun showInputDialog() {
val builder = AlertDialog.Builder(this)
builder.setTitle("Input Information")
val input = EditText(this)
builder.setView(input)
builder.setPositiveButton("OK") { _, _ ->
Toast.makeText(this, "Input value: ${input.text}", Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton("Cancel") { dialog, _ -> dialog.cancel() }
builder.show()
}
6. Utilizing Material CardView
CardView is a great component that can effectively showcase a collection of information. Let’s learn how to display multiple pieces of information using CardView.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CardView Title"
android:textStyle="bold"
android:padding="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is where the content of the card goes."
android:padding="16dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
7. Animations and Transition Effects with Material Design
Let’s learn how to implement animations and transition effects to provide visual impact. You can use the Transition API to apply animation effects during screen transitions.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Transition animation from the previous screen
val transition = ChangeBounds()
transition.duration = 300
TransitionManager.beginDelayedTransition(findViewById(R.id.coordinatorLayout), transition)
}
8. List Composed with Material Design
To effectively list information in an Android app, I will explain how to configure a list using RecyclerView.
class MyAdapter(private val items: List) : RecyclerView.Adapter() {
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.text_view)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = items[position]
}
override fun getItemCount() = items.size
}
9. Conclusion
In this tutorial, we learned how to construct the UI of a basic Android app using Kotlin and Material Design components. With Material Design, you can conceive apps that are more intuitive and inviting for users.
Material Design offers a variety of components and functionalities, allowing you to combine them freely as needed. Continue to add more features, practice, and gain experience!
Thank you!