Table of Contents
Button In Android – Introduction
Button In Android –
Create activity_button_activity 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 In Android – Output
Check Out the Headphones – 50% to 70% Off
Check Out the Earbuds – 50% to 70% Off
Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Check Out The LAB Configuration eBook – Class A, Class B, Class C, MiN worth $78 Get at $18