Android - Specify theme of each fragment

Viewed 7569

I try to write an application using one activity-multiple fragment approach. Some pages should has translucent toolbar but not others.

new MyFragment(isTranslucentToolbar)

But theme property belongs to application.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

If I set this property as below, I cannot change theme of the fragment.

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDisablePreview">true</item>
</style>



<style name="AppTheme.Transparent" parent="AppTheme">
    <item name="windowActionBarOverlay">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

If I set the theme as transparent, how do I create a fragment with non-translucent toolbar? I can arrange the margin&padding values of fragment root layout and statusbar bar color programmatically.

Is this any way of this task?

3 Answers

Keep the normal theme with the ActionBar enabled.

When you want to hide/show the ActionBar:

activity.getSupportActionBar().hide(); //for AppCompatActivity
activity.getActionBar().hide(); //for Activity

activity.getSupportActionBar.show(); //for AppCompatActivity
activity.getActionBar().show(); //for Activity

When you want to change the translucency of the status bar:

activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

In your fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   getContext().getTheme().applyStyle(R.style.AppTheme_Transparent, true); 
   View view = inflater.inflate(R.layout.Your_fragment_layout, container, false);
   return view;
}

try setTheme() method in your java file

EDIT 1:

Setting Theme in manifest is usually used for Activity.

If you want to set Theme for Fragment, add next code in the onCreateView() of the Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    // inflate the layout using the cloned inflater, not default inflater
    return localInflater.inflate(R.layout.yourLayout, container, false);
}

Also give a try to this code segment if above doesn't work

View decorView = getWindow().getDecorView();
        // Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
Related