I need to make two themes for android app. Dark and Light theme. And I want to use custom colors, sizes, fonts... So I want to make something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<!-- sizes, fonts... -->
</style>
<style name="AppTheme.Dark" parent="AppTheme">
</style>
<style name="AppTheme.Light" parent="AppTheme">
</style>
and then I want to make Dark and Light theme to extend AppTheme and just to change colors.
So I need some tutorials, tips or whatever how to do this most efficiently.
I appreciate every advice, thank you. :)
I think you are on a good track, you would define different styles for each and every theme, something like this:
<style name="AppTheme.Dark" parent="AppTheme">
<item name="colorPrimary">/* color for dark theme */ </item>
<item name="android:windowBackground">/* color for dark theme */ </item>
// default font color for dark theme
// etc.
</style>
<style name="AppTheme.Light" parent="AppTheme">
<item name="colorPrimary">/* color for light theme */ </item>
<item name="android:windowBackground">/* color for light theme */ </item>
// default font color for light theme
// etc.
</style>
AppTheme would define the style common to both Dark & Light themes.
Also, you would have a setting in your app that keeps track of current user theme. So let's imagine that this setting is kept in SharedPreferences. Then, every time an Activity is created, you would check the current setting and apply the corresponding theme:
#Override
protected void onCreate(Bundle savedInstanceState) {
boolean darkModeIsEnabled = // read the setting from SharedPreferences
if (darkModeIsEnabled) {
setTheme(R.style.AppTheme_Dark)
} else {
setTheme(R.style.AppTheme_Light)
}
super.onCreate(savedInstanceState)
setContentView(R.layout.my_activity)
}
One last point, you probably will want the users to switch the theme from the app, in that case you would need to recreate the activity/activities so that the new theme is applied.
For an example app you could have a look here. You can inspect the themes.xml file to see how there it's done.
Related
I'm creating a custom view in android that needs to get the fontFamily when its overridden in a custom app theme like so:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fontFamily">#font/raleway_semibold</item>
</style>
I want to have a way of getting this value, whilst also defaulting to the system default fontFamily if this custom override wasn't there.
I know I could provide a styleable attribute on the custom view but i wanna make use of the app theme so it stays consistent throughout the app.
Hope that makes sense
Thanks!
I managed to solve this by doing this:
final TypedArray typedArray = theme.obtainStyledAttributes(new int[] {
R.attr.fontFamily
});
Typeface font = typedArray.getFont(0);
if(font == null)
font = Typeface.DEFAULT;
and changing the xml value slightly to 'fontFamily':
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="fontFamily">#font/raleway_semibold</item>
</style>
Im trying to set the text and icons colors from header bar( i don't know the name of it exactly) when user click in a square button on menu. Follow the image below
enter image description here
i was trying to set there by style like this
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorOrange</item>
<item name="colorPrimaryDark">#color/colorOrangeDark</item>
<item name="colorAccent">#color/colorWhite</item>
<item name="android:textColorPrimary">#color/colorWhite</item>
<item name="iconTintMode">#color/colorWhite</item>
</style>
but im stuck. How can i do that ?
I learned how to color an Android activity status bar thanks to this solution: How to change status bar color to match app in Lollipop? [Android]
However it doesn't say how to make this for the entire app (all activities).
i don't want to duplicate those 4 lines of code into each Activity, and if I make a Java class for Utils, I can't reach my colors by using R.color.blue or getResources(....).
Is there a way to do this through the Manifest, perhaps? Or any other way?
Thank you!
You should create your own style in values/styles.xml. Then make your own theme with such parameters. Loock code below
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/color_primary</item>
<item name="colorPrimaryDark">#color/color_secondary</item>
<item name="colorAccent">#color/color_accent</item>
<item name="android:statusBarColor">#color/color_primary</item><!--this is what you need-->
</style>
Or just use method (for Lolipop):
public abstract void setStatusBarColor (int color)
I'm developing an android application and I cannot remove the title bar permanently. A solution posted here: How to hide the title bar for an Activity in XML with existing custom theme
(the 600 upvoted one, the others didn't work as described below) worked in general, but it maintained the title bar for a brief millisecond when initially launching the app.
I've tried various solutions throughout stackoverflow and other sites modifying the android theme in the manifest xml and style file(s). However, all of these solutions have all crashed the application before it began. The message in LogCat being that I must use an AppCompact theme. My main activity extends the ActionBarActivity class, and I have tried switching it to just Activity while also removing the :
if (savedInstanceState == null) {getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
that is generated. However, when I do so, all of the views on the main activity disappear and it becomes completely white with nothing else. Is there a way to completely remove the actionbar while extending ActionBarActivity? If not, how can I switch to extending Activity or some other class while maintaining no other errors?
on styles.xml you should make that
parent="Theme.AppCompat.NoActionBar"
Do you use custom view for the ActionBar? if yes, then maybe you'll find the approach I use acceptable. I just set text color for Title and Subtitle the same as background color in my app's theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.ActionBar">
<item name="android:titleTextStyle">#style/ActionBar.Title</item>
<item name="android:subtitleTextStyle">#style/ActionBar.Subtitle</item>
<item name="android:background">#color/my_color</item>
<item name="android:backgroundStacked">#color/my_color</item>
<item name="android:backgroundSplit">#color/my_color</item>
</style>
<style name="ActionBar.Title">
<item name="android:textColor">#color/my_color</item>
</style>
<style name="ActionBar.Subtitle">
<item name="android:textColor">#color/my_color</item>
</style>
Then I inflate and apply custom ActionBar view in onCreate and set custom title
i wan't to do a simple thing, my background's windowTitle must change in my application. But how i can access to my background's windowTitle ?
My style.xml :
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#drawable/fond</item>
</style>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowTitleSize">40dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
And in my Activity :
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.page_doc);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
How i can do this ?
You can just use the ids of the window_title layout. Just like you would do with normal layout.
I assume that you have something like a LinearLayout in your window_title layout. Just use the id of it to get it and set the background Drawable.
try to use this
((TextView)((FrameLayout)((LinearLayout)((ViewGroup) activity.getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setBackgroundResource(R.drawable.back);