Question

I would like to place the navigation bar at the top of each activity (see: screenshot). The navigation bar should have two buttons (previous and next activity) and activity indicator. I've seen some solutions on SO (using include, fragments, actionbar), but which one would be the best or may someone have another option?

enter image description here

Was it helpful?

Solution

This functionality is provided by the ViewPager class. Instead of activities you provide fragments and you get quick and efficient horizontal scrolling between provided views. You would use just one activity, so your navigation bar will be always visible. The only part that would be changing are the pages of the ViewPager.

As for your navigation bar, there are couple of options. You can provide your own implementation of the view, but you will have to create a listener for changing pages that will set current page for the ViewPager. This is the most complicated way.

Another solution is to use ActionBarSherlock that you would find useful in many ways. There you can connect the navigation of action bar with your ViewPager. This solution is the simplest one and the most robust. In the Google IO app you can see how it's done. The home activity is what you should be looking at. The problem with this solution is that you will get predefined view for navigator.

The last solution probably best for you is to try and play with Page Indicator, that could simplify your navigation bar implementation.

Everything mentioned here stands on the ViewPager, so be sure to use it. I don't know of any other better solution. The question is how would you like to implement the navigation.

OTHER TIPS

I agree with both Commonsware and MartinSVK. I would invite you to try out the code sample at http://code.google.com/p/sherlock-demo/. As both of the aforementioned posters mention, it uses ViewPager to let you swipe horizontally between Fragments. But it also uses Tab navigation, which provides visual indication at the top of the screen, as you originally called for. For ideas about Android UI creation, you really should take a few minutes to look around http://developer.android.com/design.

My choose is creating shared activity class(BaseActivity for example) and override onCreate method which setup basic layout. Child clases should use setContent instand of setContentView

public class BaseActivity extends Activity {
    private LayoutInflater mInflater;

    @Override
    public void onCreate(Bundle b) {
         super.onCreate(b);
         setContentView(R.layout.base);
    }

    public void setContent(View content) {
         setContentPart(R.id.baseContentContainer, content);
    }

    public void setContent(int contentLayoutId) {
        setContentPart(R.id.baseContentContainer, contentLayoutId);
    }

    private void setContentPart(int containerId, int layoutId) {
        final LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        setContentPart(containerId, inflater.inflate(layoutId, null));
    }

    private void setContentPart(int containerId, View view) {
        final ViewGroup vg = (ViewGroup) findViewById(containerId);
        vg.setVisibility(view != null ? View.VISIBLE : View.GONE);
        vg.removeAllViews();
        if (view != null) vg.addView(view, layoutFillParent);
    }

    public LayoutInflater getLayoutInflater() {
        if (mInflater == null) mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return mInflater;
    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top