0

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?

Community
  • 1
  • 1
Indra C.
  • 31
  • 2
  • 6

2 Answers2

2

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")));

Apar Amin
  • 653
  • 12
  • 22
0

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

Roadblock
  • 2,041
  • 2
  • 24
  • 38
  • I can't use this, because the toolbar already have based on main Toolbar (set in main activity). – Indra C. Mar 24 '15 at 08:22
  • you can always use the getActivity() method and refer to the toolbar that is defined outside. You can also create a global variable to refer to toolbar – Roadblock Mar 24 '15 at 08:39