46

How do I access setSupportActionBar(Toolbar toolbar) inside FragmentActivity? I can't access it inside FragmentActivity

toolbar = (Toolbar) findViewById(R.id.search_bar);
setSupportActionBar( toolbar);
katwal-Dipak
  • 3,523
  • 2
  • 23
  • 23

9 Answers9

28

With the latest version of the support library you should make your Activity extend AppCompatActivity as ActionBarActivity has been deprecated.

It provides the same functionality as your ActionBarActivity previously did. You shouldn't need to make any further changes.

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
21

You can just extend your class with AppCompatActivity, since AppCompatActivity extends FragmentActivity internally. Also, ActionBarActivity is deprecated.

Seto
  • 1,234
  • 1
  • 17
  • 33
zero
  • 2,054
  • 2
  • 14
  • 23
9

Use ActionBarActivity from support library, ActionBarActivity extends FragmentActivity, So that you can get SupportFragmentManager and set toolbar as actionbar

Ex:

public class MainActivity extends ActionBarActivity
{

 Toolbar toolbar = (Toolbar) findViewById(R.id.search_bar);
 setSupportActionBar( toolbar);

 FragmentManager manager=this.getSupportFragmentManager();
}
Viral Thakker
  • 547
  • 2
  • 10
5

AppCompatActivity extends FragmentActivity

public class AppCompatActivity extends FragmentActivity implements AppCompatCallback, SupportParentable, DelegateProvider

you can use AppCompatActivity instead

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
mhKarami
  • 844
  • 11
  • 16
3

If your class extends FragmentActivity

and if the toolbar is inside the layout you used, it will be set by default. To access it simply do

(Toolbar) findViewById(R.id.toolbar)

Udo
  • 1,129
  • 3
  • 15
  • 30
1

Use this methods your activity need to extend ActionBarActivity instead of FragmentActivity

toolbar = (Toolbar) findViewById(R.id.search_bar);
setSupportActionBar( toolbar);

Hope it helps

1

If you want your ViewPager to add fragments, you can extend AppCompatActivity, it also works. (Often, we extend FragmentActivity, but after that setSupportActionBar( toolbar) doesn't work) So, we can extend AppCompatActivity instead of extending FragmentActivity.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
lily.leung
  • 19
  • 1
0

For FragmenrtActivity, you should look into FragmentTabHost, and to add tabs simply:

tab = (FragmentTabHost)findViewById(android.R.id.tabhost);
tab.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
tab.addTab(tabs.newTabSpec("tab1").setIndicator("TAB1"), tab1.class, null);
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
DaveDave
  • 293
  • 1
  • 2
  • 10
-3

Instead of using setSupportActionBar use setActionBar Eg:

android.widget.Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);    
setActionBar(toolbar);
JibinNajeeb
  • 784
  • 1
  • 10
  • 31