4

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.id.button1);
    b1.setOnClickListener(usage);

    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(price);

    b3 = (Button) findViewById(R.id.button3);
    b3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            System.out.println("Button3");
            mainDialog3 = new Dialog(Advice.this);
            mainDialog3.setContentView(R.layout.fuel);
            mainDialog3.setCancelable(true);

            mainDialog3.show();
            Window window = mainDialog3.getWindow();
            window.setLayout(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);

        }
    });


private View.OnClickListener usage = new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        System.out.println("Button1");
        Toast.makeText(getApplicationContext(), "Button 1",
                Toast.LENGTH_LONG).show();
        mainDialog1 = new Dialog(Advice.this);
        mainDialog1.setContentView(R.layout.usage);
        mainDialog1.setCancelable(true);

        mainDialog1.show();
        Window window = mainDialog1.getWindow();
        window.setLayout(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);

    }
};

private View.OnClickListener price = new View.OnClickListener() {
    public void onClick(View v) {
        System.out.println("Button2");
        mainDialog2 = new Dialog(Advice.this);
        mainDialog2.setContentView(R.layout.price);
        mainDialog2.setCancelable(true);

        mainDialog2.show();
        Window window = mainDialog2.getWindow();
        window.setLayout(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);

    }

};

The problem is with button 1 (b1). The rest of the buttons (b2, b3) work perfectly fine. Here is my XML:

<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:text="Edit" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button3"
    android:layout_alignLeft="@+id/button1"
    android:text="Edit" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView8"
    android:layout_alignLeft="@+id/button2"
    android:text="Edit" />
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Droid
  • 419
  • 4
  • 15
  • 1
    Silly question, but are you sure the button you are clicking is button1? It seems like you have a number of buttons all with the same text ("Edit") in a RelativeLayout. Is it possible that another button is sitting on top of what you think is button1, thus preventing it from being clicked on? – brianestey Jun 27 '13 at 05:34
  • yes i checked no other button is overlapping the button. Instead i added a new button and checked but still the same problem. – Droid Jun 27 '13 at 05:50
  • Hey thank you so much, i don't know where was the problem, but it worked now. – Droid Jun 27 '13 at 05:57
  • @IrshadKhan: when editing posts, please try and edit more than just a few words -- try and fix up as many issues as you can find in a post as possible. If a suggested edit only improves a post a tiny bit, it may be rejected as "too minor". Thank you! – Qantas 94 Heavy Jan 22 '14 at 11:09

2 Answers2

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.

public void foo(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:

    ImageButton button = (ImageButton) findViewById(R.id.list_manager_add);
    button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            button pressed...
        }
    });
Devolus
  • 21,661
  • 13
  • 66
  • 113
-8

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

jigar
  • 1,571
  • 6
  • 23
  • 46