Wednesday, March 23, 2011

How to compose an 'attrs.xml' in android?

There are a lot of blogs which describe how to create custom attributes [3-5]. But I couldn't find anyone which describes the syntax and possible xml attributes for each of the elements inside it. Also, there aren't any documentations available for this as well!

In order to figure out this syntax, I had dig into some internal android code! I know that android is open-source. I like open-source and it's cool. But one shouldn't have to dig into the code in order to understand this simple information, which could have been documented.

Thus this blog is an attempt from my side to document this feature, so as to keep others from wasting their time in doing the same!

declare-styleable
List of possible xml-attributes for this element:
  1. name: name of this styleable.
  2. parent: name of the styleable from which to inherit the attributes.
List of possible children:
  1. attr: the attribute which needs to be defined inside this styleable.

attr
This element can either be found as a tag outside the 'declare-styleable' tag or as an child inside it. List of possible xml-attributes for this element:
  1. name: name of this attribute.
  2. format: data-type represented by this attribute. (This is optional)
List of possible children:
  1. enum: If the current 'attr' is of type 'enum', then you have to define possible enumerations for this attribute.
  2. flag: If the current 'attr' is of type 'flag', then you have to declare this.

enum
List of possible xml-attributes for this element:
  1. name: name of the enum element.
  2. value: the value to be used for this element.

flag
I'm still unable to figure out the syntax for this element. Whenever I'll find out, I'll update this section. So, please do check-back here for more info on this!

format
One small catch here! I just said that this is an optional attribute to 'attr' element. However, from the code [1] it seems that an 'attr' with not 'format' will be silently ignored! Hence, I would recommend putting one of the values below for this attribute. Possible values for 'format' attribute of 'attr' are: [2]
  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

That said, an example should clarify much of the stuffs here. Given below is an example 'attrs.xml' which contains all the above elements/attributes. Go through this carefully in order to understand the syntax.

An example 'attrs.xml':
<resources>
    <attr name="global1" format="float"/>
    <declare-styleable name="parent-attr">
        <attr name="attr1" format="integer" />
        <attr name="global1" />
    </declare-styleable>
    <declare-styleable name="myattr" parent="parent-attr">
        <attr name="attr2" format="integer" />
        <attr name="attr3" format="enum">
            <enum name="name1" value="value1" />
            <enum name="name2" value="value2" />
        </attr>
    </declare-styleable>
    <declare-styleable>
        <attr name="global1" />
        <attr name="attr4" format="boolean"/>
    </declare-styleable>
</resources>


NOTE:
  1. All the 'attr' elements in the current xml file will be parsed into a single 'Map'. So, you have to make sure that all 'attr' elements have unique names! [1]
  2. However, if there is an 'attr' element which needs to be shared between 2 styleables, then make it global. (But make sure that you specify a 'format' for this!) Now inside the 'declare-styleable's where you want to define this 'attr', just put this without the 'format'. There is an example of this in the sample xml above. [1]


REFERENCES:

Saturday, March 19, 2011

Proguard.cfg missing in eclipse?

This happened to me after I updated my eclipse today. Basically, whenever I was trying to create a new project, I used to get this error message. The reason for this error is due to mismatch between the versions of your ADT, sdk-tools and eclipse. The best thing to do is to upgrade all of them to their latest versions.

Upgrading ADT: From inside eclipse, Help -> Check for Updates and then follow the instructions.
Upgrading sdk-tools: From inside eclipse, Window -> Android SDK and AVD manager and then follow the instructions.

After you are done, just restart the eclipse and you'll be back on track!

Tuesday, February 1, 2011

Small class to quickly print out application information in HTML

Wrote a class, which queries the package manager in order to fetch the package name, version code/name and permissions for a given app. There's also a method which returns a html formatted string which could be used to display your app's info (probably in a TextView or an AlertDialog). This will help avoid creating a separate 'about' HTML for your app. Go through this link for more information on this class. (The source file has been released under APACHE LICENSE2)

Monday, January 24, 2011

Setting up LG-Optimus One for development

I have been writing android-apps for over more than 8 months now. Finally, I got hold of my first android smart-phone, LG-Optimus One (Model name P500). The following were the steps I had to perform in order to get this phone ready for Android development.

Installing USB driver:
By default, this phone will not be detected over USB, as windows (Win7 32b) failed to find the USB driver for this device. Google search suggested that P500 USB driver needs to be manually installed. To do that, just go to the following link: http://www.lgforum.com/resources/ and select the 'LGAndroidDriver_Ver_1.0_All.exe' executable (about 9.4MB). Then run this exe in order to install the USB driver. Wallah! You're done!

Enabling USB debugging:
Again, by default, USB debugging will be disabled on this phone. (Makes perfect sense as this feature is for development only) In order to enable just select the Settings -> Applications -> Development -> USB debugging check-box. Now connect the USB cable. After this, open a command-prompt and then type adb devices. You'll see something like this:
> adb devices
List of devices attached
******************      device
 You're done! Now, you should be able to run an application (assuming android:debuggable is set true in the app's AndroidManifest.xml) and see the log messages through 'ddms'.

Happy coding!

Thursday, January 20, 2011

traceview fails to find the trace file (on windows)

You have the trace file 'my-trace.trace' present in the current working directory. However, when you type traceview my-trace, you would get the error message: "trace file 'my-trace' not found". Even if you append the trace file with the '.trace' extension, you would get the same error message.
Somehow, the traceview cannot recognize relative paths. So, the solution is to pass the absolute path of the trace file as input to the traceview.
EG: traceview C:\path\to\file\my-trace

(PS: It's been assumed here that the android-tools folder is in your PATH env-variable.)

Sunday, January 2, 2011

Kaprekar-Numbers

Kaprekar-numbers are interesting. For eg.: 703 is a kaprekar-number. Reason: 7032 = 494209 and 494 + 209 = 703. It's also been proven that one can get all the Kaprekar-numbers by prime factorization of 10n-1. However, I wanted to list all the Kaprekar-numbers by solving it in its pure form! A number 'k' (in base 10) is called as Kaprekar-number if it can be expressed in the form:
k = q + r   and   k2 = q * 10n + r
Where, q >= 1, 0 < r < 10n
I've hosted this program on github at the following location (read-only): git://github.com/teju85/Kaprekar-Numbers.git. If you are interested in this, please download the program and follow the instructions inside the 'README'.
I profiled this program and here are the results:
$ make profile
printCpuInfo.sh
Processor Information:
processor       : 0
vendor_id       : GenuineIntel
model name      : Intel(R) Xeon(R) CPU           X5355  @ 2.66GHz

kaprekarnumber -nooutput -profile -limit 10000
Found 17 kaprekar-numbers among first '10000' integers.
Took 0.001309 seconds to search for self-numbers among these integers.

kaprekarnumber -nooutput -profile -limit 100000
Found 24 kaprekar-numbers among first '100000' integers.
Took 0.014230 seconds to search for self-numbers among these integers.

kaprekarnumber -nooutput -profile -limit 1000000
Found 54 kaprekar-numbers among first '1000000' integers.
Took 0.188414 seconds to search for self-numbers among these integers.

kaprekarnumber -nooutput -profile -limit 10000000
Found 62 kaprekar-numbers among first '10000000' integers.
Took 1.616492 seconds to search for self-numbers among these integers.

Not sure whether the timings are 'optimal' or not. Still looking for possible optimizations...

Saturday, January 1, 2011

Removing password from pdf files

We frequently come across a lot of password-protected pdf's in our daily lives, be it our internet/credit-card bills. Problems come when we want to share these kind of files with others. We obviously don't want to share the password as well! There are a lot of (commercial!?) tools out there which claim to remove password from pdf files. However, we all know that ghostscript already provides an option of reading password-protected pdf files and then storing them back to hard-disk. So, wrote a wrapper-tool for myself in order to do this process for me. I've created a repo for this tool and is publicly available (for free, ofcourse!). If you want to use this tool, please download all the files from this repo: git://github.com/teju85/No-Pass.git.

Happy new year fellas!