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>
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.
Then give style a name and there you go..!
For more you can refer the documentation.
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
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"
Related
What is the name of the blue view in the picture below?
I have tried searching "EditText cursor" and "EditText selector", but I am unable to find a name for this drawable.
Also, is it possible to change this drawable?
It is called the Text Select Handle. You can customize it with this handy tool. http://android-holo-colors.com/
in your theme add something like:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style parent="#style/_AppTheme" name="AppTheme"/>
<style parent="android:Theme.Holo.Light" name="_AppTheme">
<item name="android:textSelectHandleLeft">#drawable/apptheme_text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/apptheme_text_select_handle_right</item>
<item name="android:textSelectHandle">#drawable/apptheme_text_select_handle_middle</item>
</style>
</resources>
edit: spelling
I have a quick question.
Is there a way, using eclipse, to define a default theme for my app, or do I have to configure each activity and asset to match my desired theme?
If there is a way, how do I go about doing that?
Define your android:theme in application element of your manifest.
Create an xml file in res\values. In it create a default layout style like this (you can define other stuff also this is just an example):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
Use one style for each default theme you want to create. Then in the Widget you want to use this style you enter like this (again just an example):
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
This TextView will now inherit the settings declared in the style.
I am writing an xml for a drawable. My question is: is there a way to give the src property a reference to a attribute defined in the attr.xml file ? I've tried:
android:src="?image"
and
android:src="?attr/image"
None of them seem to work. Here's my attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="image" format="reference"></attr>
</resources>
and where i define the values, which are related to theme:
<resources>
<style name="HOLO" parent="#android:style/Theme.Black">
<item name="android:textColor">#00FFFF</item>
<item name="image">#drawable/holo_vcard_settings_icon</item>
</style>
<style name="HOLO_LIGHT" parent="#android:style/Theme.Light">
<item name="android:textColor">#FF00FF</item>
<item name="image">#drawable/holo_light_vcard_settings_icon</item>
</style>
</resources>
A few suggestions:
Try clean your project. I've experience a few times where the XML changes are not updated in an Eclipse build.
Are you sure that your application or activity is either using HOLO or HOLO_LIGHT theme? Check the 'android:theme' key is set in your activity.
Try using the 'android:background' property instead of 'android:src', meaning don't use ImageView and just use a View or LinearLayout's background.
BTW, the correct syntax to reference your attribute is:
<... android:src="?attr/image" .../>
4th option: If all else fails, you can try using the attribute as a style. (just a hunch, it may not work) e.g:
<resources>
<attr name="imageStyle" format="reference"></attr>
</resources>
then in your styles.xml
<style name="LightImageStyle">
<item name="android:src">#drawable/light_png</item>
</style>
<style name="DarkImageStyle">
<item name="android:src">#drawable/dark_png</item>
</style>
then in your themes.xml
<style name="HOLO" ...>
<item name="imageStyle">#style/DarkImageStyle</item>
</style>
<style name="HOLO_LIGHT" ....>
<item name="imageStyle">#style/LightImageStyle</item>
</style>
finally in your layout:
<ImageView style="?attr/imageStyle" ... />
I would like all the buttons of my android application use the same background image, that's all buttons have attribute:
android:background="#drawable/my_btn_bg"
Is there any way to define this in one place and take effect in the whole application's buttons? (instead of define the background on each button component)
I think define inside theme.xml could be a solution, but I do not know how to do it ? Could some one give me some hints?
For Android styles, you reference the preset attributes of R.attr. Here, you want to to reference android:buttonStyle. You can try this :
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">#style/yourButton</item>
</style>
Also look in this Themes and Styles
Define styles.xml with the below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/bg_button</item>
</style>
</resources>
And use the same style for any button within your application as:
<Button
android:text="Test Button"
android:id="#+id/btnTest"
style="#style/buttonStyle">
</Button>
I have a problem while creating styles in android:
When I use
android:textAppearance="?android:attr/textAppearanceSmall"
in Button for instance, that's working fine, but if I create a styles.xml like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="smalltext" parent="#style/Text">
<item name="android:textSize">10sp</item>
<item name="android:textColor">#FFF</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>
</resources>
and replace in Button:
style="#style/smalltext"
nothing is taken into account.
Would you have any idea of what could be wrong ?
thanks a lot,
Luc
Keep in mind that styles will not be applied in the layout view in Eclipse. You'll have to deploy the application to a device (or the emulator) to see the changes.