How to implements OnClickListener for a View item in Android

Hi friends🙂 Today I come up with a Android post. In this post I am trying to address a very small part, which is, how to tackle with the OnClicklistner in the enclosing View class. Yes, you guessed correctly. This is a so called event listening thing. We have to implement callback methods which comes with the OnClickListner interface. That is how the user interacts with the application.

Following are the interfaces that you get after you have completed this exercise.

OnClickListener for a View item in Android - Figure 1
Figure 1

Then you have to click on one of the Buttons on the screen. According to your selection, the appropriate text will appear on behalf for the existing text.

OnClickListener for a View item in Android - Figure 2
Figure 2

In this post, I haven’t mentioned how I design the interfaces. You can design it with your own wish with the XML files.

This is the code which perform our targets we have mentioned above.


import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FirstActivity extends Activity implements OnClickListener {

	// Declare UI elements
	private TextView changingTextView;
	private Button firstButton;
	private Button secondButton;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main); // Our only layout for this app is main.xml

        // Initialize the UI components
        changingTextView = (TextView) findViewById(;
        firstButton = (Button) findViewById(;
        // When we creating a button and if we expect that to use for event handling we have to set the listener
        secondButton = (Button) findViewById(;

    // Have to implement with the OnClickListner
    // onClick is called when a view has been clicked.
	public void onClick(View v) { // Parameter v stands for the view that was clicked.  

		// getId() returns this view's identifier.
		if(v.getId() =={
			// setText() sets the string value of the TextView
			changingTextView.setText("You clicked First");
		}else if(v.getId() =={
			changingTextView.setText("You clicked Second");

With in the codes you can see the comment which describe the methods and why we use those things. So I am not going to repeat it in here.

You can download this app for my GitHub repository. (click here)

That’s it. Enjoy🙂

About AnujAroshA

An undergraduate in the stream of ICT (Information & Communication Technology). A simple person :)
This entry was posted in Android Examples. Bookmark the permalink.

22 Responses to How to implements OnClickListener for a View item in Android

  1. Guest says:

    Can I implement 2 (or more) things in this class?
    What is the syntax?

    I have a seekbar and a dialogalert with another seekbar.

    • anujarosha says:


      Your question is not clear. I’ll give you an answer based on what I have understood from your question. Yes, you can add two or more items. But OnClickListener is for Buttons and there is another one call OnItemClickListener for Items. Here is a link for an example of OnItemClickListener. Is that what you looking for?

  2. sanchit says:

    thnks ahuja……….

  3. Rohit says:

    Hi, i m new for Android. i am working in java/j2EE, I have seen there is spring support in Android at client side(Message Convertor,SQLite) , but i am not getting any good resource, So If you have any resource or sample code , can you please send me on my gmail id?

  4. Anand.v says:

    Hi , in the above example i need listview to be displayed when i click that secondButton can u help me….

    • anujarosha says:

      Hi Anand,

      The simple way is define another activity which has a ListView component. Then for your OnClick event call an Intent to newly declared Activity. That’s it.

  5. Behzad says:

    What about the view itself? I mean how can I find out that user clicked on the view surface not on buttons?
    Thank you in advance.

    • anujarosha says:

      Hey Behzad,

      It is simple and I already included that part in to my coding. You can see in the “onClick” method I have used “if” clauses to check some view “id”s. That means we can identify the click event with a particular view item. Again “onClick” method fires only when we set the listener for the particular view item. As an example, if we don’t set onClickListener method to a button, even though we clicked that button, nothing gonna happen with onClick method.

      • Behzad says:

        Thanks anujarosha.
        So I have only one component/view on my layout and its a button. If I want to set an “onClick ” event to the area except button, I should implement it in this way:
        public void onClick(View v) { // Parameter v stands for the view that was clicked.
        // getId() returns this view’s identifier.
        if(v.getId() =={
        // setText() sets the string value of the TextView
        changingTextView.setText(“You clicked First”);
        // this is an “onClick” event for the whole layout area except button

      • anujarosha says:

        Hey Behzad,

        You cannot track the event on “touching” the surface with onClick method. To do that you have to use OnTouchListener(). There you can identify the event of touching a button as well as the surface.

      • Behzad says:

        Hey anujarosha,
        Thank you very much for response.
        Do you have any idea for implement double click on android?

      • anujarosha says:

        “Double click” ???:-/ Nope But double tap with map tried

      • Behzad says:

        Yes, double tap. Any link or source, please?

  6. Nikitha says:

    dear anujuarosha,
    is there any coding to put data in database without form..
    i need to login oly a set of students.where others cant login.i dont want any register form and save the data in database

  7. rahul says:

    good work bro…

  8. Ali says:

    what i have to do if i want to get the text from the edittext to the listview?
    thanks in advance…

  9. ashique12009 says:

    do clicklistener inside onDraw

  10. ankushminda says:

    Can you write a tutorial explaining delete an entry from the list view as well as from the database.

  11. Thabiso says:

    Good Practice Bro, Keep U the Good Work

  12. niharika says:

    hello ,m a beginner in android app devl.. i just want to know how to create an calculator app with only one onclicklistner button? is it possb? please tell me asap ,i have searched alot but couldnt find that. thanks in advance.

  13. Prashant mourya says:

    Why this parameter is used in setonclicklistener(this).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s