1

I am new to fragment. I want to show an image in Dialog. Can anybody provide a code snippet of how to show a DialogFragment with support for all devices?

1 Answer 1

8

Make a layout file and put your image in it (e.g. dialog_fragment.xml).
Make your dialog fragment (e.g. MyDialogFragment.java)

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

public class MyDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment, container);
        // This shows the title, replace My Dialog Title. You can use strings too.
        getDialog().setTitle("My Dialog Title");
        // If you want no title, use this code
        // getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return view;
    }
}

Imports: import android.support.v4.app.FragmentManager;

FragmentManager fm = getSupportFragmentManager();
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(fm, "dialog_fragment");
1
  • it is 2018... and I still forget support in getSupportFragmentManager, Thanks bro Jun 28, 2018 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.