Make the transparent actionbar in fragment - java

I've been searching on how to make transparent actionbar in fragment and no answer so far. So, the scenario is, I create an app using Navigation Drawer, each menu refer to same actionbar in MainActivity.
But I can't customize actionbar in transparent mode, like this Transparent Actionbar: custom tabcolor. In this:
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#550000ff")));
that code had to before setContentView, and as you know on the fragment we use onCreateView for the layout.
How can I achieve the transparent actionbar in fragment?

if you just want to make transparent Actionbar then you can do it by this way in Fragment's onCreateView
getActivity().getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33000000")));

Use a Toolbar and use the following code to let it behave like Action bar
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
To include toolbar in your layout use the following.
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />
It is a lot easy to change transparency of Toolbar as compared to dealing with ActionBar
Once set you can deal with toolbar as any other view i.e. changing transparency/color dynamically is a lot easy for Toolbar

Related

How to dynamically change Toolbar from activity inside ViewPager (TabLayout)

I'm struggling with Toolbar Button and ViewPager with TabLayout inside my app. I have multiple Toolbar Buttons, that I want to change dynamically (Add or remove icon/button on Toolbar, and set different onClickListener for each Fragment) inside Fragment loaded inside TabLayout. The main problem here is that ViewPager load 2 fragments in a row in order to provide the scroll animation, but there is no method in order to know if the fragment is really displayed or not... (Except setUserVisibleHint, but this method is deprecated, and doesn't seems to be really reliable), so does someone have a solution in order for my app to run great with that behaviour? (Changing dynamically the icon on my Toolbar inside the Fragment)
Thanks!
Try implementing the logic inside the onResume of your Fragment. onResume basically means that this fragment is visible for the user. This should solve your problem. Hope it helps :)

How to show Hamburger Icon, when toolbar icon is enabled

For my App i need to show icon in toolbar. So I referred a question on stackoverflow
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
I applied the same to my code but after running the app i noticed my navigational drawer icon i.e. hamburger gone missing.
How can i get it back along with the app icon?
Remember, ToolBar is the more customizable version of ActionBar, and are two different things, though they basically follow the same pattern.
My answer is to user Toolbar and customize it for the required view instead of using the default ActionBar.
If you are using the customized ToolBar, then the below code is not going to work.
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
Use ToolBar.
Change from this
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
To
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_launcher);

Add custom toolcontrol to toolbar E4

Currently my custom toolcontrols are outside the toolbars because of the scaling issue in E4. Toolbar does not scale correctly in E4
Now my client want to have different toolbars for all the toolcontrols, so that the user can rearange the order of the toolbar and separate the toolcontrols from each others. How is this possible in E4? I know the toolcontrols inside the toolbar is not scaling the toolbar itself. But is there any workaround to place a toolcontrol inside a toolbar?
I added ahidden icon to the toolbar when adding the custom toolcontrol to it. Eclipse scales the toolbar correct know.

ActionBar animation issues

I am using the ObservableScrollView library to show and hide the ActionBar as you are scrolling up and down an activity. This works well but when the ActionBar is animating sliding in or out a blank area at the bottom of the screen about the size of the ActionBar is briefly pushed up.
After searching similar issues the suggested solution is to the following line to onCreate():
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
This has had no visible effect and the issue is still present.
Solved by setting my ActionBar to overlay content in the XML style.

How to hide actionbar and show tabs

Many suggested that I should use:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
and it shows me an actionbar with no title but with the application logo.
It was also suggested that I hide the logo and, when I did, the actionbar appeared empty and didn't hide.
duplicate question , see :
Android: Hide ActionBar, keep Tabs , Only display tabs in action bar
Android: How do you hide Tabs in the ActionBar?
You can hide the action bar at runtime by calling hide(). For example:
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
Full documentation

Categories