Newts
Newts

Reputation: 1372

How to access ImageButton from fragment in fragment activity?

I am using ViewPager and loading multiple fragment in Fragment Activity using FragmentPage Adapter. I want to access a fragment which contains an ImageButton. Can any one help me to access Imagebutton from fragment in fragment activity?

Here is my code :

Fragment Activity

       /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_gallery);
        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (CustomPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        mPager.setOnPageChangeListener(this);

       pagerLayout = (LinearLayout) findViewById(R.id.tour_pager_lay);
       mPageControl = (PageControl)findViewById(R.id.horizontal_pager);
       mPageControl.addPagerControl(3);
    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                return new ImageFragment(R.drawable.landing_logo);
            case 1:
                return new ImageFragment(R.drawable.welcome_postit);
            case 2:

                return new LoginFragment();
            case 4:
                return new RegisterFragment();

            default:
                return null;
            }
        }
    }

Upvotes: 0

Views: 332

Answers (1)

Michał Z.
Michał Z.

Reputation: 4119

Suppose that your ImageButton is on your RegisterFragment, then you can do:

RegisterFragment tmp = ((RegisterFragment) ((MyAdapter) mPager.getAdapter()).instantiateItem(mPager, 4));

//your ImageButton should be public or you can create a getter or something, I assume that it's public
tmp.imageButton.setText("foo");

Upvotes: 1

Related Questions