2

I have an application composed by a pageviewer with 3 fragments. one of this fragment is a map and the idea is to show a bottosheetbehaviuor on map marker click.

here is my layout:

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.android.activities.MainActivity"
    android:nestedScrollingEnabled="true">



    <RelativeLayout
        android:id="@+id/rlMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        tools:layout="@layout/content_main"
        />


    <androidx.core.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@android:color/white"
        app:behavior_hideable="true"
        app:behavior_peekHeight="155dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <TextView
            android:id="@+id/bottom_sheet_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp"
            android:textSize="16sp" />

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


then... during oncreate I setup both view pager , map and bottom sheet

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        bottomSheetText = findViewById(R.id.bottom_sheet_text);
        View bottomSheet = findViewById(R.id.bottom_sheet);
        mBottomSheetBehaviour = BottomSheetBehavior.from(bottomSheet);
        mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_HIDDEN);

        ViewPager viewPager = findViewById(R.id.viewpager);
        setupViewPager(viewPager);

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

the viewpager and map via a call to a a func

 private void setupViewPager(ViewPager viewPager) {
        Log.d(TAG, "setupViewPager");
        ViewPagerAdapter adapter = new ViewPagerAdapter
                (getSupportFragmentManager());
        createUpMap();
        adapter.addFragment(mapFrag, "Map");
        //TEMP
        adapter.addFragment(new SupportMapFragment(), "List");
        adapter.addFragment(new SupportMapFragment(), "Session");
        viewPager.setAdapter(adapter);


private void createUpMap() {
        Log.d(TAG, "createUpMap");
        if (mapFrag == null) {
            Log.d(TAG, "mapFrag is null, creation of map...");
            mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.rlMap);
            mapFrag = new SupportMapFragment();

lastly, I ovveride marker click to display the bottomsheet:

 mMap.setOnMarkerClickListener(marker -> {

            Log.e(TAG, "POINT HAS BEEEN CLICKED status: "  + mBottomSheetBehaviour.getState());
            if (mBottomSheetBehaviour.getState() == BottomSheetBehavior.STATE_EXPANDED) {

                bottomSheetText.setText(marker.getTag().toString());
                mBottomSheetBehaviour.setPeekHeight(200);
                mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED);

I launch the application, click on the marker and in the log in log I can see correct state ( I'm printing it as you see from calls) but absolutely no way to see it on screen. I 'm little out of ideas as there's no error and i don't know how to move.

Someone can move me in the right direction ?

many thanks!

giorgio
  • 105
  • 1
  • 8

1 Answers1

0

You can try to build your own BottomSheetDialogFragment and show it with

val bottomSheetDialogFragment = MytBottomSheetDialogFragment.newInstance()
//show it
bottomSheetDialogFragment.show(fragmentManager!!, bottomSheetDialogFragment.tag)

So you don't have to create the view inside your layout file and can simple reuse it.

Update:

You can set your layout in the setupDialog function. Just overrride it and inflate your custom BottomSheetLayout.

public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);

    View contentView = View.inflate(getContext(), R.layout.custom_layout, null);
    dialog.setContentView(contentView);
}
Thomas Meinhart
  • 659
  • 2
  • 7
  • 28
  • Hello , i’m Gonna try that to see if works , problem is that actually I need to have a particular layout inside of that – giorgio Oct 11 '19 at 06:07
  • thanks, for your answer, I see this is btw bottomsheetdialogfragment, I'm using bottosheetbehaviuor instead who has just the setstate method. how could this be readapted ? – giorgio Oct 11 '19 at 07:09
  • Maybe this answer helps you: https://stackoverflow.com/a/35976745/7454336 – Thomas Meinhart Oct 11 '19 at 09:15
  • Thanks again, unfortunately I cannot find a way to display in fragment... really boring!!! :( – giorgio Oct 11 '19 at 14:56