Thursday, October 29, 2009

Event Handling In Android


In my previous article I discussed about how to create user interfaces in Android. Here I’m going to show you how you can add event handler to that UI. To do that, first add these two lines between package and class declaration.

To do that, first add these two lines between package and class declaration.

import android.view.View;
import android.widget.Button;

Now replace your onCreate method using following code.

import android.view.View;
import android.widget.Button;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.testview);
 Button ok = (Button)findViewById(R.id.btn_testview_ok);
 ok.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v){
   moveToMain();
  }
 });
}
private void moveToMain() {
 // TODO Auto-generated method stub
 setContentView(R.layout.main);
}

In the above code "btn_testview_ok" is the id of the "Ok" button in previous article. When you click that button, the moveToMain(); method will be called and content view will changed as "main.xml". I will discuss more about this topic in my next posts.

10 comments:

  1. I'm new one for Android.
    Thanks mate.

    ReplyDelete
  2. These information are very very useful
    (Muhammad Nasir)

    ReplyDelete
  3. Thanks A lot
    Sudhanshu Ranjan

    ReplyDelete
  4. it makes the event handler in java like in visual basic. Thanks it really helpfill

    ReplyDelete
  5. @ All Anonymous,
    Thank You !

    @ Muhammad Nasir,
    Thank you very much for the comment.

    @ Sudhanshu Ranjan ,
    Yes, thanks for the comment.

    ReplyDelete
  6. @ Jack D'silva,
    Thanks for the comment...

    ReplyDelete
  7. your post ha been very useful. thank you very much. i am facing a problem when i am trying to implement the code in a user interface. can u help me out?

    ReplyDelete
  8. @ Sanghamitra,
    sure, tell me your problem.

    ReplyDelete
  9. Hi Saranga

    your post ha been realy very helpful, thank u very much.
    can u please help me with geo location finder code and google map integration to tht geo location.

    ReplyDelete
  10. Hey Saranga,

    Your article is really helpful to beginner. I appreciate your effort.
    I am also new to this technology. Can you help me to learn XMLParsing topic. Thanks in advance.


    Thanks,
    Amrish Patel

    ReplyDelete