Android Button Example- How to add button in android application.

Button is a very common component in user interface.You can write text or put any icon on button so user can understand what action occurs when they press or touches it.
In Android programming to display a normal button “android.widget.Button” class is used.
I will show you, How to add button in android application and the use of click listener. When user touches the button then the URL which is written in “onClick listener” opens in a default browser.
Now, the steps for creating this application are :

  1. Design your UI in .xml file.
  2. Create a java file.
  3. Add onClick listener in java file.

Your activity_main.xml file :

<?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 - Go to www.vivekbarot.com" />

</LinearLayout>

Once you Design your UI in “activity_main.xml” file, Write your code in “MainActivity.java” file.

Your MainActivity.java file:

package com.example.button;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}

private void addListenerOnButton() {
// TODO Auto-generated method stub

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("http://www.vivekbarot.com"));
startActivity(browserIntent);

}

});

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Now run it as Android application.

 Here is the first view of applicatin :


Now, This is second view of application when you touches the button.


Android Hello world Example – How to write “Hello world” app in android.

Now  a days Android is a most popular operating system led by Google.
In this tutorial i will show you the list of basic android programs to get you start programming in android.

Basic Steps to Develop an Android Application :

  1. Install Android SDK
  2. Install ADT Eclipse plugin
  3. Create an Android Virtual Device (AVD)
  4. Create Android Project with Eclipse (Wizard)
  5. Code it…
  6. Start it in Android Virtual Device (AVD)

Android Hello World Example :

In core Android programming if you want to write hello world program you have to create one textview and its object (here “text”).
Now set the object value as Hello Wold,as shown in below program.

HelloWorldActivity.java :

package com.example.helloworld;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Hello World");
setContentView(text);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Run this as a Android Application 
Here, if you press “Home” button then you can see your hello world application is successfully deployed in your AVD (Android Virtual Device).