Thursday 17 April 2014

Custom Alert Or Custom Dialog Fragment in Android

I recently can not able to Create Alert Dialog with out it Boarder so i end up to resolved it by Support Dialog Fragment .

You can check my code .



public class AlertActivity extends ActionBarActivity {

    private AlertDialog mAlertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.alert, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {

            //showAlertDialog();
           
            CreateDialogFragment();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_alert,
                    container, false);
            return rootView;
        }
    }

    private void showAlertDialog() {

        AlertDialog.Builder mBuilder = new Builder(new ContextThemeWrapper(
                this, android.R.style.Theme_Translucent_NoTitleBar));// new
                                                                        // Builder(this,android.R.style.Theme_Translucent_NoTitleBar);

        View mView = LayoutInflater.from(this).inflate(
                R.layout.alert_dialog_layout, null);

        mBuilder.setView(mView);
        // mBuilder.setC
        // mBuilder.s

        mAlertDialog = mBuilder.create();

        mAlertDialog.setInverseBackgroundForced(true);
        mAlertDialog.setCanceledOnTouchOutside(true);

        mAlertDialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent);
        mAlertDialog.show();

    }

    private void CreateDialogFragment() {

        View mView = LayoutInflater.from(this).inflate(
                R.layout.alert_dialog_layout, null);
        MyDialogFragment mDialogFragment=new MyDialogFragment(mView);
       
        //mDialogFragment.setStyle(MyDialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar);
        mDialogFragment.setShowsDialog(true);
       
       
        mDialogFragment.show(getSupportFragmentManager(), "dialog");


    }

    public class MyDialogFragment extends DialogFragment {

        private View mView;

        /*
         * public MyDialogFragment newInstance(View mView) {
         *
         * mView=mView; return new MyDialogFragment(); }
         */

        public MyDialogFragment(View mView) {
            this.mView = mView;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
           
            return mView;
        }
       
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
          Dialog dialog = super.onCreateDialog(savedInstanceState);

          // request a window without the title
          dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
          return dialog;
        }
    }

}