I am an android newbie trying to create a bottom navigation bar for my new android app similar to one there in Instagram. Like this where clicking on the search icon adds a
You can look at this example that name NavigationAdvancedSample in google samples. NavigationExtensions will solve your problem
Navigation Advanced Sample
Should I use three
activities
to display the content of correspondingtabs
orfragments
and how can I achieve that?
Definitely you should use Fragment
for each bottom navigation Item / Tab
. Like FragmentHome
, FragmentSearch
and FragmentSettings
.
To change the Fragment
, add NavigationItemSelectedListener
to your BottomNavigationView
and change Fragment
as per MenuItem
selection:
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation_view);
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = FragmentHome.newInstance();
break;
case R.id.action_item2:
selectedFragment = FragmentSearch.newInstance();
break;
case R.id.action_item3:
selectedFragment = FragmentSettings.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
Here is a tutorial about: BottomNavigationView with multiple Fragments
I need a
recyclerview
to display appointments
In your Fragment's
layout XML
, add a RecyclerView
to show list of appointments. In your Fragment
class, initialize RecyclerView
and create an ArrayList<Appointment>
and pass this list
to your Adapter
to show on RecyclerView
row items.
Here is an tutorial about: How to use RecyclerView in Fragment
How can I display the
search bar
only when thesearch
icon at bottom is clicked?
You can show/hide option item programmatically from ToolBar/ActionBar
as per your Fragment change.
In your FragmentSearch
, do below changes to show Searchbar
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragmet_search, parent, false);
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_search_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Here are some useful links:
Hope this will help to understand the scenario.
The concept of tabs is that you have to have a MainActivity, which will handle let's say 3 fragments. With the code that @Alireza suggested, you will handle the change between fragments. Please look over a tutorial on how to implement Tabs ( like this one here ) . After that, in your "SearchFragment.java" you will build your XML accordingly with your Search bar on top, and recycler view below, or whatever you wish. Suggestion: When using fragments, be sure to have a Context mContext = getActivity(); defined first, as it will be easier for you to implement all the features.
*For you to achieve the bottom tab effect, you can move android.support.design.widget.TabLayout to the bottom of the screen
you put your bottom navigation in the activity and fill it with whatever you want then you use this
bottomNavigationView.setOnNavigationItemSelectedListener(this);
To add an item select listener. You also implement required method in your activity like this
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_home:
//open home fragment
break;
case R.id.action_search:
//open search or whatever
break;
}
return true;
}
Now it's up to you to do whatever you want when some item is selected, for example when search item is selected you can show search section and hide it when other items are selected.
If you want a library for BottomBar here's one. You have to control Visibility of your SearchBar on the click of that bottom bar item. If you are trying to implement your own searchBar then check out this links link1 link2