Reputation: 6899
I am using pager sliding tab strip where I am getting extra space at right side. How to equally fit for two categoreis in tabs?
My code is as follows:
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
tabs.setIndicatorColor(Color.BLUE);
tabs.setIndicatorHeight(2);
mPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
mPager.setAdapter(adapter);
tabs.setViewPager(mPager);
tabs.setShouldExpand(true);
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
//final int PAGE_COUNT = 2;
public ViewPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
private final String[] TITLES = { "SIGN IN", "REGISTER"};
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
and my xml is :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.views.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="@drawable/background_tabs" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/colors"
android:layout_below="@+id/tabs"
tools:context=".MainActivity" />
Here is my screenshot
Upvotes: 4
Views: 6318
Reputation: 715
To split the tabs equally like WhatsApp add the below code in your mainActivity.java file.
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// CategoryFragmentPagerAdapter adapter =
// new CategoryFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(new CategoryFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
}
}
Kindly take note to make it work don't add
app:tabMode="scrollable"
code in the XML file.
Upvotes: 1
Reputation: 332
Add
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
tabView.setLayoutParams(lp);
To the populateTabStrip() method in SlidingTabLayout. It should be looks like:
private void populateTabStrip() {
final PagerAdapter adapter = mViewPager.getAdapter();
final OnClickListener tabClickListener = new TabClickListener();
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
TextView tabTitleView = null;
if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
}
if (tabView == null) {
tabView = createDefaultTabView(getContext());
}
if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
}
tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
tabView.setLayoutParams(lp);
mTabStrip.addView(tabView);
}
}
Upvotes: 2
Reputation: 6899
You need to customise in PageSlidingTabStrip by making width as 0. I will show what changes I made in that class.
public static int width = 0;
Then in MainActivity:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
PagerSlidingTabStrip.width = width / 2;
setContentView(R.layout.loginpager);
//others are all same.
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
tabs.setIndicatorColor(getResources().getColor(R.color.blue));
tabs.setIndicatorHeight(5);
tabs.setTextColor(getResources().getColor(R.color.blue));
FragmentManager fm = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
mPager.setAdapter(adapter);
tabs.setViewPager(mPager);
Here is output screen
Upvotes: 0
Reputation: 2138
Instead of adding setShouldExpand
in java file, i think will have to add it in xml file... like this ..
<com.views.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="@drawable/background_tabs"
app1:pstsShouldExpand="true"/>
also add xmlns:app1="http://schemas.android.com/apk/res/your.package.name"
in your parent layout like this..
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app1="http://schemas.android.com/apk/res/your.package.name" >
Hope it works...!!
Upvotes: 7
Reputation: 2468
In my case I used some custom sliding pager, the below line of code helped me,
View tabView = null;
if (mTabViewLayoutId != 0) {
tabView = LayoutInflater.from(getContext()).inflate(
mTabViewLayoutId, mTabStrip, false);
TextView tabTitleView = (TextView) tabView
.findViewById(mTabViewTextViewId);
tabTitleView.setWidth(getResources().getDisplayMetrics().widthPixels
/ mNumberOfTabs);
}
Here, mTabViewLayoutId is a custom layout resourse id
Hope something like this will help you.
Upvotes: 0