Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:39:49+00:00 2026-06-04T04:39:49+00:00

Is it possible to add views to a different layout than the one called

  • 0

Is it possible to add views to a different layout than the one called in setContentView() in the OnCreate()?

Im trying to use Zylincs ViewPager (found at http://www.zylinc.com/blog-reader/items/viewpager-page-indicator.html) and I have that part setup and working great.

What this leaves me with is 4 layouts. 1 is main.xml, and the other three are main_pg0.xml, main_pg1.xml, and main_pg3.xml

The three pages are where the content of each “page” is where main.xml contains the page viewer.

In the onCreate im using setContentView(main.xml).

What im trying to do now is be able to add textViews programically to some of the pages.

What I have right now is:

LayoutInflater factory = getLayoutInflater();

View layout = factory.inflate(R.layout.main_pg0, null);
View linearLayout = layout.findViewById(R.id.linearLayout);

TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

((LinearLayout) linearLayout).addView(valueTV);

This is as far as I’ve been able to get, which does not add the textview at all.

—-EDIT—-

Hoping to clarify a little more what im trying to do
Heres my code

Main.java

public class Main extends FragmentActivity implements PageInfoProvider {
  public GlobalVars globalVars;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    globalVars = (GlobalVars) getApplicationContext();

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    ViewPagerIndicator indicator = (ViewPagerIndicator) findViewById(R.id.indicator);
    myPager.setOnPageChangeListener(indicator);

    indicator.init(0, adapter.getCount(), this);

    Resources res = getResources();
    Drawable prev = res.getDrawable(R.drawable.indicator_prev_arrow);
    Drawable next = res.getDrawable(R.drawable.indicator_next_arrow);

    indicator.setArrows(prev, next);
  }

  private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
      return 3;
    }

    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.main_pg0;
        LoadVehicles();
        break;
      case 1:
        resId = R.layout.main_pg1;
        break;
      case 2:
        resId = R.layout.main_pg2;
        break;
      }

      View view = inflater.inflate(resId, null);
      ((ViewPager) collection).addView(view, 0);

      return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
      ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public void finishUpdate(View arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == ((View) arg1);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
      // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public void startUpdate(View arg0) {
      // TODO Auto-generated method stub

    }

  }

  public String getTitle(int position) {
    String title = "--Untitled--";
    switch (position) {
    case 0:
      title = "Page 0";
      break;
    case 1:
      title = "Page 1";
      break;
    case 2:
      title = "Page 2";
      break;
    }
    return title;
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <com.zylinc.view.ViewPagerIndicator
    android:id="@+id/indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#EAEAEA"
    android:paddingBottom="8dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="8dp" />
  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffeaeaea" >
    <View
      android:layout_width="fill_parent"
      android:layout_height="1dp"
      android:layout_alignBottom="@+id/current"
      android:background="#C5C5C5" />
    <ImageView
      android:id="@+id/current"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerInParent="true"
      android:src="@drawable/indicator_current" />
  </RelativeLayout>
  <android.support.v4.view.ViewPager
    android:id="@+android:id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

main_pg0.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FFFFFF"
  android:orientation="vertical"
  android:id="@+id/linearLayout" >
  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="5dp"
      android:text="main_pg0.xml"
      android:textColor="#0C0C0C"
      android:textSize="18sp" />

</LinearLayout>

What im trying to do is add a text view programmatically to main_pg0.xml ONLY, while leaving the other 2 pages alone

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:39:49+00:00Added an answer on June 4, 2026 at 4:39 am

    You haven’t explicitly mentioned whether you’re feeding Fragments to the ViewPager, but since you’re working from the Zylinc implementation, I’m going to assume you are. As a matter of fact, I’ll just use the provided example code to show you the idea.

    The key to understanding how to realise what you’re after is to get that the pages in the ViewPager are self-contained units in the form of Fragments. They are reusable, have their own life cycle, and can handle their own layouts. Hence, if you want to make runtime modifications to the layout of the pages, you will want to do this inside the Fragment(s) that make up these pages.

    The following method can be found in the example code’s ItemFragment class. I’ve simply added the TextView from your own code snippet to it. For demonstration purposes, I did hide the ListView that used to fill up all the space in the date_fragment.xml layout file.

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.date_fragment, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText(readableDateFormat.format(date));
    
        // changes below
        TextView valueTV = new TextView(getActivity());
        valueTV.setText("hallo hallo");
        valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        ((ViewGroup)v).addView(valueTV);
    
        return v;
    }
    

    That will give you the “hallo hallo” text (you’re a Dutchie? ;)) directly underneath the date in the page:

    result of code snippet

    By the way, you might want to consider switching to Jake’s ViewPagerIndicator. Although I haven’t closely looked at the one you’re currently using, Jake’s seems more flexible to me and cleaner to work with. Up to you of course.


    Edit: Okay, so you’re not actually using fragments in the ViewPager, but are rather feeding it the layouts. That’s fine, although it’ll probably become harder to manage when the pages get more complex. Anyways, key to changing the page’s layouts at runtime is still to do it whenever you build/inflate it. When using fragments, that would be in the overridden method, as shown above. In your case, however, you’ll need to do it directly in instantiateItem(...).

    I didn’t actually type this out in an IDE, so please mind and minor syntactical errors.

    public Object instantiateItem(View collection, int position) {
    
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        View view = null;
        switch (position) {
        case 0:
            view = inflater.inflate(R.layout.main_pg0, null);
            TextView valueTV = new TextView(getActivity());
            valueTV.setText("hallo hallo");
            valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            ((ViewGroup)view).addView(valueTV);
            break;
        case 1:
            view = inflater.inflate(R.layout.main_pg1, null);
            // or perhaps better for readability: build the layout in a different method, passing in the root
            buildSecondPageLayout(view);
            break;
        case 2:
            view = inflater.inflate(R.layout.main_pg2, null);
            buildThirdPageLayout(view);
            break;
        }
    
        return view;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add views to LinearLayout dynamically as much as it possible
How to add multiple views in one template of django ? Is it possible
Is it possible to create views on different threads and add them to a
Is it possible to add the heading for each menu list? Something like: <asp:Repeater
Is it possible to add a dependency to the dependencies.yml that points to a
Is it somehow possible to add a dynamic Tile in WP7, where i take
Is it possible to add a custom tab to a project properties page in
Is it possible to add attributes at runtime or to change the value of
Is it possible to add an image overlay to a google map that scales
Is it possible to add custom command-line arguments to an Eclipse .app folder? In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.