Android Button Example

0

In this article I am going to show you an example of android button. A button is the most common widget in any graphical user interface.
In this example, We will create a button, add some text or an icon to it and then attach a listener.

Add Button to the Layout

Add a text field and a button to the layout. We will enter website name in the text field and then click on the button to open it. The button contains some text as well as an icon. The icon is set against the attribute android:drawableEnd.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.javarticles.android.MainActivity" >

    <EditText
        android:id="@+id/site"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="78dp"
        android:hint="@string/site"
        android:textSize="20sp" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/open_site"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/site"
        android:layout_alignTop="@+id/site"
        android:layout_marginTop="31dp"
        android:drawableEnd="@drawable/open_site"
        android:drawablePadding="20sp"
        android:text="@string/opensite_btn" />

</RelativeLayout>

Attach a listener to the button

Use button.setOnClickListener to set a listener on the button. When clicked, it should open the site address entered in the text box.

package com.javarticles.android;

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

public class ButtonExampleActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.button_layout);		
		Button openSiteBtn = (Button) findViewById(R.id.open_site);
		final EditText textField = (EditText) findViewById(R.id.site);
		openSiteBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String site = textField.getText().toString();
				if (!site.startsWith("http://")) {
					site = "http://" + site;
				}
				Intent openSite = new Intent(Intent.ACTION_VIEW, Uri.parse(site));
				startActivity(openSite);
			}
		});
	}	
}

Run the application

You should see a text box and a button.

Open Site

Open Site

Enter javarticles.com in the text box and click on the button to open the site in a browser.

Open javarticles.com

Open javarticles.com

The site gets launched in a browser.

Javarticles.com in browser

Javarticles.com in browser

Download the Source Code

This was an example of android button. You can download the source code here: buttonExample.zip

Share.

Comments are closed.