What Is Button | Button Tutorial With Examples In Android Studio

Button Tutorial – Introduction

Button Tutorial – A button is user interface control is used to perform an action whenever the user presses the button. You can redirect users to whenever screen/activity you want. You can send the user to other activities as well as website/store etc. Generally, buttons contain text to guide people about what action should they take. There are two types of buttons, one is text on it, another is an image button. Here is the example of the normal/text button, in the next article we will see the image button example.

Create activity_button file in 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”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_marginTop=”30dp”
android:orientation=”vertical”
android:gravity=”center”
tools:context=”.buttonActivity”>

<Button
android:id=”@+id/buttonAct1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”This is our First Button” />

<Button
android:id=”@+id/buttonAct2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:text=”This is our Second Button”
android:backgroundTint=”@color/teal_200″
android:textColor=”@color/design_default_color_error” />

<Button
android:id=”@+id/buttonAct3″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:text=”This is our Third Button”
android:textSize=”20sp”
android:backgroundTint=”@color/black”
android:textColor=”@color/cardview_light_background” />

<Button
android:id=”@+id/buttonAct4″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:text=”This is our Fourth Button”
android:textSize=”20sp”
android:textStyle=”bold”
android:textAllCaps=”false” />

</LinearLayout>

Create buttonActivity file in Java

package com.eiheducation.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class buttonActivity extends AppCompatActivity {

Button buttonAct1,buttonAct2,buttonAct3,buttonAct4; // declared the button objects

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);

// We need to find our elements by IDs

buttonAct1 = findViewById(R.id.buttonAct1);
buttonAct2 = findViewById(R.id.buttonAct2);
buttonAct3 = findViewById(R.id.buttonAct3);
buttonAct4 = findViewById(R.id.buttonAct4);

// button code

buttonAct1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(buttonActivity.this, “You clicked on Button 1”, Toast.LENGTH_SHORT).show();
}
});

buttonAct2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(buttonActivity.this, “You clicked on Button 2”, Toast.LENGTH_SHORT).show();
}
});

buttonAct3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(buttonActivity.this, “You clicked on Button 3”, Toast.LENGTH_SHORT).show();
}
});

buttonAct4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(buttonActivity.this, “You clicked on Button 4”, Toast.LENGTH_SHORT).show();
}
});

}
}

Android Manifest file

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.buttonActivity” />
</application>

</manifest>

Button Tutorial – Output

Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Online Surveys – Earn Real Money by Online Surveys
Kids Books – 10+ books for Kids | Age group 2 – 10 | Improve knowledge & Productivity

Leave a Reply

Your email address will not be published.