简体   繁体   中英

How to change TextSize from a different activity

I am displaying text using viewpager. I want to change the textsize of the text displayed. The problem i am having is that the button is in class which extends Activity which sets adapter to viewpager and the Textview for which i want to change the size is in different class which extends PagerAdapter.

This is the code i have tried. My main class:

public class NewsDisplay extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_pager);

.
.
.
.
 ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);

 adapter = new NewsSlider(NewsDisplay.this, newsList);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);
   }

}

This is options menu from where user clicks to change textsize:

public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
         MenuInflater menuInflater = getMenuInflater();
         menuInflater.inflate(R.menu.menu, menu);
         return true;
   }  

This is where i handle click event:

public boolean onOptionsItemSelected(MenuItem item)
    {

       switch (item.getItemId())
        {
        case R.id.menu_textSize:
            NewsSlider.news_content.setTextSize(25);
            return true;

        }
    } 

And this is my class which extends PagerAdapter:

public class NewsSlider extends  PagerAdapter {

Context context;

 LayoutInflater inflater;
 private ArrayList<HashMap<String, Object>> newsListArray;
 public static TextView news_content;  // This is the textview size i want to change

public NewsSlider(Context context,ArrayList<HashMap<String, Object>> d)  {

    newsListArray = d;
    inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
.
.
.
public Object instantiateItem(ViewGroup container, int position) {

news_content = (TextView) itemview.findViewById(R.id.news_description);
Spanned s = Html.fromHtml(newsinfo.get("news_content").toString());

    news_content.setText(s);
}

When i am clicking textsize menu, the size is not changing. Itried creating options menyu in pageradapter but its giving me error. Please help me to solve this issue...

The first problem is keeping a static reference to anything in your UI.

public static TextView news_content;

Setting that will make sure your whole view and Activity, and anything associated with it, will stay in memory until your whole app process is shut down. This falls under the category of things you CAN do, but never SHOULD do.

Second problem, related to this specifically, is that its a ViewPager. It'll inflate multiple views, assuming you have multiple pages, so the last TextView inflated will be the one you're calling. By design, ViewPager will create the view before and after the one you're currently viewing, so you're pretty much guaranteed to never see this work visually, except MAYBE if you scroll to the last view, and then hit the menu.

What I would do is post some kind of message to a bus when you click the menu, and have your instantiated pages listening for that message. You can use LocalBroadcastManager, but I'd probably use greenrobot. In any case, register for events in instantiateItem, and unregister in destroyItem.

Another thing you might need to consider is if the html includes some kind of text size info, in which case, it might override your settings, although I'm not sure how that works in the Html.fromHtml case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM