Ad

Is It Possible To Open BottomSheetDialog With BottomNavigationBar?

I want to open bottom sheet dialog when i clicked bottom navigation item (Yandex Maps android app like. There is map on background always. Bottom navigation items just open bottom sheet dialog).

yandex view However i can not do that. Is it possible and if it is how?

I tried some fragment methods but app is crashing.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".MainActivity" >

    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/berkay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.mapbox.mapboxsdk.maps.MapView>

    <FrameLayout
        android:id="@+id/frame_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/main_bottom">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/main_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation"
        android:background="?android:attr/windowBackground">
    </android.support.design.widget.BottomNavigationView>


</RelativeLayout>

Bottom navigation item selection

private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            Fragment selectedFragment = null;

            switch (menuItem.getItemId()){
                case R.id.navigation_map:

                    break;
                case R.id.navigation_search:
                    SearchFragment searchFragment = new SearchFragment();
                    searchFragment.show(getSupportFragmentManager(),"SearchFragment");
                    break;
                case R.id.navigation_event:
                    selectedFragment = new EventsFragment();
                    break;
                case R.id.navigation_profile:
                    selectedFragment = new ProfileFragment();
                    break;
            }

            getSupportFragmentManager().beginTransaction().replace(R.id.frame_main,selectedFragment).commit();

            return true;
        }
    };

SearchFragment.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapboxMap;

public class SearchFragment extends BottomSheetDialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_search,container,false);
        return v;
    }
}

Logcat Error for SearchFragment case

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

Ad

Answer

I did it.

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            Fragment selectedFragment = null;

            switch (menuItem.getItemId()){
                case R.id.navigation_map:
                    MapFragment mapFragment = new MapFragment();
                    mapFragment.show(getSupportFragmentManager(),"MapFragment");
                    break;
                case R.id.navigation_search:
                    SearchFragment searchFragment = new SearchFragment();
                    searchFragment.show(getSupportFragmentManager(),"SearchFragment");
                    break;
            }


            return true;
        }
    };
Ad
source: stackoverflow.com
Ad