Srneczek
Srneczek

Reputation: 2193

Generate style from xml in intellij (/android studio)

Is there a way how to generate style from XML attributes so I don't have to manually copy paste 1 by 1? When I prototype, I use the "inline" styles then I want to export styles into styles.xml but manually it is time consuming task. It is just different format, should be easy to automate.

<ImageView
        android:layout_width="80dp"
        android:layout_height="47dp"/>

into

<ImageView
        style="@style/h2_image"/>

<style name="h2_image">
    <item name="android:layout_width">80dp</item>
    <item name="android:layout_height">47dp</item>
</style>

Upvotes: 14

Views: 2314

Answers (3)

Archit Garg
Archit Garg

Reputation: 3257

Very Simple.!

Just keep the cursor in the file from where you want to extract the style resource. Just a right click then click on refactor.

Refactor -> Extract -> style/layout.

This bow will apear

Then give style a name and there you go..! For more you can refer the documentation.

Upvotes: 16

BOUTERBIAT Oualid
BOUTERBIAT Oualid

Reputation: 1544

Try to Declare your Style like this :

<?xml version="1.0" encoding="utf-8" ?>
      <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <style name="YourCustomStyle">
            <item name="android:layout_width">"80dp"</item>
            <item name="android:layout_height">"47dp"</item>
             <!-- another attributes -->
            <item name="customAttr">value</item> 
        </style>
      </resources>

And in your xml add this android:theme="@style/YourCustomStyle"

Upvotes: 0

peko
peko

Reputation: 11335

Try something like that:

<resources>
    <style name="CustomImgView">
        <item name="android:layout_width">80dp</item>
        <item name="android:layout_height">47dp</item>
    </style>
</resources>

<ImageView style="@style/CustomImgView" />

more information: https://developer.android.com/guide/topics/resources/style-resource.html

Upvotes: 0

Related Questions