0

I have an activity, called CollectionActivity, with a viewpager. The viewpager has it owns adapter, PagerAdapter: CollectionPagerAdapter extends PagerAdapter.

The important instantiateItem method:

@Override
public Object instantiateItem(View collection, int position) {
        LinearLayout lay = null;
        if(position==0){
            lay = new Report(context);
            lay.setOrientation(LinearLayout.VERTICAL);
            ((ViewPager) collection).addView(lay);
        }
//else for the other positions...


        return lay;
}

The constructor in the class Report (extends LinearLayout) looks like:

    public Report(Context context) {
    super(context);
    this.context = context;

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.activity_report, null);

    this.addView(view);

    Button button = new Button(context);
    button.setText("LinearLayoutExtended");

    this.addView(button);

The problem; the view I´m getting from R.layout.activity_report shows up with a really tiny width on the screen. The Button below it however, get the width it needs.

activity_report.xml has the width and height of fill parent, and starts with a horizontal layout filled with textboxes, buttons and other stuff. Activity_report.xml looks great on the preview in eclipse, and has looked fine when I used it to an activity before.

Why cant I use this.addView(view) to add my layout activity_report.xml and getting it on the whole screen? (not with a tiny width as it is now)

Malin
  • 655
  • 4
  • 10
  • 20
  • Without xml files it's a guessing game. I can assume though that the Report class assumes `match_parent` for height as default. You don't seem to be passing any layout attributes in the constructor. – Joel Bodega Jul 13 '13 at 16:45

2 Answers2

0

When you use addView(View) you have to specify the LinearLayout.LayoutParams of the parent view.

Frederick Nyawaya
  • 2,285
  • 1
  • 15
  • 19
  • The viewpager, which is the parent already has the params in the xml file, android:layout_width="fill_parent" android:layout_height="fill_parent" The view itself, the layout, has also fill_parents stated in the xml file. Do I really need to do it again..? – Malin Jul 13 '13 at 17:07
0

For some reason, everything worked when I had no images lying by themselves in my view xml file. I simply put each image in their own linear layout - and I could now see the whole layout on the whole screen.

I have no idea why this was the issue. If you know, leave a comment!

Malin
  • 655
  • 4
  • 10
  • 20