I want to display tabview in dialog. I have one main class file which extend to FragmentActivity and inside that i am showing dialog by calling this method like given below.

private void dialogDictionary() {

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_dictionary);
    dialog.setCancelable(true);

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    ImageView imgDialogDictionaryClose = (ImageView) dialog.findViewById(R.id.imgDialogDictionaryClose);
    imgDialogDictionaryClose.setOnClickListener(v -> dialog.dismiss());

    ViewPager viewPager = (ViewPager) dialog.findViewById(R.id.dialogViewpager);
    setupViewPager(viewPager);

    TabLayout tabLayout = (TabLayout) dialog.findViewById(R.id.dialogTabs);
    tabLayout.setupWithViewPager(viewPager);

    dialog.show();
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

when i run app, I am getting this error.

  E/FragmentManager: No view found for id 0x7f0e00d5 (com.example.page:id/dialogViewpager) for fragment OneFragment{eb2a3cc #1 id=0x7f0e00d5 android:switcher:2131624149:0}
  E/FragmentManager: Activity state:
  E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.page, PID: 2710
    java.lang.IllegalArgumentException: No view found for id 0x7f0e00d5 (com.example.page:id/dialogViewpager) for fragment OneFragment{eb2a3cc #1 id=0x7f0e00d5 android:switcher:2131624149:0}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

dialog_dictionary.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@drawable/dialog_title_bar">

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true" />

    <EditText
        android:id="@+id/edtDictionarySearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/imgDialogDictionaryClose"
        android:background="@android:color/transparent"
        android:drawableLeft="@mipmap/icn_search_white"
        android:drawablePadding="10dp"
        android:hint="@string/search"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="@dimen/dialog_search_text_size" />

    <ImageButton
        android:id="@+id/imgDialogDictionaryClose"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:background="@android:color/transparent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@mipmap/icn_close" />
</RelativeLayout>

<android.support.design.widget.TabLayout
    android:id="@+id/dialogTabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabGravity="fill"
    app:tabMode="fixed" />


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bg_area"
    android:padding="@dimen/dialog_inner_padding">

    <android.support.v4.view.ViewPager
       android:id="@+id/dialogViewpager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>
</LinearLayout>

OneFragment.java

public class OneFragment extends Fragment {
public OneFragment() {

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_one, container, false);
}
}

fragment_one.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:text="Tab One"
       android:textSize="50dp" />
 </LinearLayout>
2

There are 2 answers

0
Mean Coder On

well it seems fine ...here google says to avoid directly instantiating Dialog ... Use its sub classes ... I think what you are trying to do can be done by DialogFragment... Hope it will help

2
Matthew Cawley On

I think from what I can remember, the content view for the Dialog is not added straight away. It is added at the time the dialog is displayed. What you could try doing is create a subclass of Dialog and adding a Global Layout Listener to your Dialog subclass.

When the GlobalLayoutListener fires, grab your references at this point and then remove the Global Layout Listener so it doesn't keep re-firing.

// in OnCreate()
getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout() 
    {
        //get references for viewPager
        viewPager = findViewById(R.id.dialogViewpager)
        //remove listener so it doesn't fire again
        getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

Update:

Actually, from what I am thinking, you can just create a normal dialog and add the layout listener to that

//note that dialog will need to be final to access it
//in onGlobalLayout below or have a global reference in your class

dialog.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout() 
    {
        //get references for viewPager
        viewPager = findViewById(R.id.dialogViewpager)
        //remove listener so it doesn't fire again
        dialog.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});