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 http://stackoverflow.com/a/15442997/656736 to generate them.
pager.setId(MyApplication.generateViewId());

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);

Published by Tom Insam

programming / london / san francisco / bacon

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s