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
Related
I know (and use it) how to add a custom view to a Toolbar (as ActionBar). However my requirement now is to display that custom view below the title. Is there a way to achieve this without having to also include the title into that custom view? I prefer to leave the title management to the toolbar itself.
I understand the wrong thing !
You can try
View custom = getLayoutInflater().inflate(R.layout.custom,toolbar,true);
custom.setLayoutParams();
I'm going to bed.
you also can use the appbar ,
<AppBarLayout>
<Toolbar/>
<CustonView/>
<AppBarLayout/>
check the official documentation
http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html
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.
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);
}
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
I have a navigation bar in my app, the thing is that I want the navbar to be available in all activities. I suppose that I have to set contentView two times, but that doesn't work, of course.
I've been looking at and but I dont get it to work. I have a super class, can I set this second layout from my super class?
You should include the nav bar via <include> tag from the other layouts. Setting the content layout twice will not work, as Android is in the callbacks basically always using what the user has told last. So
setContentLayout(R.layout.nav);
setContentLayout(R.layout.main);
will result in only the main layout being used.
Have a look at this article which gives an example of using the include tag.
You can extend standard activities (Activity, ListActivity, etc.. if you use any others) and use them as a base for including a nav_bar.
For example:
Define a layout with nabar like this
<LinearLayout
...
android:orientation="vertical"
>
<YourNavBarComponent
...
/>
<FrameLayout
android:id="#+id/nav_content"
...
>
// Leave this empty for activity content
</FrameLayout>
</LinearLayout>
This will be your base layout to contain all other layouts in the nav_content frame.
Next, in create a base activity class, and do the following:
public abstract class NavActivity extends Activity {
protected LinearLayout fullLayout;
protected FrameLayout navContent;
#Override
public void setContentView(final int layoutResID) {
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.nav_layout, null); // Your base layout here
navContent= (FrameLayout) fullLayout.findViewById(R.id.nav_content);
getLayoutInflater().inflate(layoutResID, navContent, true); // Setting the content of layout your provided in the nav_content frame
setContentView(fullLayout);
// here you can get your navigation buttons and define how they should behave and what must they do, so you won't be needing to repeat it in every activity class
}
}
And now, when you create a new activity, where you need a nav bar, just extend NavActivity instead. And your nav bar will be placed where you need it, without repeating it in every layout over and over again, and polluting the layouts (not to mention repeating a code to control navigation in every activity class).
Try merging layouts, as described on Android Developers Blog.