Nested setOnClickListener in Java / Android


Nested setOnClickListener in Java / Android



I am trying to setup a onclicklistner on a dialog button. If the listener is set using


bnxt.setOnClickListener(new x());



the app works as expected but if the below given 2nd implementation is used, the app crashes. Any hint or help is highly appreciated.


bnxt.setOnClickListener(new OnClickListener() { ...



Supplied code:


class x implements OnClickListener {
x() {
}
public void onClick(View v) {
if (listener != null) {
final Dialog dialog = new Dialog(getActivity());
FancyButton m_no = (FancyButton) dialog.findViewById(R.id.b_no);
m_no.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}



}


public View onCView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_x, container, false);
final FancyButton bnxt = (FancyButton) view.findViewById(R.id.btn_popup);

//bnxt.setOnClickListener(new x());

bnxt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (listener != null) {
final Dialog dialog = new Dialog(getActivity());
FancyButton m_no = (FancyButton) dialog.findViewById(R.id.b_no);

m_no.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
});
return view;
}





what error you are getting.
– pop
Jun 30 at 1:50





@pop No errors except thread crashes in logcat.
– Shahzad
Jun 30 at 1:52





FancyButton m_no = (FancyButton) dialog.findViewById(R.id.btn_popup); --> X listener. FancyButton m_no = (FancyButton) dialog.findViewById(R.id.b_no); --> onclicklistener. which one is right ?
– pop
Jun 30 at 2:07






@pop I have edited the code and it's correct now.
– Shahzad
Jun 30 at 2:12





I had tried the same there is no problem in creating nested listener. you need to check your code whats going wrong. If you want I will post my sample code what i have created.
– pop
Jun 30 at 3:06




1 Answer
1



There is no problem with using a nested onClicklistener. Below is the sample code. Kept layout as simple just to test.


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);

view.findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_view);
dialog.findViewById(R.id.dialog_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Dialog btn clicked", Toast.LENGTH_LONG).show();
}
});

dialog.show();
}
});
return view;
}



fragment_test.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivityFragment">

<Button
android:id="@+id/btn1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="btn1"
app:layout_constraintStart_toStartOf="parent" />


<Button
android:id="@+id/btn2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="btn2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/btn1" />
</android.support.constraint.ConstraintLayout>



dialog_view.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/dialog_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog BTn" />

</android.support.constraint.ConstraintLayout>





Thank you very much.
– Shahzad
Jun 30 at 5:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

The forked VM terminated without saying properly goodbye. VM crash or System.exit called