Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Button click event

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

like image 980
Tvd Avatar asked Nov 16 '11 09:11

Tvd


People also ask

How do you click an event on Android?

Responding to Click Events 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.

How can I tell if an Android button is clicked?

If you have more than one button click event, you can use switch case to identify which button is clicked. Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.

Does Android have OnClickListener?

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.


2 Answers

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;
        }
}
like image 102
Ravi Makvana Avatar answered Sep 19 '22 07:09

Ravi Makvana


Button Name is MyButton.it's working.

   MyButton.setOnClickListener(new OnClickListener() 
{
 @Override          
 public void onClick(View v) 
 {              
     mytextView.setText("Messi");           
  }         
});
like image 22
Erhan Demirci Avatar answered Sep 20 '22 07:09

Erhan Demirci