Home‎ > ‎android‎ > ‎Layout‎ > ‎

Code Description

For interacting the layout we define with the code, we add some line in MyLayout.java, so pressing save button will show an alert dialog. The full code is as below:

package com.iftitah.android.layout;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyLayout extends Activity
{

EditText edName;
EditText edAddress;
EditText edHomePhoneNumber;
EditText edMobilePhoneNumber;
/** Called with the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
edName = (EditText) findViewById(R.id.txtName);
edAddress =
(EditText)findViewById(R.id.txtAddress);

edHomePhoneNumber = 
(EditText) findViewById(
 R.id.txtHome);
edMobilePhoneNumber = 
(EditText)findViewById(
R.id.txtMobile);
Button btnSave = 
(Button) findViewById(R.id.btnSave);


btnSave.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.show(MyLayout.this, "Save!", 
"You Press Save Button, Not Yet Implemented!", 
"Close", true);

});

}

}

Line with bold face is added. normal line is default code we have from activitycreator. Below is a code description part by part:

----------------------------------------------------------
package com.iftitah.android.layout;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyLayout extends Activity
----------------------------------------------------------
It is a header stated that this code is member of package com.iftitah.android.layout, just like any other java code that each file/class is a member of a package for better code management. 

Series of lines begin with "import" state that the code/class will use some class declaration from package name stated after "import" clause.

The last line inform us, that the code is a MyLayout class that inherit from Activity class.

----------------------------------------------------------
EditText edName;
EditText edAddress;
EditText edHomePhoneNumber;
EditText edMobilePhoneNumber;
----------------------------------------------------------
Four line above declare an EditText Component variable that will be used as class field variable at the rest of class.

----------------------------------------------------------
public void onCreate(Bundle icicle)
{

super.onCreate(icicle);
setContentView(R.layout.main);
edName = (EditText) findViewById(R.id.txtName);
edAddress = (EditText)findViewById(R.id.txtAddress);
edHomePhoneNumber = (EditText) findViewById(
R.id.txtHome);
edMobilePhoneNumber = (EditText)findViewById(
R.id.txtMobile);
Button btnSave = (Button) findViewById(R.id.btnSave);

----------------------------------------------------------

MyLayout class overrideonCreate method from ancestor class, Activity. So, when the instance of the class call onCreate method, it will execute the command above, not the ancestor's onCreate Method. But in the onCreate method, the class still call Ancestor's method with code super.onCreate(icicle)

To show the layout we have define, we use setContentView method with layout name as a parameter.

The rest of lines is a way to bind layout component with code variables. All declared with EditText type except btnSave variable that use local variable, and declared as ButtonEditText is a View class for Text Editting, and Button is a View class for button that receive click event at most.

----------------------------------------------------------
btnSave.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.show(MyLayout.this, "Save!", 
"You Press Save Button, Not Yet Implemented!", 
"Close", true);

});
----------------------------------------------------------
The last lines tell us that BtnSave will react when it is clicked. it will show an alert dialog contains message that the button has been clicked.


Was this information helpfull ?

Comments