-1

I need my toolbar to be transparant but only in one activity/fragment. I know that this is the way to do it:

toolbar.getBackground().setAlpha(0);

But it changes the toolbar also for the other fragments. Is there an alternative or a way to make it only applicable for one fragment.

This way I need to set the toolbar's alpha back in every activity/fragment.:

toolbar.getBackground().setAlpha(255);

Is this the only way or?

Regards,

Laurenswuyts
  • 2,144
  • 4
  • 21
  • 39

2 Answers2

0

You could use themes:

<style name="my.ToolbarActivity" parent="base">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBarOverlay">false</item>
    <item name="windowActionModeOverlay">false</item>
</style>

<style name="my.ToolbarActivity.Overlay" parent="my.ToolbarActivity">
    <item name="android:windowActionBarOverlay">true</item>
    <item name="windowActionModeOverlay">true</item>
</style>

<style name="my.ToolbarActivity.Overlay.Transparent" parent="my.ToolbarActivity.Overlay">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

Then in your manifest:

 <activity
        android:name=".activity.TransparentActivity"
        android:theme="@style/my.ToolbarActivity.Overlay.Transparent"
        ....
  </activity
  <activity
        android:name=".activity.OverlayActivity"
        android:theme="@style/my.ToolbarActivity.Overlay"
        ...
  </activity>
Cory Roy
  • 5,379
  • 2
  • 28
  • 47
0

If your fragment control flow within an activity is such that you have a standard switch fragment function defined as below, then its pretty obvious -

public void switchfragment(){
     switch(fragment_number){
         case 1:
            hideActionBar() ; // or hideToolbar() as u want
            return new Fragment1();
         case 2:
            showActionBar();  //or showtoolbar() as u want
            return new Fragment2();
}

}

I dont see a case when you would have to go and set it from within indivisual fragments unless you try to switch fragment from within a fragment which I generally avoid.

Manish Jangid
  • 185
  • 1
  • 2
  • 9