Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add onclick listener to predefined button?

Tags:

android

I've got the following button in my xml layout file...

<Button
    android:layout_width="150dip"
    android:id="@+id/button1"
    android:layout_height="50dip"
    android:text="@string/login"
    android:layout_marginRight="10dip">
</Button>

I'd like to programmatically add an onclick() listener in it's Java file. How would I do this?

like image 693
Skizit Avatar asked Mar 22 '11 17:03

Skizit


People also ask

How can we set click listener to a button?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

Which method is used to add onClick listener to button?

Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method.

What is an onClick listener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.


4 Answers

You just need something like this:

Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
            //Do stuff here
    }
});
like image 134
Ross Hambrick Avatar answered Sep 23 '22 13:09

Ross Hambrick


This answer comes from Five Ways to Wire Up an Event Listener. Please read that blog post for a fuller explanation from the author. See my other answer for these five ways reworked to add multiple onClick listeners.

1. Member Class

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //attach an instance of HandleClick to the Button
        findViewById(R.id.button1).setOnClickListener(new HandleClick());
    }    
    private class HandleClick implements OnClickListener{
        public void onClick(View arg0) {
            Button btn = (Button)arg0;  //cast view to a button
            // get a reference to the TextView
            TextView tv = (TextView) findViewById(R.id.textview1);
            // update the TextView text
            tv.setText("You pressed " + btn.getText());
        }
    }
}

2. Interface Type

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //use the handleClick variable to attach the event listener
        findViewById(R.id.button1).setOnClickListener(handleClick);
    }    
    private OnClickListener handleClick = new OnClickListener(){
        public void onClick(View arg0) {
            Button btn = (Button)arg0;
            TextView tv = (TextView) findViewById(R.id.textview1);
            tv.setText("You pressed " + btn.getText());
        }
    };
}

3. Anonymous Inner Class

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
            Button btn = (Button)arg0;
            TextView tv = (TextView) findViewById(R.id.textview1);
            tv.setText("You pressed " + btn.getText());
            }
        });
    }     
}

4. Implementation in Activity

public class main extends Activity implements OnClickListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(this);
    }    
    public void onClick(View arg0) {
        Button btn = (Button)arg0;
        TextView tv = (TextView) findViewById(R.id.textview1);
        tv.setText("You pressed " + btn.getText());
    }
}

5. Attribute in View Layout for OnClick Events

public class main extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }    
    public void HandleClick(View arg0) {
        Button btn = (Button)arg0;
        TextView tv = (TextView) findViewById(R.id.textview1);
        tv.setText("You pressed " + btn.getText());
    }
}

xml:

<Button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:onClick="HandleClick"/>
like image 29
Suragch Avatar answered Sep 20 '22 13:09

Suragch


You can try this.

public class myNewClass extends Activity implements OnClickListener {
    ................... 
    ...................       

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(this);

        public void onClick(View v) {
                Intent i = new Intent();
                Bundle extras = new Bundle();

        // This will catch the button click 
        // Now do what you wanted to do as a 
        // result of the onClick
        }
 }
like image 33
apesa Avatar answered Sep 19 '22 13:09

apesa


You can apply onClicklistner in Two way : 1. Under onCreate Method
2. Out side onCreate Method


if we will use under onCreate method then we will use like this:-

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_graphics1);

        textview1 = (TextView) findViewById(R.id.textview1);
        circleBtn = (Button) findViewById(R.id.circleBtn);

        // Click Listner Under on Create Method
        circleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

}

if You want out side to onCreate Method then first register the onClickListner in onCreate Method like this :-

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_graphics1);

        circleBtn = (Button) findViewById(R.id.circleBtn);

        **// Register on click on button
        circleBtn.setOnClickListener(new ClickMe());**

    }

and then implement clicklistner outside of onCreate Method so full code will be like this :-

public class ActiononBtn extends AppCompatActivity {

    private TextView textview1;
    private Button circleBtn;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_graphics1);

        textview1 = (TextView) findViewById(R.id.textview1);
        circleBtn = (Button) findViewById(R.id.circleBtn);

        // Register on click on button
        circleBtn.setOnClickListener(new ClickMe());

    } // Close onCreate Method


    private class ClickMe implements View.OnClickListener {
        public void onClick(View v) {

        }
    }

}//Close main Activity Class
like image 35
Pradeep Sheoran Avatar answered Sep 23 '22 13:09

Pradeep Sheoran