Reputation:
I know I am not the first to ask this question but I have referred many SO post regarding this but nothing is solved my query .
What I want to do is in my MainActivity(Bottom Navigation bar Activity) I have Bottom Navigation Bar, In this MainActivity I have cardviews If I clicked on the cardview I need to show another fragment in that fragment I want to hide the bottom navigation bar .And When I nav back to MainActivity botoom Navigation bar should be there.
Here in my case Alarm.java is the fragment where I want to hide the bottom navigation bar.
Alarm.java
public class Alarm extends Fragment {
private OnFragmentInteractionListener mListener;
public Alarm() {
}
public static Alarm newInstance(String param1, String param2) {
Alarm fragment = new Alarm();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_alarm, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Upvotes: 1
Views: 6402
Reputation: 150
I have tried the method by @Suleyman but it didn't work for me. The simplest solution is to use a public static
method in the MainActivity
and reference it from the fragment where you want to hide the bottom navigation bar.
Don't forget to initialize navView
as public static
.
In your MainActivity
public static void hideBottomNav(){
navView.setVisibility(View.GONE);
}
public static void showBottomNav(){
navView.setVisibility(View.GONE);
}
In your MyFragment
@Override
public void onResume() {
super.onResume();
MainActivity.hideBottomNav();
}
@Override
public void onStop() {
super.onStop();
MainActivity.showBottomNav();
}
Upvotes: 0
Reputation: 2943
In your MainActivity
you can implement two methods that will be responsible for showing and hiding your BottomNavigationView
. For example, these two methods animate it sliding up and down:
private void hideBottomNavigationView(BottomNavigationView view) {
view.clearAnimation();
view.animate().translationY(view.getHeight()).setDuration(300);
}
public void showBottomNavigationView(BottomNavigationView view) {
view.clearAnimation();
view.animate().translationY(0).setDuration(300);
}
In MainActivity
you call hide
right before opening your CardView
, and call show
in onCreate
or onResume
.
EDIT:
But, I think that a cleaner way would probably be to create an interface in your Fragment:
public interface OnCardViewOpenedInterface{
void onOpen(); // hide bottom bar when photo is opened
void onClose(); // show bottom bar when photo is opened
}
And call these methods in onStop
and onResume
of your Fragment
:
@Override
public void onStop() {
super.onStop();
mListener.onClose();
}
@Override
public void onResume() {
super.onResume();
mListener.onOpen();
}
And then implement the interface in your MainActivity
, override the methods onOpen()
and onClose()
and inside call your hide
and show
methods.
They will probably both work, maybe the second one is overcomplicated, it's just I like it more.
Upvotes: 3