Feroz Siddiqui
Feroz Siddiqui

Reputation: 4106

Android BottomsheetFragment Viewpager Fragment does not have a view

I am trying to implement a viewpager inside bottomsheetFragment but it seems like since viewpager is inside nestedScrollview its not visible.

Below is code for opening bottomsheetFragment

        BottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();

        bottomSheetDialogFragment.show(((MyActivity)context).getSupportFragmentManager(), bottomSheetDialogFragment.getTag());

this is function get triggered from recylerview item click

below is CustomBottomSheet Fragment class:

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import sales.talentify.ai.aitalentifysales.R;
import sales.talentify.ai.aitalentifysales.landing.adapter.AddLeadAdapter;
import sales.talentify.ai.aitalentifysales.salespojo.Lead;

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
    public static final String DATA="data";
    Lead lead;
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }

        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    };


    private LinearLayoutManager mLinearLayoutManager;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        lead =(Lead) getArguments().getSerializable(DATA);

        return super.onCreateDialog(savedInstanceState);


    }

    @Override
    public void onViewCreated(View contentView, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(contentView, savedInstanceState);


    }

    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        View view = View.inflate(getContext(), R.layout.include_modal_content, null);

        EditText company_name = view.findViewById(R.id.company_name);
        Spinner lead_source = view.findViewById(R.id.lead_source);
        EditText address = view.findViewById(R.id.address);
        EditText state = view.findViewById(R.id.state);
        EditText country = view.findViewById(R.id.country);
        EditText city = view.findViewById(R.id.city);
        EditText pincode = view.findViewById(R.id.pincode);
        Button lead_submit = view.findViewById(R.id.lead_submit);
        TextView company_title = view.findViewById(R.id.company_title);
        final Button company_button = view.findViewById(R.id.company_button);
        company_title.setText(lead.getCompanyName());






        dialog.setContentView(view);

        ViewPager viewPager = view.findViewById(R.id.sales_viewpager);

        AddLeadAdapter addLeadAdapter = new AddLeadAdapter(getChildFragmentManager(),lead);
        viewPager.setAdapter(addLeadAdapter);
        CoordinatorLayout.LayoutParams layoutParams =
                (CoordinatorLayout.LayoutParams) ((View) view.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
        if (behavior != null && behavior instanceof BottomSheetBehavior) {
            ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
        }

    }



}

Below is include_modal_content layout file

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:id="@+id/main_layout"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:id="@+id/toolbar"
            android:foreground="?attr/selectableItemBackground"
            android:orientation="horizontal"
            android:paddingBottom="8dp"
            android:paddingLeft="18dp"
            android:paddingRight="18dp"
            android:paddingTop="8dp"
            android:background="@drawable/input_shape"
            android:focusable="true">

            <Button
                android:id="@+id/company_button"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="8dp"
                android:background="@drawable/circle_button_solid"
                android:text="#"

                android:textColor="@color/white"
                />

            <TextView
                android:id="@+id/company_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:maxLines="1"
                android:ellipsize="end"
                android:textSize="@dimen/h6"
                app:fontFamily="@font/lato"
                android:text="Email a Copy"
                android:textColor="@color/black_theme_text"
            />

        </LinearLayout>

       <include layout="@layout/myinclude"/>
        <Button

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lead_submit"
            android:background="@color/theme_color"
            android:text="Save Lead"
            android:textColor="@color/white"
            android:fontFamily="@font/lato"
            android:textSize="@dimen/p1"

            android:layout_alignParentBottom="true"/>
    </RelativeLayout>



 myinclude is included layout which contains nestedscrollview and viewpager as :


 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:id="@+id/main_layout"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:id="@+id/toolbar"
            android:foreground="?attr/selectableItemBackground"
            android:orientation="horizontal"
            android:paddingBottom="8dp"
            android:paddingLeft="18dp"
            android:paddingRight="18dp"
            android:paddingTop="8dp"
            android:background="@drawable/input_shape"
            android:focusable="true">

            <Button
                android:id="@+id/company_button"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="8dp"
                android:background="@drawable/circle_button_solid"
                android:text="#"

                android:textColor="@color/white"
                />

            <TextView
                android:id="@+id/company_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:maxLines="1"
                android:ellipsize="end"
                android:textSize="@dimen/h6"
                app:fontFamily="@font/lato"
                android:text="Email a Copy"
                android:textColor="@color/black_theme_text"
            />

        </LinearLayout>

       <include layout="@layout/edit_lead"/>
        <Button

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lead_submit"
            android:background="@color/theme_color"
            android:text="Save Lead"
            android:textColor="@color/white"
            android:fontFamily="@font/lato"
            android:textSize="@dimen/p1"

            android:layout_alignParentBottom="true"/>
    </RelativeLayout>

Please help me how to resolve viewpager inside this nestedscrollview.

i am getting the below exception :

  java.lang.IllegalStateException: Fragment does not have a view
        at android.support.v4.app.Fragment$2.onFindViewById(Fragment.java:2391)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1444)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:168)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:23169)
        at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
        at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
        at android.view.View.measure(View.java:23169)
        at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1502)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)

Upvotes: 2

Views: 814

Answers (2)

vaishak bg
vaishak bg

Reputation: 123

I don't know how the the above answer got expected as it leaves out very important details. You can't directly attach the bottomSheetBehavior and the bottomSheetCallback in the onCreateView method.

Override onStart instead:

override fun onStart() {
        super.onStart()
        bottomSheet =
            dialog!!.findViewById(com.google.android.material.R.id.design_bottom_sheet) as ViewGroup // notice the R root package
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) 
        // SETUP YOUR BEHAVIOR HERE AND ADD THE BOTTOMSHEETCALLBACK HERE

Then override onCreateView as mentioned above

@Override
public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.include_modal_content, container,
                false);

        // get the views and attach the listener

        return view;
    }

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 82968

Use onCreateView(..) instead of setupDialog(...) to setup UI for your BottomSheetDialogFragment. Like below

@Override
public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.include_modal_content, container,
                false);

        // get the views and attach the listener

        return view;
    }

Upvotes: 1

Related Questions