lisovaccaro
lisovaccaro

Reputation: 33956

Create layout programatically to add to ViewPager?

I'm working on a home replacement app, I'm using a viewpager to have three 3 layouts with lateral navigation. The problem is that these layouts are stored in three different layout resource files (layout1.xml, layout2.xml and layout3.xml) and instead of them I want to create the content for each screen dynamically.

This is an excerpt of the Adapater that states the content of each page:

   public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.layout1;
            break;
        case 1:
            resId = R.layout.layout2;
            break;
        case 2:
            resId = R.layout.layout3;
            break;
        }
        View view = inflater.inflate(resId, null);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }

How can I create a simple layout programatically and place it in one of my screens? I've been working on this a lot and cannot find any clues on how to do it, any info pointing me in the right direction will be useful.


Edit:

This is what I am trying now, I don't think I'm very far off, however all screens are blank:

Context context = collection.getContext();
LinearLayout layout = new LinearLayout(context);
TextView view = new TextView(context);
switch (position) {
    case 0:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 1:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 2:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
}
layout.addView(view);
return layout;

Upvotes: 0

Views: 1591

Answers (1)

Sudarshan Bhat
Sudarshan Bhat

Reputation: 3772

Instead of inflating layout, create objects of layouts and views and return it.

Example for position 1,

LinearLayout layout = new LinearLayout(context);
 switch (position) {
        case 0:
           TextView tv = new TextView(context);
           tv.setText("some text");
           tv.setPadding(5, 5, 5, 5);
           tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
           // And so on customize and create as many views as you want. 

           // And finally add them to your layout
          layout.addView(tv);
          return layout;

        case 1:
          .
          .
          .
        case 2:
          .
          .
          .
}

UPDATE:

Editing answer after your update on question.

I've taken your code and modified it. And this must work.

@Override
public Object instantiateItem(View container, int position) {
    Context context = collection.getContext();
    LinearLayout layout = new LinearLayout(context);
    TextView view = new TextView(context);

    switch (position) {

    case 0:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 1:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    case 2:
       view.setText("some text");
       view.setPadding(5, 5, 5, 5);
       view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
       break;
    }
    layout.addView(view);
    ((ViewPager) container).addView(layout, 0); // This is the line I added
    return layout;
}

Upvotes: 1

Related Questions