Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Working With 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.

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.

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.

buttonclicksum1

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

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <EditText
  7.         android:id=“@+id/editText1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentTop=“true”
  11.         android:layout_centerHorizontal=“true”
  12.         android:layout_marginTop=“24dp”
  13.         android:ems=“10” />
  14.     <EditText
  15.         android:id=“@+id/editText2”
  16.         android:layout_width=“wrap_content”
  17.         android:layout_height=“wrap_content”
  18.         android:layout_alignLeft=“@+id/editText1”
  19.         android:layout_below=“@+id/editText1”
  20.         android:layout_marginTop=“34dp”
  21.         android:ems=“10” >
  22.         <requestFocus />
  23.     </EditText>
  24.     <Button
  25.         android:id=“@+id/button1”
  26.         android:layout_width=“wrap_content”
  27.         android:layout_height=“wrap_content”
  28.         android:layout_centerHorizontal=“true”
  29.         android:layout_centerVertical=“true”
  30.         android:text=“@string/Button” />
  31. </RelativeLayout>

Activity class

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

File: MainActivity.java
  1. package com.example.sumof2numbers;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10. public class MainActivity extends Activity {
  11.     private EditText edittext1,edittext2;
  12.     private Button buttonSum;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         addListenerOnButton();
  18.     }
  19.     public void addListenerOnButton(){
  20.         edittext1=(EditText)findViewById(R.id.editText1);
  21.         edittext2=(EditText)findViewById(R.id.editText2);
  22.         buttonSum=(Button)findViewById(R.id.button1);
  23.         buttonSum.setOnClickListener(new OnClickListener(){
  24.             @Override
  25.             public void onClick(View view) {
  26.                 String value1=edittext1.getText().toString();
  27.                 String value2=edittext2.getText().toString();
  28.                 int a=Integer.parseInt(value1);
  29.                 int b=Integer.parseInt(value2);
  30.                 int sum=a+b;
  31.     Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG).show();
  32.             }
  33.         });
  34.     }
  35.     @Override
  36.     public boolean onCreateOptionsMenu(Menu menu) {
  37.         // Inflate the menu; this adds items to the action bar if it is present.
  38.         getMenuInflater().inflate(R.menu.activity_main, menu);
  39.         return true;
  40.     }
  41. }

It is developed by adt bundle on android 4.2 using minimum sdk 8 and target sdk 16.


Output:

buttonclicksumoutput1

buttonclicksumoutput2

Comments

comments