How to use android custom component dynamic attr - android

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

Related

Is there a way to read a view attribute via databinding during view creation?

I'm using databinding and I'm trying to compose some settings. I have a view whose layout I want to be determined via a custom attribute (passing in an enum). For example passing in either Setting.CHECKBOX or Setting.SWITCH should let me inflate the appropriate control for the view. The view would be defined like this:
<data>
<import type="com.mypackage.ui.SettingView.SettingControl" />
<SettingView
android:id="#+id/setting_foo
android:layout_width="match_parent"
android:layout_height="wrap_content"
setting:control="#{Setting.CHECKBOX} />
...
Is there a way I can read that setting:control attribute before the layout is inflated? Based on the setting, I want to specify how to inflate the SettingView.
I know I can do it via custom attributes/styleables, but I was wondering if it could be achieved via databinding.
No. In order to look at the attribute value during inflation, you can't use data binding syntax. Data binding will strip the value from the XML and add it to the generated binding code.
Instead, add the attribute to the attrs.xml file and assign the value using the normal syntax. You can then read the attribute using context.obtainedStyledAttributes() and accessing the value through the TypedArray.

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

Create custom widgets for 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

How do I used values declared in XML on my custom layout?

Im creating a custom layout and I want to use the text declared in the layout.xml file in my layout.
Like when I create a TextView in XML and set android:text=#string/text1 when I run the app text view automatically loads the text from android:text. So how can I access attributes declared in the layout XML file in my custom component.
I have learn how to do it from this:
WebImageView: Buy one, get two!
In simple steps:
Define the string of your XMLS.
Get the attributes in the code.
Did you complete the Android Developer Tutorials? The Hello, Views | Relative Layout tutorial will walk you through how to do this.

Categories

Resources