Reputation: 2190
I'm creating a custom dialog having this layout (title with image, text, 2 buttons):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:background="@color/white">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dip"
android:paddingTop="10dip">
<ImageView
android:id="@+id/dialog_title_image"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/info"/>
<TextView
android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_centerInParent="true"
android:text="Title"
android:layout_toRightOf="@id/dialog_title_image"
android:textColor="@color/black"
android:textSize="30sp"/>
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="@color/header_grep"
android:layout_marginTop="5dip"/>
<TextView
android:id="@+id/dialog_msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/dark_gray"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:gravity="bottom|center_horizontal"
android:paddingBottom="5dip">
<Button
android:id="@+id/positive_button"
android:layout_alignParentLeft="true"
android:layout_width="100dip"
android:layout_height="wrap_content"
style="@style/Gradient"
android:text="Si"/>
<Button
android:id="@+id/negative_button"
android:layout_width="100dip"
style="@style/Gradient"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/positive_button"
android:text="No"/>
</RelativeLayout>
</LinearLayout>
This is CustomDialog.java
public class CustomDialog extends Dialog {
Context context;
public CustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
public CustomDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.context = context;
}
public CustomDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.custom_dialog);
}
}
and this is the method i've implemented to show it:
private void showDialog(String msg, String title)
{
final Dialog dialog = new CustomDialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
TextView title_dialog = (TextView) dialog.findViewById (R.id.dialog_title);
title_dialog.setText(title);
TextView text = (TextView) dialog.findViewById(R.id.dialog_msg);
text.setText(msg);
Button positiveButton = (Button) dialog.findViewById(R.id.positive_button);
Button negativeButton = (Button) dialog.findViewById(R.id.negative_button);
positiveButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
finish();
}
});
negativeButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
when i launch the app and the dialog is shown, the title and the text are that ones defined in the xml file (so 'Title' and nothing for the body of the dialog) and if i click on the buttons nothing happens. Can anyone help me? Thank you in advance.
Upvotes: 0
Views: 1464
Reputation: 62391
Try it out:
final Dialog dialog = new Dialog(YourActivity.this);
instead of
final Dialog dialog = new Dialog(context);
If you are going to reuse this code, as a reusable component or as a mechanism to create dialogs at multiple places, create a base activity class and have this method in there, and use it in sub-classed activities as needed.
Upvotes: 0
Reputation: 47807
You must pass Application context on Dialog. If you passed this as context and you called customdialog from button onclick() event then it's refer this as button onclicklistner() object and that is wrong. so you must passed your activity or your application context.
final Dialog dialog = new CustomDialog(yourappliactioncontext);
Upvotes: 1