Set Theme for a Fragment

Theme isn't applying in Fragment

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>);
setSupportActionBar(yourToolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true/false);
actionBar.setDisplayHomeAsUpEnabled(true/false);
actionBar.setTitle(<whatever title you want>);

Otherwise, everything from my original answer still holds.

Changing the activity theme from within the fragment

The issues you are describing are known bugs.

You can only set the theme of the activity before the onCreate for this activity happens

So you have two options :

  1. open a new activity with said fragment, and set its theme in the
    android manifest.

  2. set the theme of the activity inside your fragment, and write logic in oncreate to display your fragment, after you change the
    theme, call activity.recreate() which will make the activity
    relaunch itself and thus applying your theme.

Change Activity Theme From a Fragment

I think I can help you with this. I spent a lot of time over the last few months working on the exact same thing for my app.

The above poster isn't exactly correct. You need to set the theme in onCreate() before views are instantiated -- before setContentView(). When super.onCreate() is called isn't important. I don't see setContentView() in your code above, so I'm wondering if you removed it?

However, if your activity is being themed correctly when you rotate (because it is destroyed and recreated on orientation change) then there's nothing wrong with how you're setting the theme. Instead, I'm inclined to think you're mistaken about onCreate() being called when you exit the SettingsFragment.

You can force your activity to recreate itself like this:

finish();
startActivity(getIntent());

Please first try setting a breakpoint in your activity's onCreate() method and confirm whether it's hit on exiting your fragment (I bet it's not.) Then try the code above.



Related Topics



Leave a reply



Submit