Tom Insam

Programmatically adding a PagerTabStrip or PagerTitleStrip to a ViewPager on Android

The Lanyrd Android app does the majority of its layout programatically, because the views are described by a JSON file sent from the server. There aren’t a lot of resources on how to build a set of swipe-able views with a PagerTabStrip programmatically, so here’s what I’m doing:

pager = new ViewPager(context);

// ViewPagers must have a view ID. I use to generate them.

PagerTabStrip strip = new PagerTabStrip(context);
ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
layoutParams.height = ViewPager.LayoutParams.WRAP_CONTENT;
layoutParams.width = ViewPager.LayoutParams.MATCH_PARENT;
layoutParams.gravity = Gravity.TOP;
pager.addView(strip, layoutParams);