Android Button onClickListener Example

Android Button onClickListener Example

Many of you are trying to code hello world application in Android. Here we will see how to add button  to Android application layout and attach onClick event to it. Import android.widget.Button package to use Button.

Add Button with id button1 to Android application layout.

<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: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=".ButtonDemoActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="78dp"
        android:text="Click to navigate" />

</RelativeLayout>

 

Source Code looks like below one.

 

package com.example.buttondemo;

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

public class ButtonDemoActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_demo);
        addButtonClickListner();
    }
    public void addButtonClickListner()
    {
        Button btnNavigator = (Button)findViewById(R.id.button1);
        btnNavigator.setOnClickListener(new OnClickListener(){
            public void onClick(View arg)
            {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.satalaj.com"));
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.button_demo, menu);
        return true;
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Android screen navigation example using Activity and IntentAndroid WebView ExampleAndroid Button ClickAndroid EditText TutorialJQuery Radio Button CheckedAndroid Tutorial For BeginnersBest Android ApplicationsJquery Radio Button CheckedJquery button click eventC# MemoryStream ExampleHow to earn money with android phone

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.