How to set an onClickListener on an included layout?
Replace findViewById(R.id.includeCallEnd0) with findViewById(R.id.includeCallEnd0).findViewById(R.id.phoneEnd) and it should work because you want to set the click listener on the ImageButton and not the whole layout Use the following function to set the OnClickListener once:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private static void applyListener(View child, OnClickListener listener) { if (child == null) return; if (child instanceof ViewGroup) { applyListener((ViewGroup) child, listener); } else if (child != null) { if(child.getId() == R.id.phoneEnd) { child.setOnClickListener(listener); } } } private static void applyListener(ViewGroup parent, OnClickListener listener) { for (int i = 0; i < parent.getChildCount(); i++) { View child = parent.getChildAt(i); if (child instanceof ViewGroup) { applyListener((ViewGroup) child, listener); } else { applyListener(child, listener); } } } |
Use applyListener(rootView, yourOnClickListener);