I am making an app, that has a lot of text to read, and i want it to have 2 themes. dark and light. So i think the way to do this is to have 2 XML layouts. But I don't know how to set a 1 style to 1 laayout, and a different style to a different layout. There is a field android:theme in the manifest, but that sets a theme to the whole app, and not a specific layout.
So, to be clear, my question is; How do you set a style/theme to a specific activity and not the whole app?
In the manifest, android:theme can be added to the activity tags, and those override the application tag theme.
<application
android:theme="..." <!-- App theme -->
...
<activity
android:theme="..." <!-- Theme for just this Acitivty -->
You can also try to programmatically call setTheme in an Activity class
i think the way to do this is to have 2 XML layouts
You just need one layout that can pick up the attributes of the theme(s) you'll set
Related
Every time I go to xml file I've to change my theme first to see how my views will actually look, my parent theme is inheriting from MaterialComponents theme and all of the activities are using the same base theme, but still I don't know why in xml it shows Material.Light as my default theme.
and AppTheme is the theme that I want to set to default, is there any way I can do that.
To set or change the default theme, open AndroidManifest.xml and in the application tag, set if not exist or change
android:theme="#style/Material.light"
to whatever you want. You can change theme of individual activity by adding same line in the Activity tag of any Activity.
I am new to android programming. I wanted to know, what would happen when you apply a theme to an application or activity as:
<application
android:theme="#style/MyTheme"...
and inside the activity one of the buttons has:
<Button
<style="#style/my_button_style"..
Would it pick the style defined in the my_button_style or the style defined in the theme? I experimented but the results are a bit confusing.
This sets a theme for your entire application from the start:
<application
android:theme="#style/MyTheme"...
But explicitly applying a style to any of your elements in your xml like this will override the application theme set in your manifest ONLY for that specific element you applied style to.
<Button
<style="#style/my_button_style"..
In summary it's the last declaration that will win so since it goes like
Manifest --> layouts
then
Manifest android:theme="theme" --> View element style="#style/style"
so in the end your style declaration will override the theme but only for the element it's declared in.
Why is there a difference between theme defined in AndroidManifest.xml and theme taken from styles.xml?
1) AndroidManifest.xml:
<application ... android:theme="#android:style/Theme.Black">
2) AndroidManifest.xml
<application ... android:theme="#style/AppTheme">
styles.xml
<resources>
<style name="AppTheme" parent="#android:style/Theme.Black" />
</resources>
1st setting gives black theme and no action bar. 2nd has dark action bar and light menu.
EDIT : options 1) and 2) - notice Menu and ActionBar
EDIT 2:
Why doesn't the 2nd option actually use the AppTheme (Theme.Black) ? (tested on SGS3)
You probably have another styles.xml file, perhaps under a directory like "values-v11", that is defining the #style/AppTheme differently than #android:style/Theme.Black and taking precedence over the file you're viewing/modifying.
#android:style/Theme.Black implements the exact theme implemented by Android (or device manufacturer). However, #style/AppTheme allows you to perform custom modification in your theme which actually extends the original Theme.Black from android, and in order to perform custom modifications, you use style resources.
In simple words, its just like using Activity class or YourOwnActivity class which extends Activity with extra features inside.
Styles.xml enables you to create your own themes. In AndroidManifest, you set the theme you want for an app or activity. You may want to use a system theme or your own. You can also extend other themes as you're doing setting "parent" attribute. For further information, check this out:
http://developer.android.com/guide/topics/ui/themes.html
You should try to put:
<resources>
<style name="AppTheme" parent="#android:style/Theme.Black" />
</resources>
in a xml file called res/themes.xml
Can I set a theme for the app I create?
I am talking about themes like in...
http://developer.android.com/design/style/themes.html
or should I use Styles and themes, and individually modify every child?
You indeed have to use Styles and Themes.
You can add a theme to whole of your application with just one line in the manifest:
<application android:theme="#style/CustomTheme">
I have a couple custom preference items -- one that displays a swatch of the currently selected color, and another one that displays a thumbnail.
I have a custom layout for these that matches up very well, and have found that I can make the text appearance match by using android:textAppearance="?android:attr/textAppearanceLarge" as part of the TextView's xml. The problem is, while these generally look fine, they must not be the appearance the 'official' preferences use, since the colors end up wrong on some devices. In particular I'm porting my application to the Nook Color, and it uses a light grey background and black text color on the preference screen instead of black background/light grey text. My text color in this situation stays the same, but the rest of my layout is themed appropriately.
I'm really unsure what I'm supposed to do here to make my text match up with the 'official' theme. Should I be using obtainStyledAttributes and running though my layout to set things? The tutorials I've seen on using that so far have been really baffling, and it seems like there must be a textAppearance or style I can set in the XML to fix this.
You've to define your own application theme which inherits from the official theme you want. Actually, you can just define a theme MyTheme extending the Theme.Light for example.
Create an res/values/styles.xml file like that:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Light">
</style>
</resources>
Then, you just have to apply your theme using the android:theme attribute of the application entity of your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
[...]
</application>