Android Tutorial: Adding a button and code in Android application with Eclipse

SEnukeTNG Rank #1 in Google, Download Now!

Here We are going to show you in this tutorial how to add a button to a layout of an Android  application in Eclipse IDE . We explain how to add code to the event “click” (when the user clicks the button with your finger on mobile Android ) as an example add the necessary to open the default browser and navigate to a code URL concrete.

Requirements to develop application for Android devices Eclipse:

The following link will show a tutorial where we explain step by step how to install Eclipse and how to develop an application for devices with Android :

My first Android application with Eclipse in Linux, Ubuntu install Eclipse .

Add a button to a layout and add code in the onClick event
Add a button from Layout or Graphical XML file manually from

Then explain how to add a (Button) button to a app Android in Eclipse , first we will have created the layout of our application (main window). In this layout that can be opened from “res” -. “Layout”, we double click on “activity_ajpdsoft_linterna.xml” (can be any other name) Eclipse graphical editing will show the layout with the possible components to add, in the group “Form Widgets” from the “Palette” window select “Button”:

 

Add a button to a layout and add code in the onClick event

And drag to place the layout where you want it to appear:

Add a button to a layout and add code in the onClick event

Selecting the New button, Eclipse will show on the right side of the “Properties” window indicate where the two most important properties:

Id: Button that identifies the name to reference it in Java code, for example “@ + id / btURL” (always indicate “@ + id / nombre_identificativo”

Add a button to a layout and add code in the onClick event

  • The other important property for the Button will be the text to be displayed to the user, we have two possibilities:

–   We can write directly on the “Text” property (not recommended).
–  Before assigning a text to create a Button “Value” in “Resources”. To do so, follow these steps, we will  open the following folders in “Project Explorer”, “res” – “values”, we double click on “strings.xml”. Eclipse on the right side will show the current values, you press “Add”:

Add a button to a layout and add code in the onClick event

–  Select “String” and press “OK”:

Add a button to a layout and add code in the onClick event

– Introduce an identifying name for the new String “Name”, for example “text_url” and a value in “Value”, eg “Visit website”. Close the file “strings.xml” and store the changes:

Add a button to a layout and add code in the onClick event

In fact all we’ve done is add the following line to the file “strings.xml”

<string name=”text_url”> Visit site </ string>

Once you add the string back to the layout, select the Button and the “Text” property will press the “…” button:

Add a button to a layout and add code in the onClick event

 

In the selection window resource (Resource Chooser) select the previously added string “text_url”

Add a button to a layout and add code in the onClick event

Thus from “Graphical layout” and we added to the main application window button Android :  Thus from “Graphical layout” and we added to the main application window button Android :

Add a button to a layout and add code in the onClick event

Like almost everything in Eclipse , we can see and modify and even add any component directly editing the layout for the file by clicking on the bottom of the file opened in xxx.xml layout, see the code XML necessary to add any component of our application , framed in red the code XML to the graphic designer added automatically to our button.

Add a button to a layout and add code in the onClick event

The code XML to add a button in Eclipse applications Android :

<Button
android: id = “@ + id / btURL”
android: layout_width = “wrap_content”
android: layout_height = “wrap_content”
android: layout_below = “@ + id / btAjpdSoft”
android: layout_centerHorizontal = “true”
android: layout_marginTop = “22DP”
android: text = “@ string / text_url” />

Assign code onClick event of a button in Eclipse for Android applications

We will open the file xxx.java our application to be in the “Project Explorer” in the “src” folder, in our case “AjpdSoftLinterna.java”. To work with a Button we have an import to the top of the program:

android.widget.Button import;

In the application class add the following line:

private Button btURL;

(Where “btURL” is the name we have assigned to the button to create the layout, the “Id” property).

Assign code onClick event of a button in Eclipse for Android applications

Finally add in “public void onCreate …” the following lines of Java code to create the OnClick event, which will be the code that runs when the user clicks the button:

btURL = (Button) findViewById (R.id.btURL);
btURL.setOnClickListener (new OnClickListener ()
{

@ Override
public void onClick (View v)
{
Intent browserIntent =
new Intent (Intent.ACTION_VIEW,
Uri.parse (“http://www.ajpdsoft.com”));
startActivity (browserIntent);
}
});

The above code will allow when the user clicks the button the default browser will open and navigate to the URL specified. To make the above code should be added two imports at the beginning:

android.net.Uri import;
android.content.Intent import;

Now test the application in the emulator, for that press in “Run” – “Run”:

Assign code onClick event of a button in Eclipse for Android applications

If successful the application will run in the emulator, showing our new button “Visit website”:

Assign code onClick event of a button in Eclipse for Android applications

If the user of our application Android developed with Eclipse press the button opens the default browser of our device Android and visit the URL ” www.ajpdsoft.com ”

Assign code onClick event of a button in Eclipse for Android applications

Source: http://www.ajpdsoft.com/

 

 

Be the first to comment

Leave a Reply