0

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 instagram.jpg where clicking on the search icon adds a search bar in action bar. I am building an app for reminding the user about his medical appointments which has three navigation tabs at the bottom. I have created till this Screenshot_2017-06-04-11-59-56.png after this I am stuck. Should I use three activities to display the content of corresponding tabs or fragments and how can I achieve that.

I need a recyclerview to display appointments. How can I display the search bar only when the search icon at bottom is clicked. Have searched a lot on achieving this but could not find anything useful.

Any suggestions for code or library which achieves the same would be great help thanks.

yogeshmanjhi
  • 335
  • 1
  • 4
  • 17
  • 1
    It seems that you don't really know a lot of basic concepts in Android development. I would suggest you follow a proper tutorial on Android development to learn the basics and then try on your own. By then you should know how to use fragments, tabs and dynamically showing/hiding the search bar. – paradite Jun 04 '17 at 07:09

5 Answers5

7

Should I use three activities to display the content of corresponding tabs or fragments 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 the search 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:

  1. Android Toolbar Adding Menu Items for different fragments
  2. Hide/Show Action Bar Option Menu Item for different fragments
  3. Adding ActionBar Items From Within Your Fragments

Hope this will help to understand the scenario.

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • 1
    Most welcome. If my answer seems useful, I hope you will give an upvote too. thank you~ – Ferdous Ahamed Jun 04 '17 at 07:59
  • 1
    @ FAT upvoted and accepted that as an answer thanks a lot :) – yogeshmanjhi Jun 04 '17 at 08:01
  • 1
    best answer... and I have a doubt... what to do if need to set frangments inside fragmentHome? – Simon Aug 03 '17 at 16:36
  • 1
    @simon just use viewpager in fragmenthome layout. Create an FragmentPagerAdapter and populate your required fragments on viewpager. – Ferdous Ahamed Aug 03 '17 at 18:23
  • This is a great answer, but, Instagram retains the state of fragments, if you use `transaction.replace(R.id.frame_layout, selectedFragment);` like you did, the fragments will be re-create every time. This is exactly the issue that I'm having now... – ClassA Apr 27 '18 at 12:06
  • @FerdousAhamed I have a clarification. If I use the fragments, inside my main activity. (i.e) for each tab i have each fragments but all are inside the main activity. Here how can I handle highlight of each tab if the user navigates to the other screen by menu labels not by footer icons. And how can I handle the backpress needs to go back the screen and how it will be handled? – sejn Nov 25 '20 at 11:09
0

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.

Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • @ Alireza should i use three activities or use three separate fragments to display the details of each tab – yogeshmanjhi Jun 04 '17 at 07:32
  • @ Alireza i have already implemented the code you suggested just wanted to know which one should i use next to display details fragments or activities.Thanks – yogeshmanjhi Jun 04 '17 at 07:53
  • 1
    Fragments! You use an activity that contains you bottom navigation and a frame layout for containing fragments, then we user clicks on a tab you display a fragment in that container. – Alireza Ahmadi Jun 05 '17 at 07:30
0

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

MrCurious
  • 150
  • 3
  • 11
  • @ Nikhya thanks for the links.should i use three fragments or three activities to display the details of tabs – yogeshmanjhi Jun 04 '17 at 07:42
  • use three fragments with View pager so that you will get a look like WhatsApp/ Instagram. You can slide thos fragments because of ViewPager. You can serach more for ViewPager – MrCurious Jun 04 '17 at 07:45
0

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

razgraf
  • 221
  • 1
  • 11
0

You can look at this example that name NavigationAdvancedSample in google samples. NavigationExtensions will solve your problem

Navigation Advanced Sample

Özer Özcan
  • 1,253
  • 16
  • 17