Well on this time we are going to see how to create the
application with onClick listener in android.For that we need to create the
android application project first.For that double click the eclipse IDE and
goto new android application project .
After that your project will appear in the package Explorer
you need to expand the package explorer on that select the src folder and you
will get the java code such as MainActivity.java after that double click the java file.
Inside the res folder
goto to the layout folder and select the activity_main.Xml file and you need to
enter the coding for android onClick listener.Xml is used for UI design so you
need to create the button in the xml file.
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="90dp"
android:text="Press to start" />
</LinearLayout>
Layout is used to split the partition in android.And in
the layout you are creating the Button and after that you are assigning the
width and size for your button.
Thats it you have been created the button now you need
write the backend process of button by using java code.
Now open the src folder and MainActivity.java file.Now
insert these coding in the java file.
public class Emmozhi extends Activity {
protected void onCreate(Bundle
savedInstance)
{
super.onCreate(savedInstance);
setContentView(R.layout.activity_main);
Button main_button=(Button)findViewById(R.id.button_1);
Main_button.setOnClickListener(new View.OnClickListener()
{
public void
onClick(View arg0) {
Toast.makeToast(getApplicationContent,”you clicked the
button”,Toast.LENGTH_LONG).show();
}
});
}
}
First you are creating the button
instance in the oncreate method then you placed the onclick listener for the
button using the button object.Once the button clicked it will display this “You
clicked the button”congratulation you created the onClickListener application.
No comments:
Post a Comment