Menu icons toolbar on Android 5.0 (Lollipop) - android

I can't load the menu items in the toolbar. I'm using the Drawer Navigation, and I can't even show the hamburger icon.
I'm using getSuportActionBar, my activity extends from ActionBarActivity, I added the toolbar xml into my activity xml.
SOLUTION
I found my solution here, I just added a LinearLayout as a parent in the toolbar
Appcompat Toolbar Not Showing With Navigation Drawer

You can set your favorite icon and add a listener.
mToolbar.setNavigationIcon(R.drawable.ic_drawer);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDrawerLayout.openDrawer(Gravity.START);
}
});

I found my solution here, I just added a LinearLayout as a parent in the toolbar
Appcompat Toolbar Not Showing With Navigation Drawer

Related

Android - When to call method of Activity from Fragment

I have an Activity which has Toolbars. When I move to Fragment A, the Toolbar needs to be changed specifically for Fragment A.
Fragment A contains 4 other Fragments as A is a FragmentPager.
I called the method of the Activity like :
((NewsfeedActivity)getActivity()).changeTitleBesideIcon("Purchases");
((NewsfeedActivity)getActivity()).changeIcon(getResources().getDrawable(R.drawable.purchase));
((NewsfeedActivity)getActivity()).changeBackground(getResources().getColor(R.color.purpleC));
((NewsfeedActivity)getActivity()).hideToolbarBottomMarketplace();
Somehow, the icon doesn't get changed, BUT the title does, along with the color.. I even set the method to setVisibility(View.VISIBLE); on the icon..
Is there a specific state where that kind of method needs to be called? Or am I just doing it wrong?
Update
This is my changeIcon method
public void changeIcon(Drawable imageDrawable){
toolbarImage.setVisibility(View.GONE);
toolbarIcon.setVisibility(View.VISIBLE);
toolbarIcon.setImageDrawable(imageDrawable);
}
toolbarImage is an ImageView located in the Toolbar
This is how it is :
<Toolbar>
<RelativeLayout>
<ImageView> //Toolbar Image
<ImageView>// Toolbar Icon
</RelativeLayout>
</Toolbar>
And I'm calling the methods in the onCreateView()..
try it.
public void changeIconToolbar(int resId){
getSupportActionBar().setHomeAsUpIndicator(resId);
}
it work for me
Well, now I can see that you possibly use Toolbar (rather than ActionBar) and once again I assume that the Toolbar is the toolbar added in API level 21 rather than AppCompat Toolbar.
In order to use this toolbar, I assume that you have called setActionBar (toolbar) to replace the ActionBar with the toolbar from your layout.
NOTE: Without calling this setActionBar (toolbar), your activity will continue using the default ActionBar (in onCreate()) and nothing will be shown.
Now, when the toolbar is in its place. I think your code will work.
PS: I recommend to use AppCompat Toolbar

animateLayoutChanges not animate removing back arrow from Toolbar

I have CoordinatorLayout with AppBarLayout and Toolbar inside it:
CoordinatorLayout
- AppBarLayout
- Toolbar
animateLayoutChanges=true
Nice. And I use this code to hide/show back arrow button:
private void setBackArrowState(boolean state) {
actionBar.setDisplayHomeAsUpEnabled(state);
actionBar.setDisplayShowHomeEnabled(state);
}
There is what I got in result:
When the back arrow button hide, title not animated to it's normal position. How can I solve it?

Getting Toolbar in Fragment

I set up a toolbar in my main activity and when I go inside a fragment, I want to add a slider on it. If I had had the access to the Toolbar object, I would simply do:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Spinner mNavigationSpinner = new SpinnerTrigger(getSupportActionBar().getThemedContext());
toolbar.addView(mNavigationSpinner);
But if I get it using
((ActionBarActivity) getActivity()).getSupportActionBar()
I don't have any addView() method. So my question is, how can I add a view to the Toolbar in fragment if the Toolbar itself was created in an Activity.
I'm not sure if this is the best view of going about this, but I don't think I can have the Spinner in defined in the layout, because most of my fragments don't use it, they simply set a title to the toolbar. But at the same time, it would be great if I could define the toolbar once in the main activity and not redo it for every fragment.
Another way of achieving the same thing from Ellitz answer, inside the fragment access the toolbar (or any other view inside activity) directly:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
you can get it using
Toolbar refTool = ((NameOfClass)getActivity()).toolbar;
or, create an instance of your MainActivity, then, override onAttach(Activity activity) and assign your instance object of MainActivity to the activity in onAttach()
See toolbar main purpose is https://developer.android.com/reference/android/widget/Toolbar.html read here so there are nothing deference in toolbar and actionbar. so if you want to add view to toolbar before it set to Actionbar then toolbar.addView(your view); is fine but after apply apply to setactionbar(toolbar) or setSupportActionbar(toolbar) you can set view to actionbar.
ex. ((ActionBarActivity) getActivity()).getSupportActionBar().setView(Your view)
Thats it...
I would like to add a casting to what Budius said.
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
is the right way of doing it. Because
getActivity().findViewById(R.id.toolbar);
returns a view. This will give you error and you should cast it to Toolbar.

How to implement the Toolbar in the default Navigation Drawer Activity?

I am using Android Studio 1.0.1. I created a new Navigation Drawer Activity that is by default provided by this IDE. It created 2 Java files -
HomePage.javathat extends android.support.v7.app.AndroidBarActivity
NavigationDrawerFragment that extends Fragment
Along with that it provided me with 3 layout files -
activity_home_page.xml
fragment_home_page.xml
fragment_navigation_drawer.xml
Now what I wanted to do was hide the Action Bar and instead get the Toolbar as I wanted to do some detailed designing. For that I took the following steps -
Created a new style in styles.xml
true
false
Changed the theme of the application in AndroidManifest.xml
android:theme="#style/AppTheme"
Created a toolbar layout and included it in activity_home_page.xml
Added a Toolbar object to HomePage.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
But unfortunately, my app force closes whenever I launch HomePage activity. Any suggestions?
Logcat
activity_home_page.xml
HomePage.java
NavigationDrawerFragment.java
custom_toolbar.xml
The problem:
In your fragment you have:
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
...
toolbar.getContext()
However when you call findViewById the activity has not yet called setContentView. That means toolbar was not yet inflated and toolbar is set to null. Therefore toolbar.getContext() throws an NPE.
Fix:
Instead of toolbar.getContext() simply call getActivity(), to get the context.
If your fragment really does need the toolbar, you should call it after your activity's onCreate. A good place for that is onActivityCreated:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
}

New NavigationDrawer on tablets with fixed left layout

I could not find a way to do a tablet multi-pane layout easily with NavigationDrawer. Play Music app does that.
I have used LOCK_MODE_LOCKED_OPENED but it opens the drawer on top of the content as expected and it cannot be closed. Therefore the content is not completely visible.
Do we have to do it manually?
The only way we found is to do it manually, we created a simple linear layout for tablet and check the view instance in activity:
View layout = findViewById(R.id.navigation_layout);
if (layout instanceof DrawerLayout) {
drawerLayout = (DrawerLayout) layout;
// initialization of drawer toggle etc.
...
} else {
// linear layout
getSupportActionBar().setHomeButtonEnabled(false);
}

Categories

Resources