简体   繁体   中英

Access Actionbar TextView from another fragement

Hi im trying to access a Text View from the Action Bar, for example when a button is pressed I would like the Text View in the action bar to change. is this possible ?

the following is my code MainActivity with actionbar

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
preferences = PreferenceManager.getDefaultSharedPreferences(this);
new URLAsyncCaller().execute();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
FragmentManager fragmentManager = getSupportFragmentManager();
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_layout);
LinearLayout search = (LinearLayout) actionBar.getCustomView().findViewById  (R.id.btn_chart);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("lastid", 1); // also puts the data into a shared
editor.commit();
TextView toptitle= (TextView)search.findViewById(R.id.tv_avtiontext);
toptitle.setText(GWave.getActiontext());`

Fragement B Code

GWave.setActiontext("Welcome To Z-Wave");

I have tried creating a method in the Main Activity to handle the text change but couldn't get it working. Any suggestions would be gratefully appreciated .. Thank you.

Assuming that the TextView in your ActionBar has an id action_text ,
in the Fragment , do this:

TextView actionText = (TextView) getActivity().findViewById(R.id.action_text);  

Et Voilà! You are done! :)

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