Search code examples
androidandroid-dialogfragmentshowcaseview

ShowcaseView on top of DialogFragment


I am using ShowcaseView in an Android App. I want to be able to display the ShowcaseView on top of a DialogFragment. But for me, the ShowcaseView is displaying underneath the DialogFragment.

There are open issues on this subject on github for this project. But I thought that I would ask here as was suggested. This post mentioned that he solved the issue "using a fake activity as a dialogfragment." But I am not sure what that means. Maybe that means that instead of using a DialogFragment, he used an Activity and is displaying it as a DialogFragment? I know that you can set the theme of an activity to be a Dialog in the Manifest: android:theme="@android:style/Theme.Dialog".

The other github issues are here and here.

I am using the "legacy" version of ShowcaseView like this:

ViewTarget target = new ViewTarget(R.id.imageView, getActivity());
ShowcaseView.insertShowcaseView(target, getActivity(), R.string.showcase_title, R.string.showcase_details);

Solution

  • So to get ShowcaseView to display properly on a DialogFragment, we have to show the DialogFragment as a Fragment instead of showing the DialogFragment as a Dialog.

    In the DialogFragment, we could have something like this:

    public static class MyDialogFragment extends DialogFragment {
    
    public static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello_world, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("This is an instance of MyDialogFragment");
        return v;
    }
    

    That we could show it in an Activity like this:

    public class MyDialogActivity extends Activity{
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_manager);
        if (savedInstanceState == null) {
            MyDialogFragment fragment = MyDialogFragment.newInstance();
            getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frameLayout, fragment, "Some_tag")
                .commit();
        }
    }
    

    activity_contact_manager.xml layout could be:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    

    And the AndroidManifest.xml entry could be:

    <activity
        android:name=".activity.MyDialogActivity"
        android:theme="@android:style/Theme.Dialog" />