Question

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?

Was it helpful?

Solution

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top