Theme isn't applying in 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:
- Set the
Activity
theme to use an overlaidActionBar
. - For those
Fragment
s where you don't want theActionBar
to be overlaid, in their layout xml, set a top padding/margin equal to theActionBar
height. Ideally you would just reference the dimension resource for action bar height. For thoseFragment
s where you want theActionBar
to overlay them, you should just have no padding/margin at the top of their layout xml. - When you switch
Fragment
s, change the background color of theActionBar
by callinggetActionBar()
(orgetSupportActionBar
if you are using the support library) and then.setBackgroundColor(Color.TRANSPARENT)
or whatever color you need from the resources usinggetResources().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.
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 :
open a new activity with said fragment, and set its theme in the
android manifest.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
Android Studio's Project Gradle File Changed
How to Implement Android Pull-To-Refresh
How to Keep Onitemselected from Firing Off on a Newly Instantiated Spinner
Clear the Entire History Stack and Start a New Activity on Android
How to Crop Circular Area from Bitmap in Android
How to Get the Actionbar Height
List of Android Permissions Normal Permissions and Dangerous Permissions in API 23
How to Customize Snackbar's Layout
Android Open and Save Files To/From Google Drive Sdk
How to Fire Onlistitemclick in Listactivity with Buttons in List
Gradle: Execution Failed for Task ':Processdebugmanifest'
How to Add Stacktrace or Debug Option When Building Android Studio Project
How Are Android Touch Events Delivered
How to Run a Method Every X Seconds
Constraintlayout: Change Constraints Programmatically
Android: Detect When Scrollview Stops Scrolling
Error:Execution Failed for Task ':App:Transformclasseswithdexfordebug'