Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
</style>

Where @style/TransActionbar is:

<!-- 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>
</style>

How I want to apply the theme in my fragment in the onCreateView() method:

@Override
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..

Edit

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

like image 572
Tim Kranen Avatar asked Apr 29 '15 18:04

Tim Kranen


People also ask

What are the themes in fragment?

The theme of Fragments is captured in the opening passage of the novel delivered by Naana: Each thing that goes away returns and nothing in the end is lost. The great friend throws all things apart and brings all things together again. That is the way everything goes and turns round.

When onviewcreated is called?

oncreate view instantiates the view, onviewcreated is called after oncreateview and before saved states are restored... it's more a timing issue in the lifecycle of the fragment. – me_ Oct 23, 2018 at 5:57.

How to set Day and night theme in Android application?

Use the system setting (Settings -> Display -> Theme) to enable Dark theme. Use the Quick Settings tile to switch themes from the notification tray (once enabled). On Pixel devices, selecting the Battery Saver mode enables Dark theme at the same time.

What is fragment in Android?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


1 Answers

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.

like image 148
Matej Avatar answered Oct 11 '22 00:10

Matej