Android Studio Button Listener Key Listener And Browse Url Example

This example will show you how to use android studio to create and register Button OnClickListener and Text Input Box OnKeyListener object to listen to the specified event. And it also show you how to open web browser to browse a web page url.

Below is the UI when this app startup, you can see there has two view component, one is a input text box the other is a button. If user input a web page url, when he click the Browse button or press enter key, the app will open a web browser to browse the url page.

android onclicklistener and onkeylistener ui

1. Add example UI components.

  1. Right click the project name, click ” New —> Activity —> Empty Activity ” menu item to create a new activity.
    create new android activity
  2. Input the activity name as below picture, and layout name is generated automatically, please do not change.
    create new activity settings dialog
  3. Click Finish to complete the wizard. Now you can see the activity java file and the layout xml file in left tree panel, double click the layout xml file, click bottom Design tab. Then you can add LinearLayout component in the root ConstraintLayout.
    add linear layout in android layout file
  4. Add EditText and Button view components in the LinearLayout component as below.
    add android input text box and button component

2. Edit layout and activity file content.

Click bottom Text tab, edit above layout, edit text and button component’s properties as below.



            android:text="Input desired web page url"
            android:textStyle="bold" />

            android:text="Browse" />

package com.dev2qa.www.helloworldexampleproject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ButtonListenerExampleActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        // Set content view to use the new layout xml file.

        // Get the page url input component.
        EditText editTxtPageUrl = (EditText)this.findViewById(;
        // Create page url text on key action listener.
        PageUrlTextKeyListener pageUrlTextKeyListener = new PageUrlTextKeyListener();
        // Regist text box onkey listener object.

        // Get the browse button component.
        Button btnBrowsePage = (Button)this.findViewById(;
        // Create button click listener.
        BrowseButtonListener browseButtonListener = new BrowseButtonListener();
        // Regist button click listener object.

// This class listen to browse button click event.
class BrowseButtonListener implements View.OnClickListener
    // Reference to owner activity's EditText view to get user input text.
    private EditText editTxtPageUrl = null;

    public void setEditTxtPageUrl(EditText editTxtPageUrl) {
        this.editTxtPageUrl = editTxtPageUrl;

    public void onClick(View v) {
            // If user click Browse button then open above page url in a web browser.
            String pageUrl = this.editTxtPageUrl.getText().toString();
            // Browse the provided web page url.
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(pageUrl));
            // Use view component's context to start browser.

// This class listen to EditText input box key event.
class PageUrlTextKeyListener implements View.OnKeyListener
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // Convert the v to EditText object.
        EditText editTextPageUrl = (EditText)v;
        // Get the input text.
        String pageUrl = editTextPageUrl.getText().toString();

        // If the text is default text then clear the EditText input box.
        if("Input desired web page url".equalsIgnoreCase(pageUrl))
            return false;
        }else {
            // If user press enter key.
            if (KeyEvent.KEYCODE_ENTER == keyCode) {
                // Browse the provided web page url.
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(pageUrl));
                // Use view component's context to start browser.
                return true;
            return false;

If you do not remember the override method name or imported java class name, you can click code menu item in top tool bar. The drop down menu list has a lot of helper menus to generate common codes.

android studio override method

3. Eidt AndroidManifest.xml and run the example.

Before you can run above example, you should edit AndroidManifest.xml as below. This will launch the newly created activity by default.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""


        <activity android:name=".ButtonListenerExampleActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Right click the new activity class name not the java file name, click “Run ButtonListenerExampleActivity” menu, after some time, you can see the device simulator startup and show the UI at this article beginning. If you enter a correct web url and click enter key or click the Browse button, it will popup a web browser and then browse the web page.

run newly created activity class

[download id=”2852″]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.