Monday, May 3, 2021

Android Button

 

Android Button Example

android button

Android Button represents a push-button. The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class.

There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc.


Android Button Example with Listener

Here, we are going to create two textfields and one button for sum of two numbers. If user clicks button, sum of two input values is displayed on the Toast.




We can perform action on button using different types such as calling listener on button or adding onClick property of button in activity's xml file.



  1. button.setOnClickListener(new View.OnClickListener() {  
  2.             @Override  
  3.             public void onClick(View view) {  
  4.                //code  
  5.             }  
  6. });  
  1. <Button  
  2.         android:onClick="methodName"  
  3. />  

Drag the component or write the code for UI in activity_main.xml

First of all, drag 2 textfields from the Text Fields palette and one button from the Form Widgets palette as shown in the following figure.

android button example

The generated code for the ui components will be like this:

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.com.sumoftwonumber.MainActivity">  
  8.   
  9.     <EditText  
  10.         android:id="@+id/editText1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_alignParentTop="true"  
  14.         android:layout_centerHorizontal="true"  
  15.         android:layout_marginTop="61dp"  
  16.         android:ems="10"  
  17.         android:inputType="number"  
  18.         tools:layout_editor_absoluteX="84dp"  
  19.         tools:layout_editor_absoluteY="53dp" />  
  20.   
  21.     <EditText  
  22.         android:id="@+id/editText2"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_below="@+id/editText1"  
  26.         android:layout_centerHorizontal="true"  
  27.         android:layout_marginTop="32dp"  
  28.         android:ems="10"  
  29.         android:inputType="number"  
  30.         tools:layout_editor_absoluteX="84dp"  
  31.         tools:layout_editor_absoluteY="127dp" />  
  32.   
  33.     <Button  
  34.         android:id="@+id/button"  
  35.         android:layout_width="wrap_content"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_below="@+id/editText2"  
  38.         android:layout_centerHorizontal="true"  
  39.         android:layout_marginTop="109dp"  
  40.         android:text="ADD"  
  41.         tools:layout_editor_absoluteX="148dp"  
  42.         tools:layout_editor_absoluteY="266dp" />  
  43. </RelativeLayout>  

Activity class

Now write the code to display the sum of two numbers.

File: MainActivity.java
  1. package example.com.sumoftwonumber;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.     private EditText edittext1, edittext2;  
  12.     private Button buttonSum;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         addListenerOnButton();  
  20.     }  
  21.   
  22.     public void addListenerOnButton() {  
  23.         edittext1 = (EditText) findViewById(R.id.editText1);  
  24.         edittext2 = (EditText) findViewById(R.id.editText2);  
  25.         buttonSum = (Button) findViewById(R.id.button);  
  26.   
  27.         buttonSum.setOnClickListener(new View.OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View view) {  
  30.                 String value1=edittext1.getText().toString();  
  31.                 String value2=edittext2.getText().toString();  
  32.                 int a=Integer.parseInt(value1);  
  33.                 int b=Integer.parseInt(value2);  
  34.                 int sum=a+b;  
  35.                 Toast.makeText(getApplicationContext(),String.valueOf(sum), Toast.LENGTH_LONG).show();  
  36.             }  
  37.         });  
  38.     }  
  39. }  

Output:

android button example 2

..

Audio/ Sound Project

 ..Mainfest.xml <? xml version ="1.0" encoding ="utf-8" ?> < manifest xmlns: android ="http://schemas.andr...