Reputation: 3691
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);
Upvotes: 46
Views: 48615
Reputation: 2104
You can just extend your class with AppCompatActivity
, since AppCompatActivity extends FragmentActivity
internally. Also, ActionBarActivity
is deprecated.
Upvotes: 21
Reputation: 1014
AppCompatActivity
extends FragmentActivity
public class AppCompatActivity extends FragmentActivity implements AppCompatCallback, SupportParentable, DelegateProvider
you can use AppCompatActivity
instead
Upvotes: 5
Reputation: 834
Instead of using setSupportActionBar use setActionBar Eg:
android.widget.Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setActionBar(toolbar);
Upvotes: -3
Reputation: 19
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
.
Upvotes: 1
Reputation: 303
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);
Upvotes: 0
Reputation: 22556
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.
Upvotes: 28
Reputation: 1157
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)
Upvotes: 3
Reputation: 547
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();
}
Upvotes: 9
Reputation:
Use this methods your activity need to extend ActionBarActivity instead of FragmentActivity
toolbar = (Toolbar) findViewById(R.id.search_bar);
setSupportActionBar( toolbar);
Hope it helps
Upvotes: 1