//File name: ButtonclickActivity.java
// Note: before executing this program, you should ensure that you have one button & one edittext in your main.xml file.
package com.techpalle.buttonclick; // This is the package name in which your project will reside.
/*
* Below are the import statements which will import different classes which you will be
* using in your program through out.
*/
import android.app.Activity; // This will import package needed to use Acitivity class in your code
import android.os.Bundle;// This will import Bundle class for savedInstanceState
import android.view.View;// This is needed if you are using View class in your code
import android.view.View.OnClickListener;// This will be needed if you are creating instance for OnClickListener interface
import android.widget.Button;//for using Button related things
import android.widget.EditText;// needed when you are using EditText instances.
/*
* ButtonclickActivity is our Activity name (which is a class actually)
* And we are extending Activity class.
* Activity –> is a single, focused thing that is used to interact with User
* Activity class takes care of creating a window for you in which you can place your UI
*/
public class ButtonclickActivity extends Activity {
/** Called when the activity is first created. */
/*
* onCreate is the first method to get called by Android, before launching your activity screen.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
/*
* Since you are using Activity base class, which actually gives you the basic
* fundamental functionalities for your activity. So, you have to call super.onCreate
* before you proceed with your stuff.
*/
super.onCreate(savedInstanceState);
setContentView(R.layout.main);// This sets your main.xml stuff to your activity screen.
/*
* Now our problem is to put some text in editText1 screen if some one clicks on
* button1. So here are the steps to be followed to do it.
* 1. get resource ref of editText1
* 2. get resource ref of button1
* 3. set onClickListener for button11,so that android will call that function if some one
* clicks on that button.
* 4. Now last thing, once android calls that onclick functionality, do what ever you want
* in our case we will fill some text in editText1
*/
final EditText t = (EditText) findViewById(R.id.editText1);
Button b = (Button) findViewById(R.id.button1);
/* setOnClickListener will take object to OnClickListener interface, so we need to implement that interface and give that ref to it. */
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
t.setText(“Button has been clicked”);
// TODO Auto-generated method stub
}
});
}
}












