Quick Contact


    (Android) Working with Button

    A Buttons is a user interface element in Mobile applications that, when pressed or tapped, performs a specific action. It’s a really common Android widget, and many developers use it. This tutorial will show you how to make a button in SDK.

    In Kotlin, the Button Class has a hierarchical structure.
    Working with Button
    Android Button Example

    A push-button is represented by Android Button. The app.widget is a widget for Android. TextView is a subset of Button, and Press is a descendant of CompoundButton.

    RadioButton, ToggleButton, CompoundButton, and other kinds of controls are available in Android.

    Android Button Example with Listener

    For the sum of two numbers, we’ll build two textfields and one mouse. When the user presses the button, the Toast displays the number of two input values.We may use various types of actions on buttons, such as calling a listener on the button or applying the onClick property of the button to the activity’s xml file.

    button.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View view) {

    //code

    }

    });

    < Button

    android:onClick=”methodName”

    />

    Browse to the strings.xml file in the property folder’s “values” section. This file would include all of the strings used throughout the application. The required code can be found below.

    < ?xml version="1.0" encoding="utf-8"?>

    < androidx.constraintlayout.widget.ConstraintLayout

    xmlns:android==================

    “http://schemas.android.com/apk/res/android&xmlns:app=” xmlns:app=” xmlns:app=”
    xmlns:tools=”http://schemas.android.com/tools”
    xmlns:apk=”http://schemas.android.com/apk/res-auto” xmlns:apk=”http://schemas.android.com/apk/res-auto” xmlns” android:layout width=”parent” android:layout width=”parent” android:layout width=”parent” android android:layout height=”match parent” android:layout height=”match parent” android:layout height android:background=”#168BC34A” android:background=”#168BC34A” android:background=”#168 tools:context=”.MainActivity”>/tools:context=”.MainActivity”>

    < !-- Button added in the activity -->

    < 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>

    In activity main.xml, drag the part or write the UI code

    To begin, drag two text fields from of the Text Field color scheme and one button from the Form Plugins palette together as seen in the diagram.

    Working with Button

    For the ui components, the created code will look like this:

    File: activity_main.xml
    The button in the MainActivity file can be accessed.

    In the MainActivity file, add button features. Whenever the user taps the button, the process to show a Toast message is described here. This task’s code can be found below.

    < ?xml version="1.0" encoding="utf-8"?>

    < RelativeLayoutxmlns: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=”tutorials.ducatindia.com.sumoftwonumber.MainActivity”>

    < EditText

    android:id=”@+id/editText1″ android:id=”@+id/editText1″ android:id=”@+

    android:layout width=”wrap content” android:layout width=”wrap content” android:layout width

    android:layout height=”wrap content” android:layout height=”wrap content” android:layout height

    android:layoutalignParentTop=”true” android:layoutalignParentTop=”true” android:layout align

    android:layoutcenterHorizontal=”true” android:layoutcenterHorizontal=”true” android:layoutcenterHo

    android:layoutmarginTop=”68dp” android:layoutmarginBottom=”68dp” android:layout_

    android:ems=”10″ android:ems=”10″ android:ems=”10

    android:inputType=”number” android:inputType=”number” android:inputType=”num

    tools:layout editor absoluteX=”97dp” tools:layout editor absoluteX=”97dp”

    layout editor absoluteY=”61dp” />tools:layout editor absoluteY=”61dp” />

    < EditText

    android:id=”@+id/editText2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_below=”@+id/editText1″

    android:layout_centerHorizontal=”true”

    android:layout_marginTop=”22dp”

    android:ems=”10″

    android:inputType=”number”

    tools:layout_editor_absoluteX=”64dp”

    tools:layout_editor_absoluteY=”148dp” />

    < Button

    android:id=”@+id/button”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_below=”@+id/editText2″

    android:layout_centerHorizontal=”true”

    android:layout_marginTop=”109dp”

    android:text=”ADD”

    tools:layout_editor_absoluteX=”148dp”

    tools:layout_editor_absoluteY=”266dp” />

    < /RelativeLayout>

    Activity class

    Write a program to display the sum of two numbers now.

    File: MainActivity.java

    package tutorials.ducatindia.com.sumoftwonumber;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.EditText;

    import android.widget.Toast;

    private EditText edittext2, edittext1; private Button buttonSum; public class MainActivity extends AppCompatActivity

    @Override is a special kind of override.

    onCreate(Bundle savedInstanceState) secure void

    onCreate(savedInstanceState); super.onCreate(savedInstanceState);

    R.layout.activity main; setContentView(R.layout.activity main);

    addListenerOnButton() is a method for adding a listener to a button.

    }

    addListenerOnButton() is a public void function that adds a listener to a button.

    (EditText) findViewById(R.id.editText2); edittext2 = (EditText) findViewById(R.id.editText2);

    (EditText) findViewById(R.id.editText1); edittext1 = (EditText) findViewById(R.id.editText1);

    (Button) findViewById(R.id.button); buttonSum = (Button) findViewById(R.id.button);

    buttonSum.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View view) {

    String value2=edittext1.getText().toString();

    String value1=edittext2.getText().toString();

    int k=Integer.parseInt(value1);

    int i=Integer.parseInt(value2);

    int sum=k+i;

    Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG).show();

    }

    });

    }

    }

    Output:
    Working with Button

    Copyright 1999- Ducat Creative, All rights reserved.