Exploring Various Types of Buttons in Android Studio: A Beginner's Guide
Are you new to Android Studio and wondering about the different types of buttons you can use in your app? Look no further! In this beginner's guide, we'll explore the various types of buttons available in Android Studio, from basic to more advanced options.
Buttons are a fundamental element of any user interface, allowing users to interact with your app in meaningful ways. Understanding the different types of buttons and how to use them effectively can greatly enhance the usability and aesthetics of your app.
We'll delve into the classic "Button" widget, versatile "ImageButton" for images as buttons, "ToggleButton" for switch-like functionality, "CheckBox" and "RadioButton" for selection, and more. By the end of this guide, you'll have a solid understanding of when and how to use each type of button in your Android app development journey.
Let's dive in and explore the world of buttons in Android Studio together!
Youtube video link:- https://youtu.be/lVIuKScMPMU
Source code:
Kotlin:
package com.example.firstproject
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.Switch
import android.widget.Toast
import android.widget.ToggleButton
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
lateinit var button: Button
lateinit var imageButton: ImageButton
lateinit var toggleButton: ToggleButton
lateinit var switch: Switch
lateinit var floatingActionButton: FloatingActionButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button = findViewById(R.id.button1)
imageButton = findViewById(R.id.imageButton1)
toggleButton = findViewById(R.id.toggleButton1)
switch = findViewById(R.id.switch1)
floatingActionButton = findViewById(R.id.floatingActionButton1)
button.setOnClickListener {
Toast.makeText(this@MainActivity, "Button clicked", Toast.LENGTH_SHORT).show()
}
imageButton.setOnClickListener {
Toast.makeText(this@MainActivity, "imageButton clicked", Toast.LENGTH_SHORT).show()
}
toggleButton.setOnClickListener {
if (toggleButton.isChecked){
Toast.makeText(this@MainActivity, "toggleButton On", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this@MainActivity, "toggleButton Off", Toast.LENGTH_SHORT).show()
}
}
switch.setOnCheckedChangeListener { view, isChecked ->
if (isChecked){
Toast.makeText(this@MainActivity, "switch On", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this@MainActivity, "switch Off", Toast.LENGTH_SHORT).show()
}
}
floatingActionButton.setOnClickListener {
Toast.makeText(this@MainActivity, "floatingActionButton clicked", Toast.LENGTH_SHORT).show()
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="400dp"
android:layout_height="250dp"
android:src="@tools:sample/avatars" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Switch" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/ic_launcher_background" />
</LinearLayout>
Comments
Post a Comment