2

I need to change the size of all views on toggle. I did it but the textsize got changed only from 3rd view. The first and second got unchanged. Please suggest to update all the views.

public class MainActivity extends Activity {
    ViewPager viewPager;
    public static ArrayList<NewsItem> newslist; 
    public static PagerAdapter adapter;
    public static ProgressDialog progressDialog;
    LayoutInflater inflater;
    public static float fntSize=30;
    private ToggleButton togButton1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        newslist = new ArrayList<NewsItem>();
        adapter = new ViewPagerAdapter(getApplicationContext(), newslist);
        viewPager = (ViewPager) findViewById(R.id.pager);       
        viewPager.setAdapter(adapter);
        addMainButtonListener();
        loadNews();
    }
      public void addMainButtonListener() {
        togButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
        togButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    fntSize=50;
                } else {
                    fntSize=30;
                }
       }

        });
     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.activity_main, menu);
        return (super.onCreateOptionsMenu(menu));
    }
        private void loadNews(){
        progressDialog= ProgressDialog.show(this,"Progress Dialog Title Text","Process         Description Text");
        News news = new News(getApplicationContext());
        news.execute();
        viewPager.setCurrentItem(0);
    }
}

And My ViewPager class is

public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    ArrayList<NewsItem> newslist = new ArrayList<NewsItem>();
    LayoutInflater inflater;
    public TextView txtNewsDesc;
    public ViewPagerAdapter(Context context, ArrayList<NewsItem> newslist) {
        this.context = context;
        this.newslist = newslist;
    }

    @Override
    public int getCount() {
        return this.newslist.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.news_item, container,
                false);


        txtNewsDesc = (TextView) itemView.findViewById(R.id.news_desc);

        NewsItem news = this.newslist.get(position);


        txtNewsDesc.setTextSize(MainActivity.fntSize);
        txtNewsDesc.setText(news.getDesc());
        // Add viewpager_item.xml to ViewPager
        Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/preeti.ttf");
        txtNewsDesc.setTypeface(typeFace);

        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

I know the first is not changed since it is not reloaded but what about 2nd view? Please help to change the text size of all the views on toggle button click.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
impream
  • 143
  • 1
  • 2
  • 11

3 Answers3

1

The ViewPager creates and retains another page beside the current one and that demonstrates why the second stay the same.

To achieve what you want you need to update the size manually when the toggle button clicked, you can keep a reference to the current view in the ViewPagerAdapter and create a method to return that view as follow

public TextView getCurrentView(){
    return txtNewsDesc;
}

Then in addMainButtonListener method get the current view using the method you have created and update the text size

togButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    {
        if (isChecked) {
            fntSize=50;
        } else {
            fntSize=30;
        }
        ((ViewPagerAdapter)viewPager).getCurrentView().setTextSize(fntSize);
    }
Omar Albelbaisy
  • 827
  • 8
  • 15
  • 1
    Thanks a lot.But though the changes are seen in second page , i still cant see the change in current page. Please suggest me. – impream Jul 21 '14 at 08:30
  • Ok, the second solution is using viewPager.getChildAt(0) then viewPager.getChildAt(1) to get the view of the first and the second page, then you can access the textview and update the text size in both pages – Omar Albelbaisy Jul 21 '14 at 21:13
  • Actually the viewPager.getChildAt(0) and viewPager.getChildAt(1) gives the 1st and 2nd page of the viewPager, but it doesnt give the current and the relatively next view of current page.Did i make any mistake in understanding.If yes then plz suggest. I want all the changes to be encorporated in all the view pages ones the toggle button is clicked. – impream Jul 22 '14 at 05:34
  • `ViewPager` will limit the number of page they will kept depending on the `OffscreenPageLimit` which is 2 by default. So if i'm not mistaken `viewPager.getChildAt(0)` and `viewPager.getChildAt(1)` supposed to return the first and the next page of the current pages of that range which is the current view and the next one – Omar Albelbaisy Jul 22 '14 at 08:37
0

You should use

public int getItemPosition(Object object) {
return POSITION_NONE;

}

use setTag() in instantiateItem() and instead of called notifyDataSetChanged() use findViewWithTag() and update tour text size.

For see this and in this thread https://stackoverflow.com/a/8024557

Community
  • 1
  • 1
Rajnish Mishra
  • 826
  • 5
  • 21
0

i found solution

crerate interface RefreshFragment

public interface RefreshFragment{void refresh();}

in the fragment which is pager item implement RefreshFragment

and at the class where you create the pager adapter do the folowing

circlePageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                ((RefreshFragment) pagerAdapter.getItem(position)).refresh();
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

i hope this is your solution