Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with a custom View attributes inside a Android Library Project

Tags:

android

I have a custom PieTimer View in an Android Library Project

package com.mysite.android.library.pietimer;  public class PieTimerView extends View { ... 

I also have a XML attributes file which I use in my PieTimer

    TypedArray a = context.obtainStyledAttributes(attrs,         R.styleable.PieTimerView); 

The XML styleable file looks like this

<?xml version="1.0" encoding="utf-8"?> <resources>      <declare-styleable name="PieTimerView">         <attr name="max_size" format="dimension" />         <attr name="start_angle" format="string" />         <attr name="start_arc" format="string" />         <attr name="ok_color" format="color" />         <attr name="warning_color" format="color" />         <attr name="critical_color" format="color" />         <attr name="background_color" format="color" />     </declare-styleable> </resources> 

I have the PieTimer in a layout file for a project that uses the library, like this

<RelativeLayout      android:layout_width="fill_parent"     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/root" android:layout_gravity="center_horizontal"     xmlns:app="http://schemas.android.com/apk/res/com.mysite.android.library.myapp"     xmlns:pie="com.mysite.library.pietimer"     >  <com.mysite.library.pietimer.PieTimerView     pie:start_angle="270"      pie:start_arc="0"      pie:max_size="70dp"     pie:ok_color="#9798AD"     pie:warning_color="#696DC1"     pie:critical_color="#E75757"     pie:background_color="#D3D6FF"      android:visibility="visible"     android:layout_height="wrap_content" android:id="@+id/clock"     android:layout_width="wrap_content"      android:layout_alignParentRight="true"> </com.mysite.library.pietimer.PieTimerView> 

Previously I was using the xmlns:app as the namespace for attributes like app:ok_color but when I made my project a library project and has a Free and Full version implementing the library, I found it no longer worked so I added the pie namespace.

The PieTimerView displays but the attributed do not work, they use the default (this was not occurring before) so there is some namespace conflict hapenning here.

What is the correct way of doing this?

like image 438
jax Avatar asked Dec 16 '10 13:12

jax


People also ask

What is the name of the file which is used to declare custom attributes?

You can take a look at it here (attrs. xml). You can define attributes in the top <resources> element or inside of a <declare-styleable> element.

When creating a custom view in Android which method is responsible for displaying that view on the screen?

Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.

What is ATTR colorSurface?

attr/colorSurface A color for surfaces of components, such as cards, sheets, and menus. ? android:attr/colorBackground The background for the screen. ? attr/colorPrimarySurface switches between colorPrimary in the Light themes, colorSurface in the Dark theme.


1 Answers

I know this post is super old, but if anyone comes looking, this problem has been fixed in ADT revision 17+ . Any services or Activities declare the namespace as:

xmlns:custom="http://schemas.android.com/apk/res-auto" 

res-auto will be replaced at build time with the actual project package, so make sure you set up your attribute names to avoid collisions if at all possible.

ref: http://www.androidpolice.com/2012/03/21/for-developers-adt-17-sdk-tools-r17-and-support-package-r7-released-with-new-features-and-fixes-for-lint-build-system-and-emulator/

like image 115
JRaymond Avatar answered Oct 01 '22 00:10

JRaymond