Custom control states in library projects
This is a tutorial about adding states to custom controls in library projects. So first, how to create and reference library projects: Setting up library projects, Referencing library projects.
Now that the project is setup you can start creating custom controls and states for them. This will all be inside the library project. Firstly you need to create an attributes xml file in values/attrs.xml (the name has to be attrs.xml) and adding the state:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="customTextViewState"
;> - <attr name="state_marked" format="boolean" />
- </declare-styleable>
- </resources>
Next is adding the state to the custom control (in this case a textview). Simply create a new class that extends TextView and add these two private variables:
- private static final int[] STATE_MARKED = {R.attr.state_marked};
- private boolean isMarked;
Secondly create a setter method for isMarked:
- public void setMarked(boolean isMarked)
- {
- this.isMarked = isMarked;
- refreshDrawableState();
- }
Calling refreshDrawableState() is necessary for the textview to update its drawable state.
And finally overriding onCreateDrawableState method:
- @Override
- protected int[] onCreateDrawableState(int extraSpace)
- {
- final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
- if (isMarked)
- {
- mergeDrawableStates(drawableState, STATE_MARKED);
- }
- return drawableState;
- }
After this you're finished with the library, next step is to create a project and reference the library. Then add the previously created custom control to your layout:
- <com.helloandroid.lib.CustomTextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Random text"
- android:textColor="@drawable/textview_color
_selector"> - </com.helloandroid.lib.CustomTextView>
The name should be library package name + the custom control class name inside that package. In this case there's a package inside the library project called com.helloandroid.lib and inside that there's the custom textview called CustomTextView.
And finally the very last step is to create the textview_color_selector xml file. To do so create a file in one of the drawable folders called textview_color_selector.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.c
om/apk/res/android" - xmlns:app="http://schemas.android.c
om/apk/res/com.helloandroid.my app" > - <item app:state_marked="true" android:color="#347829"/>
- <item android:color="#FFFFFF"/>
- </selector>
Notice the app xml namespace, the package name is not the library package name. Because of a bug/limitation in android you have to write the package name of the project you're using the library in. For example if you have a library package com.helloandroid.lib and a project that references this library with a package name of com.helloandroid.useslib then you have to write this xml namespace import:
- xmlns:app="http://schemas.android.c
om/apk/res/com.helloandroid.us eslib"
and not
- xmlns:app="http://schemas.android.c
om/apk/res/com.helloandroid.li b"
even though the state is declared inside the library project. Hopefully this will get fixed someday. After all this you can use the CustomTextView's setMarked(boolean) method to change the state and the text color will be updated.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@LatestDroidNews (Droid News)Google Glasses Will Be Powered by Android http://t.co/WPhVO61C #android
9 min 39 sec ago -
@MissMary06 (Miss.Mary)I've just received an achievement: Guide http://t.co/5M4I8miy #Android #Androidgames
9 min 39 sec ago -
9 min 51 sec ago -
@indylewis (Chris Lewis)I've just received an achievement: The roads expert http://t.co/G3agIo2R #Android #Androidgames
10 min ago -
@hhy7923 (황하영)I've just received an achievement: Resource Hunter https://t.co/JPqyzIPe #Android #Androidgames
10 min 2 sec ago
Poll
Useful resources
Android Development Projects
- Android eMenu System by martinwinter76
- Online Marketplace Mobile App by Rbay
- Investigation Game Need developed by coder93
- Mobile app designer by mobileColling
- Nonpublic project #1462803 by Tolemy
- Smartphones tracking by tay2000
- Reader credit card from BlackBerry from audio jacket by garbageyunk
- Phonegap app developer by networkdeveloper
- Twitter Android Widget by Rock09tx
- Augmented Reality iPad / iPhone App for Revenue Share Deal by comiccreations



