10

I have a layout with an ImageButton that I included in several other layouts.

ImageButton Layout:

call_cancelled.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/callEndLayout"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:height="70dip"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <ImageButton
        android:id="@+id/phoneEnd"
        android:layout_width="63dp"
        android:layout_height="63dp"
        android:paddingBottom="7dp"
        android:background="@drawable/phone_cancelled"  />

</LinearLayout>

My include:

  <include
   android:id="@+id/includeCallEnd0"
   android:layout_width="70dp"
   android:layout_height="70dp"
   android:layout_alignBottom="@+id/include"
   android:layout_alignParentRight="true"
   layout="@layout/call_cancelled" />

For this includes I need a onClickListener when it's pressed. I tried this:

View endcall0 = (View) findViewById(R.id.includeCallEnd0);

 endcall0.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent callIntent = new Intent(Intent.ACTION_CALL_BUTTON);
            startActivity(callIntent);

            int id = viewFlipper.getDisplayedChild();

            if (id == 0) {
                hideSoftKeyboard();
            }
        }
     });

But it doesn't work. Does anybody know the solution?

silvia_aut
  • 1,481
  • 6
  • 19
  • 33

4 Answers4

8

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:

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);

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
4

You could have done

LinearLayout endcall0 = (LinearLayout) findViewById(R.id.includeCallEnd0); 

and then simply

ImageButton imgBtn = (ImageButton) endcall0.findViewById(R.id. phoneEnd); 
imgBtn.setOnClickListener(this);
An-droid
  • 6,433
  • 9
  • 48
  • 93
0

Try to set your clickListener on callEndLayout (LinearLayaout)

A.S.
  • 4,574
  • 3
  • 26
  • 43
0

very easy with Kotlin::

{appBar} is include view

{btnOrders} is button view inside appBar layout

binding.appBar.btnOrders.setOnClickListener {
   startActivity(Intent(this, OrdersActivity::class.java))
}
Abd Nezar
  • 341
  • 4
  • 8