Add custom XML properties to default android views - android

I have been searching around with google about this topic, but found no relevant information. It is clear to me how I can do it extending Views, but I don't want to extend anything.
I would like to somewhat "annotate" whichever android view (or whichever descendant of view) with custom properties and then retrieve their value in runtime.
Like this:
<TextView
custom:my_property_name="foo here"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_view"
android:text="Rule" />
Then once I have a reference to this TextView I would like to call a method like:
String myProperty = textView.getProperty("my_property_name");
myProperty.equals("foo here");
Is this possible? How?
Thanks.

At the very least you have to create your own class that subclasses one of the standard View classes (or View itself). The existing framework code does not read attributes that are not defined by Android and that are not part of the styleable declaration for that View.
Android documentation has a page describing creating your own views with your own XML attributes: http://developer.android.com/training/custom-views/create-view.html

Related

Creating id for Android Views

This question is quite basic. Let me go with an example.
I have two activities
activity_a.xml
ActivityA.java
activity_b.xml
ActivityB.java
Both the XML files contain only a TextView to display a simple text. As usual, the TextViews are going to be referenced in the corresponding .java files using their View id
My question is, if it is right to reference the TextView in both the XML files with same id? (like using the below code with exactly same id for activity_a.xml and activity_b.xml)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I was practising this procedure without any problems. When trying to reach the corresponding xml code for the TextView using Ctrl + Click (on Windows), I am provided with two options (to display the TextView's xml code from activity_a.xml or from activity_b.xml).
Also, what is the recommended way to name a View in Android? This will be helpful, when your Android project contains multiple layout files.
Yes , if you have same name of view or view group in different layout then it tells from which layout file it belongs to, ask for select required layout file.
so for that you have to follow proper naming conventions to avoid this type of confusion
https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md
or you can give name like
activity_home_tvUserName if username textview from home activity
and
activity_profile_tvUserName if username textview from profile activity.
if it is right to reference the TextView in both the XML files with same id?
It is totally fine, the compiler will only look at the ID under a single view hierarchy.
e.g.: findViewById(R.id.textview) inside ActivityA.java will only search for textview ID inside activity_a.xml (assuming you have setContentView(R.layout.activity_a); beforehand.
what is the recommended way to name a View in Android?
In my opinion, you just need to be consistent in naming your view throughout the app. The main goal is to avoid misinterpretation and confusion.
Hope it helps!
They are in different activities, so you should have no problem using the same id. you could event declare them both as #+id/textview. However, why not just use the same XML file for both activities? No reason you can't.
You can also create an ids.xml file under the values folder and declare all your ids under it, so you don't have to declare them in your layouts, but this is not a very common approach.
if it is right to reference the TextView in both the XML files with same id?
My answer is Yes, It is right.
Whenever we set the setContentView(R.layout.activity_a), then it'll search for the given id within the above activity. Local attribute having the same id will take the more preference over the other attributes with the same id.
But having unique id's is Best practice.

Use of android:tag parameter in xml?

I saw a xml layout which has a textView as below
<TextView
android:id="#+id/tvHeaderTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Change password"
android:tag="#string/regular"
android:textAllCaps="true"
android:textColor="#color/header_color"
android:textSize="#dimen/font30" />
I want to know what is android:tag property is used for. A detailed answer for be greatly appreciated.
Note - I read that it is used for data binding, but could not follow the context.
Update 1 - (Thanks Blackbelt for the answer)
Usages
1. Could be used for compile time binding of xml elements with the activity. Although android recommends the use of id.
2. Could be used for identifying elements for list adapter. For eg, a special case of a list adapter with multiple sort support. Then we could use the tag element to identify the desired list item.
I would be glad to know if it could also be used for assigning any other property?
I want to know what is android:tag property is used for
it is a different way to add associate additional information with the view object itself. In you case your tag is a string, but it can store complex objects too. For instance the tag could be a model class set and retrieve at runtime using the setTag()/getTag() pair. The way this information is used is up to the users. Android allows to findViewWithTag too. In your case, for instance you could look for the same object using findViewById(R.id. tvHeaderTitle); or using findViewWithTag(getString(R.string. regular)); . An example of usage is with ListView when you have a button part of you item and, upon click you want to know the which item in your dataset is associated with that row. Using the pair setTag/getTag this thing is easily achievable

Building my custom compound view for Android. How to expose properties?

Doing something similar to this: http://developer.android.com/guide/topics/ui/custom-components.html
Now I want to do something like this:
<com.me.activities.MyCompoundControl android:id="#+id/someName" android:layout_height="wrap_content" android:layout_width="wrap_content" customproperty="12345"/>
So, my question is how do I declare/code
customproperty
So I can reference it in XML when insert my custom view?
See http://www.infidian.com/2008/05/02/android-tutorial-42-passing-custom-variables-via-xml-resource-files/ for how to use the /res/values/attrs.xml file to specify the custom attributes you want to attach to your custom view and then access in the component initialization.
You need to define custom attributes in res/values/attrs.xml and read their values in the attribute-aware constructor of your view. Then you refer to these attributes using your custom namespace.
You can see an example I have done in one f my libraries:
Attributes are defined here:
http://code.google.com/p/android-flip3d/source/browse/res/values/attrs.xml
And parsed here:
http://code.google.com/p/android-flip3d/source/browse/src/pl/polidea/androidflip3d/Flip3DView.java#117
And referred here:
http://code.google.com/p/android-flip3d/source/browse/res/layout/main.xml
There is no good documentation on available attribute types though and you need to browse android source code (look for attrs.xml there)

how to find in d.android.com?

Im new to this so much is a bit confusing now. But I see that d.android.com is a goldmine if you know how to use it and find the stuff.
How do I use this resource to find what Im searching for? To explain a bit how I mean. I have read a book with this code:
<LinearLayout...
...
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/on" />
I wanted to see what variables (right name?, like layout_gravity) ImageView could have and its attributes (?, like center_horizontal) so I checked out:
http://d.android.com/reference/android/widget/ImageView.html
but nowhere I could find any of the above variables. So instead I tested to check its parent LinearLayout:
http://d.android.com/reference/android/widget/LinearLayout.html
But there were nothing either of above variables. There was android:gravity thats looks alike tho.
So how should I do to find which variables and its attributes a class (?, like imageview)
can have?? Where/how do I find information like this??
First, everything you can set through XML can be set through code too, so this correspondence can help you.
Second, in the references the attributes (it's the name for XML "variables") are not always shown: only the ones that are particular of that class are, the others are inside an inherited XML attributes expandable section.
As an example, android:id is an attribute in common with every class inheriting from View.
Third, LayoutParams are a kind of their own: programmatically, you set a view's layout params with View.setLayoutParams(LayoutParams), and it's LayoutParams that cointains those members/attributes. In XML this is represented by prepending layout_, but it's only a convention.
The base class for LayoutParams is ViewGroup.LayoutParams. Every layout class adds something by extending it (for example, android:layout_gravity is an attribute added by most of the layouts).

Define custom Android components using short namespace?

Working on my first Android application. I'm wondering if there's a way to use the xmlns in the markup in any way. In Flex, for example, I can define a namespace:
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cb="com.typeoneerror.apps.app.views.components.*">
<cb:CustomComponent paramName="demo"></cb:CustomComponent>
</mx:VBox>
Android seems to be slightly different. You use the namespace when defining params but not the tag itself. This is a bit wordy to me, so I'm wondering if there's a way to configure or change this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cb="http://schemas.android.com/apk/res/com.typeoneerror.apps.app">
<com.typeoneerror.apps.app.views.components.CustomComponent cb:paramName="demo"/>
</LinearLayout>
I'd like to use
<cb:CustomComponent cb:paramName="demo"></cb:CustomComponent>
Possible?
No, sorry. The element name is a Java class name, and in the case of custom widgets, is a fully-qualified class name.
I have seen some syntax where the element name is View and there is a class attribute with the widget's class name. I can't find that in the docs and don't have an sample available, though.

Categories

Resources