2

I'm customming dialog (extend Dialog) with a custom layout:

setContentView(LayoutInflater.from(getContext()).inflate(R.layout.dialog_call, null));

in this layout, I have some TextView and Button need catch button click event so that I put android:onClick="onClick" to each view.

In .java file I implemented public void onClick(View v){....}

However when I click each view, I received crash and It show that it not found onClick.

I also tried add android:clickable="true" to each view in XML file but not success.

What is my wrong here ?

mdtuyen
  • 3,880
  • 5
  • 19
  • 48
  • 1
    http://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android please refer this answer – Srinivasan Jan 10 '16 at 11:45
  • it is difficult if I must defined a onClick method with each activity that I use my Dialog. – mdtuyen Jan 10 '16 at 11:53
  • Instead of directly using android:onClick="onClick" method, why dont you implement the OnClickListener and override the onClick(View view) method in the dialog class. – Srinivasan Jan 10 '16 at 12:01

1 Answers1

2
 AlertDialog dialog;
    View v=// dialog layout ;
    dialog.setContentView(v);
    Button btn = v.findViewById(your Button id);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // your code 
        }
    });
Salauddin Gazi
  • 1,312
  • 1
  • 11
  • 18
  • yes, I know this solution will work fine. But because I have many view in XML need implement onClick method, so that I choice add Click event by android:onClick. – mdtuyen Jan 10 '16 at 11:54
  • inflate(R.layout.dialog_call) it is not root layout that;s way onclick not work – Salauddin Gazi Jan 10 '16 at 11:59
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Aug 08 '18 at 11:37