Android tutorial - Android Button - android app development - android studio - android development tutorial



 Android button example
  • 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.
 Android button example
android programming - android software - android emulator

Add Button

  • Open “res/layout/main.xml” file, add a button.

File : res/layout/main.xml

android codes - android tutorial - android sdk
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button - Wikitechy.com" />

</LinearLayout>
click below button to copy the code from android tutorial team

2. Code

  • Attach a click listener to the button.
  • When user click on it, open mobile browser and display

2. Code URL : https://www.wikitechy.com. File : MyAndroidAppActivity.java

android codes - android tutorial - android sdk
package com.wikitechy.android;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		addListenerOnButton();

	}

	public void addListenerOnButton() {

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

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			  Intent browserIntent =
                            new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.wikitechy.com"));
			    startActivity(browserIntent);

			}

		});

	}

}
click below button to copy the code from android tutorial team

Demo - android emulator - android tutorial

  • Run the application.
  • Result, a normal button.
 Android button example
  • Click on the button, display URL in browser.
 Android button example

Related Searches to Android Button