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?
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.
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.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With