Android App Development Course in Kotlin, Creating the Keypad Screen of a Phone App
In Android app development, UI/UX is a very important factor. In this course, we will learn how to implement the keypad screen of a phone app using Kotlin. Through this example, you will learn basic layout construction, button actions, event handling, and UI state management.
1. Project Setup
After launching Android Studio, create a new project. Please follow the settings below:
Application Name: PhoneDialer
Language: Kotlin
Minimum API Level: API 21: Android 5.0 (Lollipop)
Then click ‘Finish’ to start the project.
2. Creating Layout
Now let’s modify the main XML layout file to design the keypad screen. Open the res/layout/activity_main.xml file and add the following code:
In the code above, we created a simple text view and a grid layout to build the keypad. Each button includes numbers from 0 to 9, as well as the asterisk (*) and the hash (#). Next, we will set up click events for each button.
3. Adding Button Click Listeners
Now let’s open the MainActivity.kt file and add button click listeners. Modify the code as follows:
package com.example.phonedialer
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var display: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
display = findViewById(R.id.display)
val button1 = findViewById
In the code above, we set click listeners for each button, and every time a button is pressed, the corresponding number appears on the screen. The onButtonClick method retrieves the text of the pressed button and appends it to the display TextView.
4. Running and Testing the App
The code is now ready, so let’s run the app to check if the keypad works properly. Click the ‘Run’ button in Android Studio to execute it on the emulator.
This keypad is designed to be easily usable, considering user experience. Every time a number is clicked, that number appears in the top TextView.
5. Developing Additional Features
You can add a few additional functionalities to this basic version. For example, you can add a Clear Number and Call button to make it suitable for an actual phone app. We will cover this part in the next steps.
5.1 Adding a Clear Number Button
To add a button that can clear the number, let’s add a clear button to the above XML file:
Then, return to MainActivity.kt to set up the click listener for the call button. This button click will require adding Permissions to actually make a call. First, add the following permission to AndroidManifest.xml:
val buttonCall = findViewById(R.id.button_call)
buttonCall.setOnClickListener {
val numberToCall = display.text.toString()
if (numberToCall.isNotEmpty()) {
val dialIntent = Intent(Intent.ACTION_CALL)
dialIntent.data = Uri.parse("tel:$numberToCall")
startActivity(dialIntent)
}
}
6. Conclusion
– Now, a basic keypad app has been completed. By utilizing UI components in Kotlin and Android, we were able to create a simple phone app. I hope this course has helped you learn the basics of Android app development.
– Additionally, implementing various features can help you gain more experience and develop it into an app that can be used in real services.
I hope this course has been helpful, and I wish you success on your Android development journey!