- HubPages»
- Technology»
- Communications»
- Smartphones»
- Android Phones
Android button application example
To display a normal button, we need the android.widget.Button class. It represents a push-button widget. Push-buttons can be pressed, or clicked, by the user to perform an action. In this tutorial, we will create a button and display it to the activity. Then we will link it to any website to go. When user clicks on the button, it will open an URL in the Android’s default Internet browser.
Steps to create Android button application
Our goal is to create an Android application using button. Follow the following steps to create the application using button:
Step 1: Creating an Android button application project
Create an Android Application project using Android ADT tool. After successful creation of the project, it looks like the following structure.
Step 2: Create a button
Add a button in the layout. Open res/layout/activity_main.xml file, add a button. Give the button an id and add the string which the button will be displayed during launch. The code of the xml file would look like the following:
<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="com.example.androidbuttionapplication.MainActivity$PlaceholderFragment" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/buttonID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/buttonGoToGoogle" /> </RelativeLayout>
Step 3: Add Java code
Now, attach a click listener for the button which we created earlier. Now, our goal is, clicking on the button it will start a website like www.google.com using mobile browser and you will see that website.
package com.example.androidbuttionapplication; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.buttonID); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri .parse("http://www.google.com")); startActivity(browserIntent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
Step 4: Run the application
You may run the application on Android ADT or on your Android mobile / tablet. The application will look like the following:
Now, if you click on the button, Google will be opened on your android device using the default browser of your Android device.
Tips and Warnings:
- In some cases URL may start with "www". In this case you will get an exception : android.content.ActivityNotFoundException: No Activity found to handle Intent
- The URL must always start with "http://" or "https://". So, a good approach would be like the following code snippets:
if (!url.startsWith("https://") && !url.startsWith("http://")){ url = "http://" + url; } Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(openUrlIntent);