Android tutorial for beginners:using Button, Listener and Toast
This tutorials describes the usage of Buttons.we will start the course with a quick summary of the basic functions.
What is a Listener?
A listener, as the name suggests, listens to a source until it is used.
For an action to be performed, it is necessary that a transmitter sends a signal to the listener that is triggered.
Action: Click on a button
Listener: Listen when the button is clicked
There is no simpler than that, we will see a later in the examples how to implement a listener.
Creating our project
As in the previous tutorial, with Eclipse and ADT, create a new project. We'll start by editing the "activity_main.xml" file located in the "res / layout" folder.
Its current syntax is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>
We will remove the textview and replace it with a button to see the functioning of the listener.
We are then left with the following syntax:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/buttonToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click here !" />
</LinearLayout>
Open the file "MainActivity.java" found at the "src"folder, and put this code:
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } }
Currently, if we run our application and you click the button, nothing will happen. To fix this, we'll add code to set the action and this is where listeners come into play.
We can not directly call our button in our Activity, we have to get its ID from the XML file.
An activity has a findViewById method to call an element of sight, with a parameter the identifier of the object you wish to call. It is stored in the ResourcesManager which is called R.
Button myButton = (Button)findViewById(R.id.buttonToast);
A button cannot process clicks by itself, it needs a listener for this, which is assigned using setOnClickListener method. When a button is clicked, listener reacts and runs the code from onClick method.
This method takes an OnClickListener,our application does not have this instance, we will give it one like this:
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
This interface implements the onClick method with the used view in parameter, here the view is our button.
We will add a short message to be displayed when you click the button, messages are displayed via a class named Toast .
The Toast class has a static method (no need to instantiate the object to use its methods) called makeText , which takes as parameters the context (our current Activity), then the message and finally a period (time).
The time can be Toast.LENGTH_SHORT or Toast.LENGTH_LONG.
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Hello !", Toast.LENGTH_SHORT).show();
}
Launch the application and look at the message displayed when you click the button.
Merci Bien
RépondreSupprimer