Tim Kranen Tim Kranen - 1 year ago 99
Android Question

Theme isn't applying in Fragment

I am trying to apply a specific theme on a Fragment. But for some reason it isn't happening. Can anyone point out mistakes in my code? Or better solutions?

Theme in styles.xml:

<!-- Theme for trans actionbar -->
<style name="TransTheme" parent="@style/AppTheme">
<item name="android:actionBarStyle">@style/TransActionbar</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="actionBarStyle">@style/TransActionbar</item>
<item name="windowActionBarOverlay">true</item>


<!-- Actionbar style for trans theme -->
<style name="TransActionbar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/first_trans_actionbar</item>
<item name="background">@color/first_trans_actionbar</item>

How I want to apply the theme in my fragment in the

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//set theme
final Context contextWrapper = new ContextThemeWrapper(getActivity(), R.style.TransTheme);
LayoutInflater localInflater = inflater.cloneInContext(contextWrapper);
contentView = localInflater.inflate(R.layout.fragment_movie_detail,
container, false);
return contentView:

For some reason the Fragment keeps the old theme..


So basically the question comes down to: How do you make an Actionbar transparent (and overlay mode) in just one fragment of an activity?

Answer Source

I guess you want to be switching two (or more) Fragments within the same Activity, where each of them could require the ActionBar to either be overlaid or not, and have different backgrounds depending on the current Fragment.

Let's get the most important thing out of the way: as of yet, you can't change an Activity's theme at runtime.

You can still relatively easily accomplish what you are after by doing the following:

  1. Set the Activity theme to use an overlaid ActionBar.
  2. For those Fragments where you don't want the ActionBar to be overlaid, in their layout xml, set a top padding/margin equal to the ActionBar height. Ideally you would just reference the dimension resource for action bar height. For those Fragments where you want the ActionBar to overlay them, you should just have no padding/margin at the top of their layout xml.
  3. When you switch Fragments, change the background color of the ActionBar by calling getActionBar() (or getSupportActionBar if you are using the support library) and then .setBackgroundColor(Color.TRANSPARENT) or whatever color you need from the resources using getResources().getColor(R.colors.some_color). You could go for something more fancy than an abrupt switch, by animating the color change, but that's beyond the scope of this question.
  4. ...

  5. Profit!

EDIT: With Toolbar

If you want to use a Toolbar, the way to do it would be to put it in a separate layout xml that you <include> inside your Activity layout and give it some ID that you can reference through code. Make sure the Activity root layout is something with Z ordering, like RelativeLayout or FrameLayout, and position the toolbar <include> at the top of the Y axis (alignParentTop for RelativeLayout or layout_gravity="top" for FrameLayout), and put the Toolbar <include> after whatever layout you will be putting your Fragments inside, so that it will overlay them.

Inside the Activity onCreate do this:

Toolbar yourToolbar = (Toolbar) findViewById(R.id.your_toolbar);
yourToolbar.setBackgroundColor(<whatever color you want>);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(<whatever title you want>);

Otherwise, everything from my original answer still holds.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download