How to show DialogFragment from Fragment in actionBar android

Ebtehal___ picture Ebtehal___ · Mar 17, 2013 · Viewed 13.2k times · Source

I use Tabs ActionBar with Fragments. Inside one fragment I have button. I want to show a DialogFragment (inside my fragment) when the button is pressed. How can I do that?

This is my code

 public class MainActivity extends FragmentActivity implements EditNameDialogListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //showEditDialog();
        ActionBar actionbar = getActionBar();
        // Tell the ActionBar we want to use Tabs.
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // initiating both tabs and set text to it.
        ActionBar.Tab InboxTab = actionbar.newTab().setText("One");
        ActionBar.Tab SentTab = actionbar.newTab().setText("Two");

        Fragment InboxFragment = new Inbox();
        Fragment SentFragment = new SentBox();
        InboxTab.setTabListener(new MyTabsListener(InboxFragment));
        SentTab.setTabListener(new MyTabsListener(SentFragment));
        actionbar.addTab(InboxTab);
        actionbar.addTab(SentTab);
    }

    private void showEditDialog() {
        FragmentManager fm = getSupportFragmentManager();
        EditNameDialog editNameDialog = new EditNameDialog();
        editNameDialog.show(fm, "fragment_edit_name");
    }

    @Override
    public void onFinishEditDialog(String inputText) {
        Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
    }

}
class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }
    public class Inbox extends Fragment implements EditNameDialogListener{

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            Utility util = new Utility(getActivity());
            View view =  inflater.inflate(R.layout.inbox, container, false);
            ArrayList<String[]> searchResults = util.ReadFile();

            final ListView lv1 = (ListView) view.findViewById(R.id.inboxList);
            lv1.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults));

            lv1.setOnItemClickListener(new OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                 Object o = lv1.getItemAtPosition(position);
                 String[] fullObject = (String[])o;
                Toast.makeText(getActivity(), "You have chosen: " + " " + fullObject[0], Toast.LENGTH_LONG).show();
             } 
            });

            return view;
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // TODO Auto-generated method stub
            super.onCreateOptionsMenu(menu, inflater);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub


            switch (item.getItemId()) {
            case R.id.menu_settings:

                showEditDialog();
                break;

            default:
                break;
            }
            return super.onOptionsItemSelected(item);
        }

        private void showEditDialog() {
            FragmentManager ft = getSupportFragmentManager();
            EditNameDialog editNameDialog = new EditNameDialog();
            editNameDialog.show(ft, "fragment_edit_name");
        }

        @Override
        public void onFinishEditDialog(String inputText) {
            Toast.makeText(getActivity(), "Hi, " + inputText, Toast.LENGTH_SHORT).show();
        }


    }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            /*
             * Toast.makeText(StartActivity.appContext, "Reselected!",
             * Toast.LENGTH_LONG).show();
             */
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.replace(R.id.fragment_container, fragment);
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(fragment);
        }

    }

public class EditNameDialog extends DialogFragment implements OnEditorActionListener{

     public interface EditNameDialogListener {
            void onFinishEditDialog(String inputText);
        }

        private EditText mEditText;

        public EditNameDialog() {
            // Empty constructor required for DialogFragment
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_edit_name, container);
            mEditText = (EditText) view.findViewById(R.id.txt_your_name);
            getDialog().setTitle("Hello");

            // Show soft keyboard automatically
            mEditText.requestFocus();
            getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            mEditText.setOnEditorActionListener(this);

            return view;
        }

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (EditorInfo.IME_ACTION_DONE == actionId) {
                // Return input text to activity
                EditNameDialogListener activity = (EditNameDialogListener) getActivity();
                activity.onFinishEditDialog(mEditText.getText().toString());
                this.dismiss();
                return true;
            }
            return false;
        }
public class Inbox extends Fragment implements EditNameDialogListener{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Utility util = new Utility(getActivity());
        View view =  inflater.inflate(R.layout.inbox, container, false);
        ArrayList<String[]> searchResults = util.ReadFile();

        final ListView lv1 = (ListView) view.findViewById(R.id.inboxList);
        lv1.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults));

        lv1.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> a, View v, int position, long id) {
             Object o = lv1.getItemAtPosition(position);
             String[] fullObject = (String[])o;
            Toast.makeText(getActivity(), "You have chosen: " + " " + fullObject[0], Toast.LENGTH_LONG).show();
         } 
        });

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub


        switch (item.getItemId()) {
        case R.id.menu_settings:

            showEditDialog();
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showEditDialog() {
        FragmentManager ft = getSupportFragmentManager();
        EditNameDialog editNameDialog = new EditNameDialog();
        editNameDialog.show(ft, "fragment_edit_name");
    }

    @Override
    public void onFinishEditDialog(String inputText) {
        Toast.makeText(getActivity(), "Hi, " + inputText, Toast.LENGTH_SHORT).show();
    }


}

Answer

Kapil Vats picture Kapil Vats · Mar 17, 2013

use this to show dialogfragment from fragment

FragmentManager fm = getActivity().getSupportFragmentManager();
DialogFragment dialog = new MyDialogFragment(); // creating new object
dialog.show(fm, "dialog");