Buttons In Android App


Buttons are an important UI element in an Android Application. They are used to record user actions. eg If you are making a messaging app,a Button can be used to trigger a send command.


Buttons are available as part of standard library in android UI widgets.


Here's an example code to use Put a button in an Coordinator Layout and to record a button press event.


eg


MainActivity.java:

package com.ourcoaching.simplebuttonclick;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private Button Button_1,Button_2;
private String text1="I Clicked first button";
private String text2="I Clicked second button";
private String text3="I clicked textview";

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

textView=(TextView)findViewById(R.id.textView);
Button_1=(Button)findViewById(R.id.button);
Button_2=(Button)findViewById(R.id.button2);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShowToast();
}
});

Button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChangeText(text1);
}
});

Button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChangeText(text2);
}
});

}

private void ShowToast() {
Toast.makeText(this,text3,Toast.LENGTH_SHORT).show();
}


private void ChangeText(String Newtext) {
textView.setText(Newtext);
}

}


activity_main.xml:

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.ourcoaching.simplebuttonclick.MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="190dp"
android:layout_height="76dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.148" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.253"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.534" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.742"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.534" />

android.support.constraint.ConstraintLayout
>


UI Screen:


Download Code from GITHUB



Related Tutorials