sojutyp sojutyp - 9 months ago 30
Android Question

Creating a "central" menu in an android app

I just started making an app with android studio. I found a good tutorial how to set up a navigation menu bar (which is located on the bottom of my app - similar to instagram). In the tutorial, the navigation bar only switched between different textViews, not between activities.

In my app, I want to switch between 4 activities when pressing the according button on the navigation bar. To achieve this, I copied the same menu bar code to all my activities, which doesn't seem to be the solution. If I switch to another activity, the code basically reloads and shows the first menu icon as "active". Also, if I want to change my menu, I need to change it on every activity.

On a website I can just generate my menu using JavaScript and have it pulled on different .htmls from only one source. How can I achieve this in a android app? Thanks a lot!

Answer Source

In my app, I want to switch between 4 activities when pressing the according button on the navigation bar... If I switch to another activity, the code basically reloads and shows the first menu icon as "active"

You loaded a whole new Activity. So, yes, only the first item is going to be active (unless set otherwise, but that's not the point).

The point of a TabLayout, BottomNavigationView, or DrawerLayout is that those are stored in a singular view, and you FragmentTransaction.replace() between Fragments within that one Activity.

On a website I can just generate my menu using JavaScript and have it pulled on different .htmls from only one source

Are you saying like Angular routers or ReactJS views that dynamically change?

In Android, think of a <FrameLayout> as a HTML <div>, then you swap in Fragment's in there