How to Create a custom Dialog with Tabs (Android)

image from blog.smartthings.com


Just earlier working on an app, I realized I needed to display tabbed information to users in a dialog box. This is quite different from the usual Tabbed Activity.



Dependency

The first step is to make sure you have "'com.android.support:design" in your build.gradle file.

Layouts

Next is to create the layout xml files for the custom dialog and your fragment. Your custom dialog may look like this:

dialog_sample.xml

And for the purpose of this tutorial, we will use a minimal Fragment, like the one indicated below:

fragment_sample.xml



Implementation

Next up is, building the java classes for the dialog, the fragment adapter, and the fragments.

Here is a sample implementation of the viewpager's adapter:

CustomAdapter.java
Note: you must override getTitle for the Tabs to have proper names.

Below is also a sample fragment for illustration purposes, we'll keep it simple for now:

CustomFragment.java

Go ahead and create your dialog's class:

TabbedDialog.java


And there you have it, it displays similarly to what's shown below:

Full source can be found here: https://github.com/kunmi/BlogTutorials/tree/master/TabbedDialog



6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is what I have been looking for. Thank you. One question however. I am using a RecyclerView in my CustomFragments. How would I pass the data from the RecyclerView in CustomFragment to MainActivity?

    ReplyDelete
    Replies
    1. how create two tabbed dialog box one tab for login and another for signup,

      Delete
  3. how to define getChildFragmentManager(), please explain.

    ReplyDelete
  4. How to define HEIGHT of FragmentDialog, as WrapContent?

    ReplyDelete