Show dialog from fragment?

前端 未结 7 759
再見小時候
再見小時候 2020-11-30 19:17

I have some fragments that need to show a regular dialog. On these dialogs the user can choose a yes/no answer, and then the fragment should behave accordingly.

Now,

相关标签:
7条回答
  • 2020-11-30 20:23

    I am a beginner myself and I honestly couldn't find a satisfactory answer that I could understand or implement.

    So here's an external link that I really helped me achieved what I wanted. It's very straight forward and easy to follow as well.

    http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application

    THIS WHAT I TRIED TO ACHIEVE WITH THE CODE:

    I have a MainActivity that hosts a Fragment. I wanted a dialog to appear on top of the layout to ask for user input and then process the input accordingly. See a screenshot

    Here's what the onCreateView of my fragment looks

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_home_activity, container, false);
    
        Button addTransactionBtn = rootView.findViewById(R.id.addTransactionBtn);
    
        addTransactionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog = new Dialog(getActivity());
                dialog.setContentView(R.layout.dialog_trans);
                dialog.setTitle("Add an Expense");
                dialog.setCancelable(true);
    
                dialog.show();
    
            }
        });
    

    I hope it will help you

    Let me know if there's any confusion. :)

    0 讨论(0)
提交回复
热议问题