I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my activity.In case of a custom action bar ((ActionBarActivity)getActivity).setcustomView(..) would have done the job.But due to use of this ToolBar I am not able to use that.Also I have implemented a method in my BaseActivity that is inherited by all Activities.This BaseActivity contains my method to initialize a sliding drawer to the left.I have to initialize the initDrawerLayout() method in activity else the drawer would not be initialized.And if I initialize it in fragment its giving me all empty results,neither the toggle button for drawer and nor is the custom title getting set.
This is my initDrawer code..
public void initDrawerLayout(String toolbar_text) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerRelative = (RelativeLayout) findViewById(R.id.drawer_relative);
if (mDrawerLayout != null) {
findViewById(R.id.drawer_btn_a).setOnClickListener(this);
findViewById(R.id.drawer_btn_b).setOnClickListener(this);
findViewById(R.id.drawer_btn_c).setOnClickListener(this);
findViewById(R.id.drawer_btn_d).setOnClickListener(this);
findViewById(R.id.drawer_btn_e).setOnClickListener(this);
findViewById(R.id.drawer_btn_f).setOnClickListener(this);
findViewById(R.id.drawer_btn_g).setOnClickListener(this);
findViewById(R.id.drawer_btn_h).setOnClickListener(this);
findViewById(R.id.drawer_btn_i).setOnClickListener(this);
findViewById(R.id.drawer_btn_j).setOnClickListener(this);
findViewById(R.id.drawer_btn_k).setOnClickListener(this);
findViewById(R.id.drawer_btn_l).setOnClickListener(this);
findViewById(R.id.my_layout).setOnClickListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setBackground(getResources().getDrawable(R.drawable.icn_actionbar_background));
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(toolbar_text);
mTitle.setTypeface(Typeface.DEFAULT_BOLD);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
toolbar.setNavigationIcon(R.drawable.ic_drawer);
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar,
R.string.drawer_open, R.string.drawer_close
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
} else {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
});
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mDrawerLayout.setScrimColor(getResources().getColor(
android.R.color.transparent));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
And this my code in the fragment..
((FirstActivity) getActivity()).initDrawerLayout(mFirst.name);
where mFirst is a object of class Person
and the toolbar code..
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Toolbar Title"
android:textColor="#color/action_text-color"
android:textSize="18sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
Please help guys..
I do this like this:
from the fragment call
getActivity().setTitle("your title");
But where you call it is important, because if each Fragment has it's own title, then one may override the other accidentally, which may be prevented like:
#Override
public void onResume() {
super.onResume();
Activity activity = getActivity();
if (activity != null) {
activity.setTitle(getString(R.string.my_title));
}
}
For example, two Fragments of the same Pager can not be in resume-state at the same time.
Also you can "cast", to call any function of your parent Activity like this:
YourActivity mYourActiviy = (YourActivity) getActivity();
mYourActivity.yourActivityFunction(yourParameters);
In Kotlin.
In fragment:
(activity as YourActivity).supportActionBar?.title = getString(R.string.your_title)
In activity:
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
If you have setSupportActionBar in Your Activity then you can easily change the toolbar title from your fragment
((YourActivity) getActivity()).getSupportActionBar().setTitle("Your Title");
To allow a Fragment to communicate up to its Activity (to set your Toolbar Title), you can define an interface in the Fragment class and implement it within the Activity as described here: Communicating with Other Fragments.
The answer is writen below in the oncreateview method of fragments.
getActivity().setTitle("your name");
You can create Interface inside the Fragment. check below:-
public class MyFragment extends Fragment {
OnMyFragmentListener mListener;
// Where is this method called??
public void setOnMyFragmentListener(OnMyFragmentListener listener) {
this.mListener = listener;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnMyFragmentListener) {
mListener = (OnMyFragmentListener) context;
mListener.onChangeToolbarTitle("My Fragment"); // Call this in `onResume()`
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
#Override
public void onResume(){
super.onResume();
mListener.onChangeToolbarTitle("My Fragment");
}
// This interface can be implemented by the Activity, parent Fragment,
// or a separate test implementation.
public interface OnMyFragmentListener {
public void onChangeToolbarTitle(String title);
}
}
In Activity:-
public class MyActivity extends AppCompatActivity implements MyFragment.OnMyFragmentListener {
#Override
public void onChangeToolbarTitle(String title){
toolbar.setTitle(title);
}
}
This works for me. :)
This has worked for me, in Kotlin. Put this in your fragment class:
if (activity != null) {
(activity as MainActivity).supportActionBar?.title = getString(R.string.action_history)
}
If somebody struggles with this problem, this may be useful.
Basically you have 4 options, how to handle that:
use an interface in order to communicate with your activity, or any other convenient method, like an event bus.
you call getActivity().setTitle("Title"), but in this case you need to attach your Toolbar to the ActionBar by calling the setSupportActionBar() in your activity.
You can have a public instance of your Toolbar and access that instance from the fragment.
Finally, if you need the instance of your Toolbar(you may want to do something else with), you can simply get it this way:
Toolbar bar=Toolbar.class.cast(getActivity().findViewById(R.id.toolbar));
Well, the last option would solve the problem only if the Toolbar hasn't been passed to the setSupportActionBar method.
If it has been, then you need to call this method in your activity:
supportActionBar.setDisplayShowTitleEnabled(false),
which will solve the problem.
However, I would suggest to use ButterKnife which will make it a little bit cleaner, here an example:
Toolbar actionBar=findById(getActivity(),R.id.actionBar);
actionBar.setTitle("Title");
In Kotlin, I use
fun onAttach(...){
..
activity?.title = "My Title"
}
This work for me :
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.id_toolbar);
toolbar.setTitle("New Title");
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
FragmentManager.BackStackEntry lastBackStackEntry=null;
int lastBackStackEntryCount = getSupportFragmentManager().getBackStackEntryCount() - 1;
if(lastBackStackEntryCount >= 0 )
{
lastBackStackEntry = getSupportFragmentManager().getBackStackEntryAt(lastBackStackEntryCount);
}
Toast.makeText(MainActivity.this, ""+lastBackStackEntryCount, Toast.LENGTH_SHORT).show();
if(lastBackStackEntryCount == -1)
{
toolbar.setTitle("");
toolbar.setLogo(R.drawable.header_logo);
}
else
{
toolbar.setTitle(lastBackStackEntry.getName());
}
}
});
For me the problem was that for some reason the label was overwritten. I had to change it back to the string resource, in
navigation.xml
inside the fragment tag;
android:label="#string/android_trivia"
You can change the title of your toolbar on the event OnAttach, something like this
var toolbar = activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.Title = "New Title";
In your Activity declare the toolbar as public.
public class YourActivity : Activity
{
public Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = .... // initilize your toolbar
}
}
Then, from your fragment
((YourActivity) getActivity()).toolbar.Title = "Your Title";
If you are using a custom toolbar, this will help you:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("Feedback");
You need to set the title in activity commiting the fragment and return the fragment
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment, mainFragment).commit();
toolbar.setTitle("ShivShambhu");
return contentFragment;
This works for me.
xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="55dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:background="#color/tool_bar_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Find TextView Id from toolbar
if you are using Activity
TextView mTitle = findViewById(R.id.toolbar_title);
mTitle.setText("set your title");
if you are using Fragment
TextView mTitle = view.findViewById(R.id.toolbar_title);
mTitle.setText("set your title");
Here's one simple solution.
In your activity that extends AppCompatActivity, you can do this:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The inside its fragment, you can do this:
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
Related
I'm using android.support.v7.widget.Toolbar and AppCompatActivity. I've enabled back up button like this supportActionBar.setDisplayHomeAsUpEnabled(true);.
The fragment in side the activity will set the title and subtitle in onResume() like this
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setTitle(title);
activity.getSupportActionBar().setSubtitle("Bingo");
The problem is, when the fragment shows up onResume is called but subtitle is not shown. When I press power OFF and ON, means fragment goes to pause and resumes again. Now, subtitle is visible. I've tested on other android phone as well.
Can you please help me finding out the problem?
This is because the toolbar is not rendered when you are setting the subtitle.
Try this code,Set title and subtitle inside this method
private void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
if(toolbar != null){
setSupportActionBar(toolbar);
}
toolbar.post(new Runnable()
{
#Override
public void run()
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(mTitle);
getSupportActionBar().setSubtitle("Subtitle);
}
});
}
I want to make on my MainActivity a property navigation for my users with hamburger and back arrow.
When I have only one fragment set hamburger, but if i have more fragments added on my MainActivity, set the back arrow.
How do I implement that?
Here is my implementation...
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(toolbar);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
//TODO: Insert back arrow button if have more than one fragment on backstack
/*getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
int stackHeight = getSupportFragmentManager().getBackStackEntryCount();
if (stackHeight > 0) {
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toggle.setDrawerIndicatorEnabled(false);
}
} else {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(true);
}
}
}
});*/
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), new ListVeiculoFragment(), R.id.container_main);
}
}
Work by creating Interface class:
public interface HideShowIconInterface{
void showHamburgerIcon();
void showBackIcon();
}
Implement Interface in your Activity:
public class YourActivity extends AppCompatActivity implements HideShowIconInterface{
#Override
public void showHamburgerIcon() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
#Override
public void showBackIcon() {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
In your Fragment, call whatever you want by:
((HideShowIconInterface) getActivity()).showHamburgerIcon();
or
((HideShowIconInterface) getActivity()).showBackIcon();
You have to catch the moment when the numbers of the fragments change and use this code to hide (false) / show (true) the DrawerToggle "Hamburger"
mDrawerToggle.setDrawerIndicatorEnabled(false);
EDIT:
In your Activity (somewhere) you have something like this (where you change current Fragment):
private void selectItem(int position) {
Fragment fragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
}
I would change last line by this:
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
and then check:
if(fragmentManager.getBackStackEntryCount() > 1) mDrawerToggle.setDrawerIndicatorEnabled(false);
setSupportActionBar(toolbar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
put this code in your activity where you use humbarger style navigation it work for me.
Actually none of above solutions works fully.
There are couple of issues that have to be considered -
a) if using ActionBarDrawerToggle then getSupportActionBar().setDisplayHomeAsUpEnabled is now working as expected - it actually replaces icon for toggle, but clicks are still handled by toggle plus hamburger icon is hidden when toggle is enabled again
b) As fragments transaction (add, restore form backstack) are done asynchronously, check for actual backstack size has to be done after fragment transaction is done - e.g. from fragments onCreateView
Here is code that works for me (in Kotlin):
override fun onCreate(savedInstanceState: Bundle?) {
// ....
drawerToggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(drawerToggle)
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_arrow_back_white)
drawerToggle.syncState()
drawerToggle.setToolbarNavigationClickListener {
// whatever action is needed on homeAsUp click
onBackPressed()
}
//And this method should be called from fragment's onCreateView
fun showUpNavigation() {
drawerToggle.isDrawerIndicatorEnabled=supportFragmentManager.getBackStackEntryCount() <= 1
}
You just add
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
For more details see Android - Switch ActionBar Back Button to Navigation Button.
I have MainActivity which is holding 2 fragments. First fragment has navigation drawer, the second fragment not. In the MainActivity i want to override onBackPressed() method so when the navigation drawer is opened, it has to close the fragment. But i don't know how to get fragment's navigation drawer's state from Activity. Here's the code :
MainActivity.java
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
FragmentListProduct.java
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.fragment_list_product_layout, container, false);
Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar);
fragmentActivity.setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) mainView.findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(fragmentActivity, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
navigationView = (NavigationView) mainView.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.nav_cat_all);
}
Of course it gives me error Cannot resolve symbol darwerlayout.
Before i made drawerLayout as a static field, but i got warning Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run). So how to solve this problem, or maybe you have another solution :D
You can access the fragment using the tag, which you'll have to assign when invoking the fragment, like this:
getFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment, "the_tag_here")
.commit();
Access it using getFragmentManager().findFragmentByTag("the_tag")
#Override
public void onBackPressed() {
FragmentListProduct fragment = (FragmentListProduct) getFragmentManager().findFragmentByTag("the_tag_here");
if (fragment != null) {
DrawerLayout drawerLayout = fragment.drawerLayout;
//make drawerLayout public in the fragment
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
Solution 1:
Put your navigation drawer in your main activity.
open the second fragment in the new activity if you do not want navigation drawer there.
Solution 2:
Create a method in your fragment say checkNavigationDrawer().
Get a fragment reference in your activity like this
Fragment yourFragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.containerId);
In your onBackPressed () call yourFragment.checkNavigationDrawer(); like this
#Override
public void onBackPressed() {
yourFragment.checkNavigationDrawer();
super.onBackPressed();
}
In your checkNavigationDrawer method check if the navigation drawer is open or not if it is open close it.
public void checkNavigationDrawer(){
if(drawerLayout.isOpen()){
drawerLayout.closeDrawers();
}
}
Hold your FragmentListProduct object in your MainActivity.
Create one method called isDrawerOpen() in the FragmentListProduct fragment.
Now you can call the isDrawerOpen() from MainActivity by using the FragmentListProduct class object.
Go for it!
In my app, I have back button as default on toolbar after the splash screen.(i.e) on home page. I don't need any icon or title on my home page toolbar alone.
How can I hide the button on home screen?
If I disable back button on MainActivity by
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
It shows NullPointerException
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
HomeFragment homeFragment = new HomeFragment();
showFragment(homeFragment, Constants.HOME_FRAGMENT, false);
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
showAlertDialog(Constants.EXIT_APP, Constants.APP_NAME);
} else {
getFragmentManager().popBackStackImmediate();
}
}
public void showFragment(Fragment fragment, String back_stack_name, boolean isAddToBackStack) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.container, fragment);
if (isAddToBackStack) {
transaction.addToBackStack(back_stack_name);
}
transaction.commit();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int vid = item.getItemId();
switch (vid) {
case android.R.id.home:
this.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I found the answer for my question.
In fragments:
For title without back arrow
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
For Title with back arrow
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
In activity:
For title without back arrow
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setTitle(title);
For Title with back arrow
getSupportActionBar().setDisplayShowCustomEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle(title);
For colored back button, modify in style
<style name="ToolbarColoredBackArrow" parent="AppTheme">
<item name="android:textColorSecondary">#ffffff</item> <!-- white arrow -->
</style>
Use theme in toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:theme="#style/ToolbarColoredBackArrow"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
Possible reason:
1) you dont have the support actionbar.
2) you might be hide the supportActionBar using styles.
3) you are hiding it programatically in your activity.
solution:
If you are using toolbar in activty , try using this below code in your onCreate() method:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
I made a ListActivity:
public class NoteListActivity extends ListActivity{
}
and the following methods are no longer available:
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
But I can use:
setActionBar(toolbar);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
Note that I used Support library in other activities in the same project. It means I have the correct gradle dependency added.
How do I use ListActivity and android.support.v7.widget.Toolbar ?
You can't do that in ListActivity.
If you want to access getSupportActionBar(), you need to extend your class with AppCompatActivity.
My Suggestion : Don't use ListActivty as you want to use ToolBar. Create an Activity and then only have ListView within that Activity. It'll work just fine.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatCallback callback = new AppCompatCallback() {
#Override
public void onSupportActionModeStarted(ActionMode actionMode) {
}
#Override
public void onSupportActionModeFinished(ActionMode actionMode) {
}
#Nullable
#Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
};
AppCompatDelegate delegate = AppCompatDelegate.create(this, callback);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.saved_report_activity);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
delegate.setSupportActionBar(toolbar);
delegate.getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(SavedReportActivity.this);
}
});
AppCompatActivity is the direct child class of FragmentActivity of support v4 .Read this article
http://android-developers.blogspot.it/2015/04/android-support-library-221.html
so for your requirement, public class NoteListActivity extends AppCompatActivity {...}
Just change your dependency,Like :
compile "com.android.support:appcompat-v7:22.1.0"
I was able to solve this by adding a support library toolbar style and then adding the style to my list activity in the manifest.
In styles.xml:
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
In Manifest
<activity android:theme="#style/FullscreenActionBarStyle"
android:name="#string/my_activity_name"
android:label="#string/my_activity_label"/>
This should add a toolbar to your activity that you can add items to and process events with, etc...