Tuesday, 3 June 2014

Example creating DialogFragment with onClickListener

Okay, another post with example DialogFragment with onClickListener.
This a my CustomDialogFragment class:

 public class CustomDialogFragment extends DialogFragment {  
   
   public static final String TAG = CustomDialogFragment.class.getSimpleName();  
   private DialogInterface.OnClickListener onItemClickListener;  
   private ImageView icon;  
   private TextView msg;  
   private Button btn;  
   private int iconRes;  
   private String msgRes;  
   private String btnRes;  
   private Context context;  
   
   public static CustomDialogFragment getInstance() {  
     CustomDialogFragment f = new CustomDialogFragment();  
     return f;  
   }  
   
   public void setOnClickListener(DialogInterface.OnClickListener onItemClickListener) {  
     this.onItemClickListener = onItemClickListener;  
   }  
   
   @Override  
   public Dialog onCreateDialog(Bundle savedInstanceState) {  
   
     final Dialog dialog = new Dialog(getActivity());  
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  
   
     View view = LayoutInflater.from(getActivity()).inflate(R.layout.custom_dialog_fragment, null);  
   
     icon = (ImageView) view.findViewById(R.id.image1);  
     msg = (TextView) view.findViewById(R.id.text1);  
     btn = (Button) view.findViewById(R.id.button1);  
   
     if (iconRes != 0)  
       icon.setImageResource(iconRes);  
   
     if (msgRes != null)  
       msg.setText(msgRes);  
   
     if (btnRes != null)  
       btn.setText(btnRes);  
   
     if (onItemClickListener != null) {  
       btn.setOnClickListener(new View.OnClickListener() {  
         @Override  
         public void onClick(View v) {  
           onItemClickListener.onClick(dialog, 0);  
         }  
       });  
     }  
   
     dialog.setContentView(view);  
   
     return dialog;  
   }  
   
   public void setIconImageResource(int res) {  
     iconRes = res;  
   }  
   
   public void setMessageTextResource(int res) {  
     msgRes = context.getString(res);  
   }  
   
   public void setMessageText(String res) {  
     msgRes = res;  
   }  
   
   public void setButtonTextResource(int res) {  
     btnRes = context.getString(res);  
   }  
   
   public void setButtonTextResource(String res) {  
     btnRes = res;  
   }  
   
   public void setContext(Context context) {  
     this.context = context;  
   }  
   
 }  

And here is dialog layout:

 <?xml version="1.0" encoding="utf-8"?>  
   
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:padding="50dp"  
   android:background="#ff0000"  
   >  
   
   <ImageView  
     android:id="@+id/image1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:src="@drawable/ic_launcher" />  
   
   <TextView  
     android:layout_below="@+id/image1"  
     android:id="@+id/text1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:text="This is message" />  
   
   <View  
     android:layout_below="@+id/text1"  
     android:id="@+id/separator"  
     android:layout_width="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:layout_height="10dp" />  
   
   <Button  
     android:layout_below="@id/separator"  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:text="OK" />  
 </RelativeLayout>  

And here is initalization in any FragmentActivity class:

 public class MainActivity extends ActionBarActivity {  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     initDialog();  
   }  
   
   private void initDialog() {  
     CustomDialogFragment cdf = CustomDialogFragment.getInstance();  
     cdf.setContext(this);  
     cdf.setIconImageResource(android.R.drawable.btn_star);  
     cdf.setMessageText("Hello world!");  
     cdf.setButtonTextResource(android.R.string.ok);  
     cdf.setOnClickListener(new DialogInterface.OnClickListener() {  
       @Override  
       public void onClick(DialogInterface dialog, int which) {  
         dialog.dismiss();  
       }  
     });  
   
     Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag(CustomDialogFragment.TAG);//avoid opening twice dialog  
     if (fragmentByTag == null)  
       cdf.show(getSupportFragmentManager(), CustomDialogFragment.TAG);  
   }  
   
 }  

Sample pic: