xleon xleon - 4 years ago 101
Android Question

Android xml global custom tag without subclassing

I know how to create a custom style attribute definition and get the value in a custom view (from this link):

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0);
String str = a.getString(R.styleable.MyCustomView_my_custom_attribute);

But I´m wondering if it´s possible to define a "global" tag that I can apply to any view, either custom or from Android SDK.

Let´s say I want to execute a static method to the views containing that attribute, but avoiding any reference to the view (like FindViewById):

whatever:custom_global_tag="key" />

MyStaticClass.Process(View view, String key) {}

Is this even possible?


Just to clarify, what I really need is a string. The goal is transforming an string into another and assign the new value to the text property.

My env is Xamarin but any native approach could be translated to Xamarin so all suggestions are welcome

Answer Source

Try Theme?

A theme is a style applied to an entire Activity or application, rather than an individual View (as in the example above). When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports.

Note: just work for property the view supports.

Edit #1

if you want to pass a different string on each view, just use android:tag

something like:

    android:tag="key" />

and get the string

String key = textView.getTag();

Edit #2

set a tag to identify which view you want.

in the activity, iterate all the views to find the identified view like

final ViewGroup viewGroup = (ViewGroup)((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
for(int i = 0; i < viewGroup.getChildCount(); i++) {
    View childView = viewGroup.getChildAt(i);
    if ("identificaton".equals(childView.getTag())) {
        // do some work to child view.

the viewGroup part can check the post

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download