Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to reuse a footer menu in Android

I'm trying to implement a menu in the footer of an app, similar to i.e. the Engadget app. As I understand, getting a standard TabLayout to work in this way is not trivial (if at all possible?).

As I see quite a few apps using this interface paradigm, I would assume there is a clever way of doing it. Currently, I'm defining the layout by adding an include line at the end of every layout. This works fine as far as rendering is concerned, but I have to add the onClickListeners to each activity. can we define clickListener through XML?

To sum up my question: What is the best way to implement a shared footer navigation accross several Activities?

like image 928
Gunnar Lium Avatar asked May 28 '10 12:05

Gunnar Lium


1 Answers

You can make all your activities derive from a common base class that extends Activity and put a method in there for building out the menu. Use a RelativeLayout as the main container, stick a horizontal LinearLayout on the bottom (layout_alignParentBottom="true"), and then align your main "content" container (whatever type of layout you want for the particular activity) above that. Using android:weight on whatever you put in your menu (ImageButton, for example) and then android:weightSum on the container of the menu, they'll be spaced evenly. So, if you have four ImageButtons with android:weight="1" and the LinearLayout that contains them have android:weightSum="4" you should be good.

So, TabContentActivity can extend Activity, and then all of your specific activities extend TabContentActivity. TabContentActivity has an onCreate that calls super.onCreate and then call some private method for building out the menu. Then, when your derived classes call super.onCreate in their own onCreate, your "tabs" are built. You can have an Enum in TabContentActivity representing each tab with a local variable of that Enum type to dictate which tab is highlighted.

Don't listen to people telling you not to do it that way. If that's the UI you want, don't be constrained by the environment you're working in. Just as you have responses telling you that's not the "right way to do it in Android", you also currently have two up votes.

like image 173
Rich Avatar answered Sep 30 '22 19:09

Rich