4

I'm working on an app which uses a ViewPager with two swiping views (first_page.xml and second_page.xml) and has one activity (activity_main.xml).

When in the main activity class I try to access TextView (which is located in the first_page.xml) using findViewById it can't be found.

This is my activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

This is my first_page.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/someText"
        android:text="example"
        android:textSize="42sp" >
    </TextView>
</LinearLayout>

This is my customPagerAdapter

public class CustomPagerAdapter extends PagerAdapter {

    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.first_page;
        break;
    }
    case 1: {
        resId = R.layout.second_page;
        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 boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}

@Override
public int getCount() {
    return 2;
}
}

And this is my MainActivity, where I want get the first_page layout, find its TextView called someText, and set a new custom font.

public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create and set adapter
    CustomPagerAdapter adapter = new CustomPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.pager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    /*
     *  Here i want to set a new custom font but it seems the someText can't
     *  be found because the Activity searches for it in the 
     *  layout.activity_main and not in the layout.first_page
    */
    TextView textView = (TextView) findViewById(R.id.someText);
    Typeface font = Typeface.createFromAsset(getAssets(), "roboto_thin.ttf");  
    textView.setTypeface(font);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

I'm struggling for two days with this problem.

Any suggestions?

1 Answer 1

-1

The textview is encapsulated in the container of the fragment. If you get a reference to the correct fragment and get a reference to the container you can call findViewById(R.id.someid).


Sample code:

// TODO cast checks
int item = mViewPager.getCurrentItem();
Fragment frag = (Fragment) mPagerAdapter.getItem(item);
View container = frag.getView();
TextView searchTextView = (TextView) container.findViewById(R.id.someid);

Or is your case:

// TODO add null + cast checks
TextView textView = (TextView) ((Fragment)adapter.getItem(0)).getView().findViewById(R.id.someText);
6
  • Thanks for you reply! But what should i put in the getItem() method in the customPagerAdapter class?
    – Andrea
    Dec 30, 2013 at 14:19
  • No problem, if the answer helped you to resolve your problem: You should accept the answer and possibly upvote the post ;)
    – Tobrun
    Dec 30, 2013 at 14:25
  • I have a problem with your solution. I don't understand how to implement the getItem() method in the customPagerAdapter class. Can you help me?
    – Andrea
    Dec 30, 2013 at 14:33
  • I have added the replaced onCreate method. If this doesn't work let me know. You could also use another approach, passing the arguments to the fragments upon creation and let the fragments itself handle the typeface. (which would be a better design in oppionion)
    – Tobrun
    Dec 30, 2013 at 14:58
  • I've copied your code in the onCreate method but Eclipse finds two errors and suggests two things: add (Object) cast to 'adapter.getItem(0)' and change getView() cast
    – Andrea
    Dec 30, 2013 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.