Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an onClickListener on an included layout?

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?

like image 703
silvia_aut Avatar asked Oct 07 '13 13:10

silvia_aut


People also ask

How do I use OnClickListener?

To implement View. OnClickListener in your Activity or Fragment, you have to override onClick method on your class. Firstly, link the button in xml layout to java by calling findViewById() method. R.

Can we set OnClickListener on TextView?

In Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView. In this tutorial, we will learn how to set OnClickListener for TextView in Kotlin file, with the help of an example.

What is the only method of the view OnClickListener interface?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked.


2 Answers

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

like image 156
Sherif elKhatib Avatar answered Sep 28 '22 20:09

Sherif elKhatib


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);
like image 29
An-droid Avatar answered Sep 28 '22 19:09

An-droid