Create custom widgets for android - android

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

Related

Layout as a subset of another layout in android

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

Add custom XML properties to default android views

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

How to use android custom component dynamic attr

I made a custom component, piechart and in the GraphicalView.class, I set this in this class
Value=attrs.getAttributeIntValue(null, "GO", 80);
So now, I can add a component by put these code in xml
<view
class="org.piechart.GraphicalView"
android:id="#+id/piechart"
GO="48"
/>
but in this situation, i have to dynamic modify the piechart value in Activity,
i hope i can use
View view1= (View)findViewById(R.id.piechart); view1.set ????
but i have no idea how to modify the value
i have to use xml to put a component to layout,and i have to dynamic modify the the component value

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 add children to an android custom view / layout in xml?

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>

Categories

Resources