Reputation: 971
I Create a dialog in java and show it when a button was clicked, in this dialog I have a button , how can I set OnClickListener of this button? This code show my dialog:
ImageView iv = (ImageView) view.findViewById(R.id.ivTaskPlus);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog d = new Dialog(getActivity());
d.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
d.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
d.setContentView(R.layout.task_new_layout);
d.show();
}
});
and in task_new_layout.xml i have this button:
<Button
android:id="@+id/btnSaveTask"
android:layout_marginTop="10sp"
style="@style/btnSuccess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/save_task"
/>
I create an onClickListener for this button but it wont work and has no error!!
final View taskView = inflater.inflate(R.layout.task_new_layout, container, false);
Button btnSaveTask = (Button) taskView.findViewById(R.id.btnSaveTask);
btnSaveTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("MGH","clicked");
}
});
Upvotes: 1
Views: 534
Reputation: 392
try setContentView(taskview)
ImageView iv = (ImageView) view.findViewById(R.id.ivTaskPlus);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog d = new Dialog(getActivity());
d.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
d.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
d.setContentView(taskView);
d.show();
}
});
final View taskView = inflater.inflate(R.layout.task_new_layout, container, false);
Button btnSaveTask = (Button) taskView.findViewById(R.id.btnSaveTask);
btnSaveTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("MGH","clicked");
}
});
Upvotes: -1
Reputation: 18112
You are re-inflating your view, which creates a new instance of the Button
and this new instance is not loaded anywhere. Button
on Dialog
is a different instance and on that you don't have any OnClickListener
attached.
When you press the Button
on Dialog
your code do not get executed.
Do this
@Override
public void onClick(View view) {
Dialog d = new Dialog(getActivity());
d.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
d.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
d.setContentView(R.layout.task_new_layout);
Button myBtn = (Button) d.findViewById(R.id.btnSaveTask);
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// your code
}
});
d.show();
}
Upvotes: 1
Reputation: 2806
You should use Dialog.findViewById();
instead of inflating the view and finding your button again.
try this in your ImageView
click listner
@Override
public void onClick(View view) {
Dialog d = new Dialog(getActivity());
d.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
d.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
d.setContentView(R.layout.task_new_layout);
Button btnSaveTask = (Button) d.findViewById(R.id.btnSaveTask);
btnSaveTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("MGH", "clicked");
}
});
d.show();
}
Upvotes: 1