Search code examples
androidandroid-layoutandroid-preferencesandroid-dialogandroid-tablelayout

Button click event not getting triggered


I have a set of buttons defined within a table layout.

custom_layout.xml :

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

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:id="@+id/tableLayout">

    <TableRow
        android:layout_weight="1">
        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/firstBtn"
            android:text="F"
            android:layout_weight="0.3">
        </Button>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/secondBtn"
            android:text="S"
            android:layout_weight="0.3">
        </Button>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/thirdBtn"
            android:text="T"
            android:layout_weight="0.3">
        </Button>

    </TableRow>

    </TableLayout>

</RelativeLayout>

This XML is set as a layout within a custom alert dialog.

Code:

public class CustomDialogPreference extends DialogPreference {


private Button firstBtn = null;
private Button secondBtn = null;
private Button thirdBtn = null;


public CustomDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setPersistent(false);
    this.context = context;
    setDialogLayoutResource(R.layout.custom_layout);



}

@Override
protected void onBindDialogView(View view) {

    firstBtn = (Button)view.findViewById(R.id.firstBtn);
    secondBtn = (Button)view.findViewById(R.id.secondBtn);
    thirdBtn = (Button)view.findViewById(R.id.thirdBtn);

    firstBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("Check","First button is clicked"));

        }
    });

    secondBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("Check","Second button is clicked"));

        }
    });

    thirdBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("Check","Third button is clicked"));

        }
    });


    super.onBindDialogView(view);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    persistBoolean(positiveResult);

    Log.d("Blockout","Dialog close detected");
    if(positiveResult)
    {

        Log.d("Blockout","Save button had been clicked");

    }

   }

}

When the dialog is opened and I click on the buttons, I am not able to see the corresponding log messages. I am also not getting any null pointer exceptions, so I think the the views are being set and accessed properly. Any idea what I am missing?


Solution

  • If you want a custom Dialog, I'd suggest the DialogFragment class. There's a documentation guide on those - Dialogs.

    I think you want this method - onCreateDialogView.

    For example,

    public class CustomDialogPreference extends DialogPreference 
        implements View.OnClickListener {
    
        private Context context;
    
        public CustomDialogPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
            setPersistent(false);
            this.context = context;
        }
    
        @Override
        protected View onCreateDialogView() {
            super.onCreateDialogView();
            LayoutInflater inflater = LayoutInflater.from(this.context);
            View rootView = inflater.inflate(R.layout.custom_layout,null);
    
            rootView.findViewById(R.id.firstBtn).setOnClickListener(this);
            rootView.findViewById(R.id.secondBtn).setOnClickListener(this);
    
            return rootView;
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.firstBtn:
                    Log.d("Check","First button is clicked"));
                    break;
                case R.id.secondBtn:
                    break;
            }
        }