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).
Related
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
I've seen that all the layout ids are identified by #+id/viewid, but it seems the #+my_group_name/viewid works as well. Is it normal to use this naming convention, or the id must be in the "id" class ? I've checked the forum and found prefix based naming conventions but nothing like this.
In examples: I have a dialog layout called dlgeffect. In this layout:
<CheckedTextView
android:id="#+dlgeffect/text"
android:layout_width="0dip" ... >
I now the id's are reused, but during layout modifications if the ids are unique for the whole project the compiler gives error this way (and not runtime error)
Thanks,
Gzp
EDIT: and of course from java it is referenced as R.dlgeffect.text
I really dont know if you can do this, but always stick to standards, if you want it for reuse porpuse or setting property for more than element at once you can use same id for different element
I m newbie to android and i am working on an app in which i want to set the border of textview dynamically, i.e by code not by xml. i searched a lot on google but everywhere i find solution by xml. I tried a lot, but didn't find a correct way to implement this.
Like using this url, but i cant get to the result
How to draw borders for TextView created using Code not by XML in Android
please suggest me ideas,how i can achieve this..
The reason why you wont find much documentation about non-XML layouts is because most problems can be solved with the layouts. I would suggest at least defining the textview in the XML, then setting the border later in code. For example:
In the layout.xml file:
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
Then in your code:
TextView text = ((TextView)this.findViewById(R.id.text)); //use id to find view
ViewGroup.LayoutParams textLayout = text.getLayoutParams();
textLayout.topMargin = 10;
textLayout.bottomMargin = 10;
text.setLayoutParams(textLayout);
You can modify any attribute of the object this way. (use ViewGroup and View as a resource)
Note the above examples are just examples (I don't have a compiler in front of me to check syntax and effectiveness)
This is probably a trivia question, but why are there package/class names in some people's XML layout files?
(please don't downvote this question if it is something trivial, i don't even know what this is called, so i couldn't look it up).
i was looking at a tutorial, and i saw something like this (in "sample.xml"):
<com.tutorials.foo
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- some buttons and views here -->
</com.tutorials.foo>
My questions are:
1) i'm assuming that "foo" is a custom view? say, like you want to extend TextView with your own version of TextView?
2) what is this pattern/technique even called?
3) what would be the advantages of using this method?
Thanks so much in advance!!!
Yes, <com.tutorials.foo .../> is a custom view.
Calling it will be as same as others.ex:
Foo foo=(Foo)findViewById(R.id.foo);
I assume you mean creating layout static(.xml) or dynamically with code. xml layout would be in advantage when you know that you will use this layout in the program and will not change its format. Of course you can add to it or edit it with code later on. It is also in advantage for readabilty.
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.