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?
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.
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.
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.
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.
I guess you want to be switching two (or more) Fragment
s 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:
Activity
theme to use an overlaid ActionBar
.Fragment
s 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 Fragment
s where you want the ActionBar
to overlay them, you should just have no padding/margin at the top of their layout xml.Fragment
s, 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....
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 Fragment
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With