Simple Button

Simple Button

Imagine a button in an app that you press to perform an action like Login, Submit, or Download.
In Android, this clickable component is called a Button.

Example:

[ Login ]
[ Submit ]
[ Register ]

When the user taps the button → an action happens.


1. Definition

A Button is a UI component in Android that users can click or tap to perform an action.

Simple meaning:

Button = A clickable UI element that triggers an action.

2. Purpose

Buttons are used to:

  • Perform actions

  • Submit forms

  • Navigate between screens

  • Trigger events

Examples in apps:

App ScreenButton Action
Login pageLogin button
RegistrationSubmit button
Music appPlay button
Shopping appBuy Now button

3. Basic Syntax (XML)

Example Button in layout:

<Button
    android:id="@+id/btnSubmit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"/>

Explanation:

  • id → unique identifier

  • layout_width → button width

  • layout_height → button height

  • text → button label

Result:

[ Submit ]

4. Using Button in Kotlin Code

Example in Activity:

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.btnSubmit)

        button.setOnClickListener {
            println("Button Clicked")
        }
    }
}

Explanation:

  • findViewById() connects Kotlin to XML

  • setOnClickListener detects button click


5. Important Properties

1. text

Sets button text.

android:text="Login"

2. textColor

Changes text color.

android:textColor="#FFFFFF"

3. background

Sets background color.

android:background="#2196F3"

4. padding

Adds space inside button.

android:padding="10dp"

5. enabled

Enables or disables button.

android:enabled="true"

Disabled example:

[ Submit ] (greyed out)

6. layout_margin

Adds space outside button.

android:layout_margin="10dp"

6. Important Methods (Kotlin)

setOnClickListener()

Handles click event.

button.setOnClickListener {
    println("Button pressed")
}

setText()

Changes button text.

button.text = "Clicked"

setEnabled()

Enable or disable button.

button.isEnabled = false

setVisibility()

Show or hide button.

button.visibility = View.GONE

Options:

VISIBLE
INVISIBLE
GONE

7. Real-Time Examples

Example 1: Login Button

XML:

<Button
    android:id="@+id/btnLogin"
    android:text="Login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Result:

[ Login ]

Example 2: Show Message on Button Click

Kotlin code:

val button = findViewById<Button>(R.id.btnSubmit)

button.setOnClickListener {
    println("Form Submitted")
}

Example 3: Change Button Text

button.setOnClickListener {
    button.text = "Processing..."
}

Result:

[ Processing... ]

Example 4: Register Form

Name      [________]
Email     [________]

          [ Register ]

Button is used to submit form data.


8. Advantages

✔ Easy to use
✔ Supports click events
✔ Highly customizable
✔ Essential for user interaction


9. Disadvantages

❌ Requires event handling code to perform actions


10. One-Line Summary

A Button is a clickable Android UI component used to perform an action when the user taps it.



Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form