Skip to content Skip to sidebar Skip to footer

Button Onclicklistener Not Working

I have a button, when I click a dialog box appears. The problem is, the setOnClickListener is not responding to my button. Here is my Java code: Button b1 = (Button) findViewById(R

Solution 1:

You don't need to set the click listener manually in the code. The easiest way, to do this is to add the callback in the XML.

Like this:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/button7"
    android:onClick="foo"
    android:text="Edit" />

If you use eclipse with the android tools, you can set this property directly in the XML editor. Then you define a function in your code named after the Click property and it will be called automatically.

publicvoidfoo(View oView)
{
    Button cklicked ...
}

You can also set a click listener dynamically, but this you need only if you create buttons on the fly like this:

ImageButtonbutton= (ImageButton) findViewById(R.id.list_manager_add);
    button.setOnClickListener(newOnClickListener()
    {
        @OverridepublicvoidonClick(View v)
        {
            button pressed...
        }
    });

Solution 2:

try b1.setOnClickListener(usage); instead of b1.setOnClickListener(usage); in your code it will work..brother

Post a Comment for "Button Onclicklistener Not Working"