MyWay
MyWay

Reputation: 1025

Create a different directory other than res/values/styles.xml to define styles

As I asked in the title, is there any way to do so?
Now, when I put all styles into one file it looks a little crowded, I would like to separate styles.

For example:

res/values/styles_for_main_screen
res/values/styles_for_set_screen

And then in main_screen layout

<TextView
     style="@styles_for_main_screen/text_view_custom_style">
</TextView>

This example obviously doesn't work, but it shows what I'd like to achieve.

I read in every tutorial that we need to put our custom styles into styles.xml file, but I wonder if there is a possibility to diversify styles in few xml files?

Every question I read was something like "how to do .... in styles.xml".
I can't find question similar to mine.

Example how it should be done, thanks @Frank N. Stein for the answer:

This how looks my custom xml res/values/styles_for_main_screen

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="custom_back">
        <item name="android:background">#E81C1C</item>
        <item name="android:text">whatr</item>
    </style>
</resources>

and then to retrieve this style I just write:

<TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        style="@style/custom_back"
        />

so the convention looks like:

style="@(what I want to retrieve)/(name of style)"

from the android developers site

In XML: @[package:]style/style_name

Upvotes: 0

Views: 533

Answers (2)

Thiago Furtado
Thiago Furtado

Reputation: 118

Remember that, android scans the files found int the /values directory by reading their content. For styles, every <style name="styleName" > ... </style> will be parser and a style object reference will be created.

Then, as Frank said, Yes. You can use whatever file name to write your custom styles.

Upvotes: 1

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

You can call your style files whatever you want (if you respect the naming conventions and you put them all in the values folder/s), as you do with strings and colors.

Therefore, YES! You can have multiple ones, if so you desire.

Obviously, you will NOT specify the path to each file.
Referencing the style/s by using R.style.your_new_style is enough.

Upvotes: 1

Related Questions