Set theme for snippet

I am trying to set a theme for a snippet.

Setting the theme in the manifest does not work:

android:theme="@android:style/Theme.Holo.Light" 

From a look at previous blogs it seems that I should use ContextThemeWrapper. Can anyone reference a coded example? I canโ€™t find anything.

+97
android android-fragments android-theme
Feb 27 '12 at 17:13
source share
12 answers

Customizing a theme in a manifest is usually used for Activity.

If you want to install Theme for Fragment, add the following code to the onCreateView () fragment:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // create ContextThemeWrapper from the original Activity Context with the custom theme final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme); // clone the inflater using the ContextThemeWrapper LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); // inflate the layout using the cloned inflater, not default inflater return localInflater.inflate(R.layout.yourLayout, container, false); } 
+163
Mar 19 '13 at 10:04 on
source share

The fragment takes its theme from its activities. Each fragment is assigned a theme for the action in which it exists.

The theme is used in the Fragment.onCreateView method, where your code creates views that are actually objects that use the theme.

In Fragment.onCreateView you get the LayoutInflater parameter, which inflates the views and contains the context used for the theme, in fact it is an Activity. So your bloated views use the theme of activity.

To override a theme, you can call LayoutInflater.cloneInContext , which mentions in Docs that it can be used to change the theme. You can use ContextThemeWrapper here. Then use the cloned inflater to create fragments.

+11
Mar 28 '13 at 8:35
source share

I also tried to open a dialogue of my fragment with another topic in my activity and follow this decision . Like some of the people mentioned in the comments, I did not get him to work, and the dialogue continued to show the topic indicated in the manifest. The problem was that I built the dialog using AlertDialog.Builder in the AlertDialog.Builder method and therefore did not use the onCreateView method, as shown in the answer I linked to. And when I instantiated AlertDialog.Builder , I walked in context using getActivity() when I had to use the ConstextThemeWrapper instance instead.

Here is the code of my onCreateDialog:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Create ContextThemeWrapper from the original Activity Context ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getActivity(), android.R.style.Theme_DeviceDefault_Light_Dialog); LayoutInflater inflater = getActivity().getLayoutInflater().cloneInContext(contextThemeWrapper); // Now take note of the parameter passed into AlertDialog.Builder constructor AlertDialog.Builder builder = new AlertDialog.Builder(contextThemeWrapper); View view = inflater.inflate(R.layout.set_server_dialog, null); mEditText = (EditText) view.findViewById(R.id.txt_server); mEditText.requestFocus(); // Show soft keyboard automatically mEditText.setOnEditorActionListener(this); builder.setView(view); builder.setTitle(R.string.server_dialog); builder.setPositiveButton(android.R.string.ok, this); Dialog dialog = builder.create(); dialog.setCanceledOnTouchOutside(false); return dialog; } 

I originally created AlertDialog.Builder as follows:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

which I changed to:

 AlertDialog.Builder builder = new AlertDialog.Builder(contextThemeWrapper); 

After this change, the fragment dialog was displayed with the correct theme. Therefore, if someone has a similar problem and uses AlertDialog.Builder , then check the context passed to the builder. Hope this helps! :)

+11
Jul 18 '14 at 23:24
source share

Make sure android:minSdkVersion="11" set in the manifest. This may be the reason David's example did not work for you.

Also set the android:theme="@android:style/Theme.Holo.Light" for the <application> tag and NOT the <activity> .

Another possible problem may be how you get your context when using ContextThemeWrapper() . If you use something like getActivity().getApplicationContext() , just replace it with getActivity() .

Typically, Theme.Holo should be applied to fragments associated with MainActivity.

Please note that you use ContextThemeWrapper when you want to apply a different theme for your fragment. This can help if you provide a snippet of code from your main service where you add your snippets.


Some useful links:

Custom ListView in fragment without sticking to parent theme

+9
Mar 24 '13 at 22:57
source share

To apply one style, I used only

 getContext().getTheme().applyStyle(styleId, true); 

in the onCreateView() fragment, inflating the root view of the fragment, and it works for me.

+9
Jan 08 '16 at 21:30
source share

I tried the solution that David suggested, but not in all scenarios:
1. for the first fragment that is added to the stack, there is an activity theme, and not the one defined in onCrateView, but in the second fragment that I add to the stack and fix them, it is applied to the fragment.

2. On the second fragment, that they were displayed correctly, I did the following: I forced the application to close, clearing the memory, open the application and when the activity was recreated with the fragment. The fragment changed their incorrect activity, and not what was set in the onCrateView fragment.

To fix the problem, I made a small change and replaced the container argument from the inflater.inflate file with zero.

I donโ€™t know how the cheater uses the context from the container view in some scenarios.

Note that im uses android.support.v4.app.Fragment and android.support.v7.app.AppCompatActivity.

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // create ContextThemeWrapper from the original Activity Context with the custom theme final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme); // clone the inflater using the ContextThemeWrapper LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); // inflate the layout using the cloned inflater, not default inflater return localInflater.inflate(R.layout.yourLayout, null, false); } 
+7
Mar 23 '16 at 13:02
source share

Create a java class, and then use the layout you want to change the theme in onCreate method. Then specify it in the manifest as normal

+2
Jun 19 '15 at 9:46
source share

I solved the problem with android:theme = "@style/myTheme" in the fragment layout file. For example, this changes the style of LinearLayout and its contents, but not anything outside of LinearLayout . Thus, in order to decorate the entire fragment with any style, apply the theme to the outer most parent layout.

  <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:theme = "@style/myTheme" > <TextView android:id="@+id/tc_buttom_text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Time elapsed"/> <TextView android:id="@+id/tc_buttom_text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="00:00:00 00"/> </LinearLayout> 
+1
Aug 29 '17 at 7:23
source share

If you just want to apply a style for a specific fragment, just add these lines before calling onCreateView() or onAttach() fragment,

 getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT); 

and if you want to set a transparent status bar, then set false to the fitsSystemWindows property of your root layout,

 android:fitsSystemWindows="false" 
+1
Aug 6 '18 at 9:09
source share

I made it work by setting the theme in the context of the fragment before calling the inflator.

NOTE. This is an example of Xamarin.Android combined with MvvmCross. I am not 100% sure if this will work for Java programmers as well. But you can try :)

 public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Context.SetTheme(Theme); base.OnCreateView(inflater, container, savedInstanceState); var view = this.BindingInflate(FragmentLayoutId, container, false); // Doing some other stuff return view; } 

SetTheme Extension Method Code

 public static void SetTheme(this Context context, AppThemeStyle themeStyle) { var themeStyleResId = themeStyle == AppThemeStyle.Dark ? Resource.Style.AppTheme : Resource.Style.AppTheme_Light; context.SetTheme(themeStyleResId); } 

I hope this helps some people, cheers!

0
Jun 14 '19 at 10:32
source share

you can try this for candy in onAttach

final Window Window = activity.getWindow (); window.setStatusBarColor (myStatusBarColor)

and set the default to ondettach

-one
Jan 13 '16 at 23:26
source share

I know it's a little late, but it can help someone else because it helped me. You can also try adding the line of code below inside the onCreatView function of the snippet

  inflater.context.setTheme(R.style.yourCustomTheme) 
-one
Feb 28 '19 at 7:09
source share



All Articles