I have a custom view extends RelativeLayout. I know I can modify it's children in the java code, but is that possible in xml too?
I want to have something like that in my main.xml:
<MyCustomView>
<SomeControl />
<AnotherControl />
</MyCustomView>
<MyCustomView>
<NewControl />
</MyCustomView>
Can you give me hints how to achieve that?
Thank you for your help!
You may find some explanations here : http://developer.android.com/guide/topics/ui/custom-components.html
You can actually use your custom components in any layout XML file, using the fully qualified name of your class:
<com.package.MyClass id="#+id/my_id" ...>
...
</com.package.MyClass>
Related
I built a layout like this in XML say "block.xml"
Now, i want to create an XML like this
so that i can access each block as an array. Can somebody tell me how can i use block.xml as a template to generate my new xml file to be put as a UI part in android. Thank You
Just to be sure , i want to use table layout i dont know how to procees.
In your layout xml you may use the include tag and re-use your block.xml
as:
<include
android:id="#+id/block1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/block" />
And in your java code you can access views from block.xml as
View firstBlock = findViewById( R.id.block1 );
View blockButton = firstBlock.findViewById( R.id.someButtonId );
You should inflate and add the sub layouts programmatically.
This link has example code:
Android using layouts as a template for creating multiple layout instances
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
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.
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).
I'm trying to create a rotating imageView. Looking at this (http://stackoverflow.com/questions/1930963/rotating-a-view-in-android) I know I'll have to override onDraw.
But when I create my own derrived TextView class, how do I use it in my xml layouts?
To use it in your xml layout add the following:
<view class="yourpackage.yourclass.yourview" ...additional parameters... />
note the lowercase "view" or by directly specifying its name
<yourpackage.yourclass.yourview ...additional parameters... />
How to create and access custom components is discussed here
http://developer.android.com/guide/topics/ui/custom-components.html