Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling Custom Views

I have a few custom views in my Android project and I've added the relevant details to the attrs.xml file. And now I can implement my objects through XML. This works fine.

How do I style these elements? When I try to use my custom attributes in the styles.xml is get an error "No resource found that matches the given name:"

For using the custom views in normal xml developement I use xmlns:app="http://schemas.android.com/apk/res/bla.bla.bla". What is the correct for use in styles?

This is what my style looks like currently

<style name="Journey_DaySelect_Sunday">
    <item name="app:onImage">@drawable/day_sunday_selected</item>
    <item name="app:offImage">@drawable/day_sunday</item>
</style>
like image 723
Kurru Avatar asked Mar 18 '11 19:03

Kurru


People also ask

What are the types of custom views?

In Android, there are actually two other Views readily available to do this: Spinner and AutoCompleteTextView , but regardless, the concept of a Combo Box makes an easy-to-understand example.

Can you create custom views How?

Go to View > Workbook Views > Custom Views > Add. In the Name box, type a name for the view. Tip: To make a view easier to identify, you can include the name of the active worksheet in the name of a view. Under Include in view, select the check boxes of the settings that you want to include.

What is meant by custom view?

A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.

Why do we need a custom view?

View are typically created to provide a user interface experience with is not possible with the default views. Using custom view allows the developer allow to do certain performance optimization, i.e., in case of a custom layout the development can optimize the layout manager for his use case.


2 Answers

After more intensive searching on Google I gave up finding it answered elsewhere, and by chance tried using the absolute namespace of my generated R file it worked. May this solve all your problems.

USE THE NAMESPACE CONTAINING YOUR R FILE

<style name="Journey_DaySelect_Sunday" parent="Journey_DaySelect">
    <item name="AppZappy.NIRailAndBus:onImage">@drawable/day_sunday_selected</item>
    <item name="AppZappy.NIRailAndBus:offImage">@drawable/day_sunday</item>
</style>
like image 93
Kurru Avatar answered Oct 05 '22 02:10

Kurru


For clarification, the item's name attribute should be the same as what is in the attrs.xml's declare-styleable name attribute + ":" + the attribute name.

For example:

attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>  
    <declare-styleable name="com.chuck.norris">
        <attr name="actionBarTextColor" format="color"/>
    </declare-styleable> 
</resources>

style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="myNewStyle">
        <item name="android:textColor">#FFFF0000</item>
        <item name="com.chuck.norris:actionBarTextColor">#ffff0000</item>
    </style>
</resources>

You can then apply this style to all activities by using a theme in your manifest.xml file. Anywhere that a custom view exists that wants to use the "actionBarTextColor" attribute, you can then use the Java code:

TypedArray typedArray = context.obtainStyledAttributes(attrSet, R.styleable.com_chuck_norris);
COLOR_ACTION_BAR_TEXT = typedArray.getColor(R.styleable.com_chuck_norris_actionBarTextColor, 0xff0000ff);
typedArray.recycle();

I'm not sure why you cannot just define your schema in your style.xml file as was asked above, but it seems to be a limitation of style.xml.

like image 34
Shellum Avatar answered Oct 05 '22 02:10

Shellum