KEMBAR78
Lecture-3-Mobile Application Development | PDF | Button (Computing) | Android (Operating System)
0% found this document useful (0 votes)
27 views16 pages

Lecture-3-Mobile Application Development

The document provides a step-by-step guide on adding a button in Android Studio, including creating a new project and activity, modifying XML code for the button, and implementing its functionality in the MainActivity.kt file. It highlights the use of attributes to customize the button and explains how to display a toast message when the button is clicked. Additionally, it emphasizes the importance of changing package and button names to match the user's application settings.

Uploaded by

noor003awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views16 pages

Lecture-3-Mobile Application Development

The document provides a step-by-step guide on adding a button in Android Studio, including creating a new project and activity, modifying XML code for the button, and implementing its functionality in the MainActivity.kt file. It highlights the use of attributes to customize the button and explains how to display a toast message when the button is clicked. Additionally, it emphasizes the importance of changing package and button names to match the user's application settings.

Uploaded by

noor003awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

ADDING A BUTTON IN Lecture 3

ANDROID STUDIO
CLICK ON NEW AND THEN
NEW PROJECT
SELECT EMPTY VIEWS
ACTIVITY
CREATING NEW ACTIVITY
In name enter a new name
Select location from save
location
Use an option from minimum
SDK, you can choose the default
settings
Select Kotlin DSL from
configuration language
FRONTEND AND BACKEND
FILES
There are several files created
for various purposes
For the task of creating a new
button, we shall be working on
activity_main.xml file for
frontend and MainActivity.kt file
for backend
ACIVITY_MAIN.XML (DESIGN)
ACTIVITY_MAIN VIEW
OPTIONS
There are three options to view
xml file
1. Code (shows only the code)
2. Split (shows design and code
side by side)
3. Design (display only the
design window)
These options are highlighted in
previous slides and zoomed in in
the given image
ADDING A BUTTON
You can click on button option
and drag and drop it on the given
interface or you can add it from
xml code as well.
ATTRIBUTES
You can change properties of any
element that you add in you
application from the attribute
option present at the right side of
the screen.
Make sure you select the
particular object before you
proceed to change its attributes
CREATING A BUTTON
Here we dragged and
dropped a button
Changed its text to RUN
Changed its id to Button_1
Changed its text
appearance to large from
the list of options (scroll
down the attributes and you
will find the options)
XML CODE (YOU CAN COPY
AND PASTE)
<?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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/Button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/self_destruct"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
tools:layout_editor_absoluteX="156dp"
tools:layout_editor_absoluteY="255dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
MAINACTIVITY.KT
package com.example.class_button

import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import android.widget.Toast
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()

setContentView(R.layout.activity_main)
val button_1 = findViewById<Button>(R.id.Button_1)
button_1.setOnClickListener {
Toast.makeText(this,"Button Clicked!", Toast.LENGTH_SHORT).show()
}
}

}
IMPORTANT NOTE
Remember to change the name of package and buttons with the
respective names that you have given in your app
TOAST
Toast is an option that allows an application to display a message
for a few seconds
This message does not need to be closed and disappears on its own
Toast.makeText(this,"Button Clicked!",
Toast.LENGTH_SHORT).show() is used to display the message and
we need to import android.widget.Toast library to use it
OUTPUT
Here is the output interface of
the app
OUTPUT TOAST
Here you can see the message
“Button Clicked” is being
displayed that will disappear on
its own

You might also like