13

I have 2 buttons in my xml file with RelativeLayout. In my class I have extended Dialog & implemetned OnClickListener and also added OnClick(View v) method. But somehow the onClick code is never executed when the button is clicked. Can anyone help me find the problem with my code :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"  
android:padding="10px">

    ......
    <Button android:id="@+id/saveBtn_settingDlg" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_below="@+id/editText1"
    android:layout_marginLeft="10px" android:text="Save" />

    <Button android:id="@+id/closeBtn_settingDlg" android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:text="Close" android:layout_alignBaseline="@+id/saveBtn_setting" 
      android:layout_toRightOf="@+id/saveBtn_setting" android:onClick="CloseDialog"  />

Class

 public class SettingDialog extends Dialog implements OnClickListener {

private Button btn_save, btn_close;

           // In Constructor
    btn_save = (Button) findViewById(R.id.saveBtn_settingDlg);
    btn_close = (Button) findViewById(R.id.closeBtn_settingDlg);
    btn_save.setOnClickListener(this);
    btn_close.setOnClickListener(this);

@Override
public void onClick(View v) {
    if (v == btn_save) 
        SaveSettings();
    else if (v == btn_close)
        CloseDialog();

    return;
}

private void CloseDialog() {
    disposeAll();
    this.dismiss();
}

public void CloseBtnClicked(View v) {
    CloseDialog();
}

In xml for close btn I tried CloseBtnClicked also but no difference and I get an UnexpectedError message and application shuts down. Somehow the event is only not activated in any ways. And also on adding onClick to closebtn the button is now shown on the top-left of the screen and lost the actual location of it.

Calling SettingDialog from Activity class :

    private void OpenSettingDialog() {

    AlertDialog.Builder ad = new AlertDialog.Builder(this);
    ad.setIcon(R.drawable.ic_dialog_small);

    View inflatedView = LayoutInflater.from(this).inflate(R.layout.settings_dialog, null); 
    ad.setView(inflatedView);

    AlertDialog adlg = ad.create();     
    adlg.show();

}

Can anyone help me know the reason for this problem and how do I solve it. I am a newbie to Android.

Thanks

1

8 Answers 8

19

You should turn to use the simplest way that I always do as below:

@Override
public void onCreate(Bundle savedInstanceState) {
    button1.setOnClickListener(onClickListener);
    button2.setOnClickListener(onClickListener);
    button3.setOnClickListener(onClickListener);
}

private OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {
        switch(v.getId()){
            case R.id.button1:
                 //DO something
            break;
            case R.id.button2:
                 //DO something
            break;
            case R.id.button3:
                 //DO something
            break;
        }
    }
};
2
  • 9
    This is really old but please never ever do this. Someone will slap you... Switches are not the best practice and tracing this to debug would be a nightmare. Please declare a new onClickListener for each button you have on screen
    – Stephen
    Nov 15, 2014 at 4:06
  • It can be done in the mentioned way, but it is better programming to use separate listeners for each button.
    – fury.slay
    May 11, 2020 at 13:54
8

i think you should compare the view id's not views

if (v == btn_save)

to

   if (v.getId() == btn_save.getId())
6
  • 1
    +1 and never say "onClick code is never executed" before adding a Log at the beginning to make sure! Nov 16, 2011 at 9:34
  • @Sherif, Yashwant Kumar, Kiran Babu, I am also trying to debug and added breakpoint at onClick(View v), but the cursor doesn't stop over here only. I added Log.i line also inside OnClick but that also doesn't show up in logs. I tried Yashwant's and Kiran Babu's option but none worked. I am calling this dialog from my Activity - should that matter.
    – Tvd
    Nov 16, 2011 at 10:19
  • @Tvd Add your onClick to the Activity and make your activity implement OnClickListener. finally setOnClickListener(yourActivity). report back plz Nov 16, 2011 at 10:25
  • @Tvd pass a context or I think you can use getContext() Nov 16, 2011 at 10:40
  • 1
    let us continue this discussion in chat Nov 16, 2011 at 11:39
4
android:onClick="CloseDialog"

of Button in layout for Dialog searches the method in Activity class not in Dialog

define your method in Activity which is calling Dialog or remove android:onClick="CloseDialog" from tag and set OnClickListener from Java code in Dialog class.

3

Button Name is MyButton.it's working.

   MyButton.setOnClickListener(new OnClickListener() 
{
 @Override          
 public void onClick(View v) 
 {              
     mytextView.setText("Messi");           
  }         
});
1
  • I needed to use View.OnClickListener instead of OnClickListener.
    – User
    Jul 31, 2018 at 21:08
1

Solution to my Problem :

Instead of using AlertBuilder and AlertDialog, I just called the dialog as :

    SettingDialog sd = new SettingDialog(this, mySettings);
sd.show();

And this worked well. All click events were handled within SettingDialog only. No changes were to be made in SettingDialog. Only the way to call SettingDialog is changed in the Activity. That's it.

BTW, In onClick() comapring a View with its name :

    public void onClick(View v) {
    Log.i("APP: ", "Into OnClick of SettingDialog. View = " + v);
    if (v == btn_save) 
        SaveSettings();
    else if (v == btn_close) 
        CloseDialog();

    return;
}

Also works perfectly. I use this way only and it works well. No need to check with the Id only.

Hope my solution will help others who are stuck like me. Thanks to all for your efforts and helping hand.

1

just Replace Your code From this code

@Override
public void onClick(View v) {

    if (v == btn_save) 
        SaveSettings();
    else if (v == btn_close)
        CloseDialog();

    return;
}

to

@Override
public void onClick(View v) {

    switch(v.getId()){
            case R.id.saveBtn_settingDlg:
                SaveSettings();

            break;
            case R.id.closeBtn_settingDlg:
                CloseDialog();
            break;
        }
}
0

add this method in java class :

public void CloseDialog(View v)
{

}

because in layout you have set android:onClick="CloseDialog"

0

Try this, I Hope it's of help

 if(v.getId()==R.id.saveBtn_settingDlg)
       SaveSettings();

        else if (v.getId()==R.id.closeBtn_settingDlg)

            CloseDialog();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.