JD Byrnes
JD Byrnes

Reputation: 831

PagerView overlapping PagerTabStrip / PagerTitleStrip

I've been trying for about a week to get my PagerView not to overlap the TitleStrip.

I've tried absolutely everything I can think of, and one StackOverflow question that looked like the same question, had an answer that wasn't applicable.

It appears that both the PagerTitleStrip and the TextView start at 0,0 (left,top)

Any help would be appreciated. Note that I can't use any XML (inc Layout.xml) so it's all done programatically.

Here's a full working example of my problem:

package com.example.projname;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
        PagerTabStrip myPagerTabStrip = new PagerTabStrip(this);
        myPagerTabStrip.setGravity(Gravity.TOP);
        ViewPager viewPager = new ViewPager(this);
        viewPager.addView(myPagerTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        viewPager.setAdapter(myPagerAdapter);
        setContentView(viewPager);
    }

    class MyPagerAdapter extends PagerAdapter {
        public final String[] Titles = {
            "Title One",
            "Title Two",
            "Title Three",
            "Title Four",
            "Title Five"
        };

        @Override
        public int getCount() {
            return Titles.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Titles[position];
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView textView = new TextView(getApplicationContext());
            String myString = new String("Page " + (position + 1) + "\r\n");
            textView.setText(myString + myString + myString + myString + myString + myString);
            container.addView(textView);
            return textView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}

Edit1: Adding a screenshot of the problem: The text Page1 is shown much higher than it should be

Edit2: Updating question with answer (in-case anyone tries this in the future

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutParams layoutParams = new LayoutParams();
    layoutParams.height = LayoutParams.WRAP_CONTENT;
    layoutParams.width = LayoutParams.MATCH_PARENT;
    layoutParams.gravity = Gravity.TOP;
    PagerTabStrip myPagerTabStrip = new PagerTabStrip(this);
    MyPagerAdapter myPagerAdapter = new MyPagerAdapter();       
    ViewPager viewPager = new ViewPager(this);
    viewPager.addView(myPagerTabStrip, layoutParams);
    viewPager.setAdapter(myPagerAdapter);
    setContentView(viewPager);
}

Upvotes: 3

Views: 4909

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

Note that I can't use any XML

Why?

Any help would be appreciated.

Try a version of addView() that takes a LayoutParams object as a parameter, and use an instance of ViewPager.LayoutParams. Also, use the gravity field on the ViewPager.LayoutParams instead of setGravity().

If that does not help, temporarily set up a ViewPager and PagerTitleStrip in XML (e.g., in a separate project) and examine the results in Hierarchy View to see how the XML approach constructs matters, so you can try to replicate it.

Upvotes: 2

Related Questions