All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Button In Android Studio

Last Updated : Mar 11, 2024

Button In Android Studio

In this article we will show you the solution of button in android studio, android Button represents a push-button. The android.widget.In the Button class we have a subclass called CompoundButton, and in the TextView class we have a subclass called Button.

RadioButton, ToggleButton, CompoundButton and other types of buttons are available in Android.

Different types of action can be performed on buttons, such as calling listeners on buttons or adding onClick properties to activities.

Step By Step Guide On Button In Android Studio :-

Xml code

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#168BC34A"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#4CAF50"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:text="@string/btn"
        android:textColor="@android:color/background_light"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Java code

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity {
    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.create(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        if (button != null) {
            button.setOnClickListener((View.OnClickListener)(new View.OnClickListener() {
                    public final void onClick(View it) {
                    Toast.makeText((Context)MainActivity.this, R.string.message, Toast.LENGTH_LONG).show();
                }
            }));
        }
    }
}
  1. Go to the Projects tab and create a new one
  2. New => New Project can be found in the File menu.
  3. The template for the project should be "Empty Activity".
  4. Kotlin should be selected as the language.
  5. You can choose the minimum SDK based on your needs.
  6. 2. Make modifications to strings.xml, select it and modify it file under the “values” directory of the resource folder. This file will contain all strings that are used in the Application. <resources> <string name="app_name">GfG | Button In Kotlin</string> <string name="btn">Button</string> <string name="message">Hello !! This is a Button.</string> </resources>
  7. Modify the activity_main.xml file, Add a button widget in the layout of the activity.
  8. Accessing the button in the MainActivity file, Add functionality of button in the MainActivity file. Here describe the operation to display a Toast message when the user taps on the button.

Conclusion :-

Android Button represents a push-button. The android.widget.In the Button class we have a subclass called CompoundButton, and in the TextView class we have a subclass called Button.

I hope this article on button in android studio helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪