0

Is it possible to apply attributes of one view that is defined via xml to other view that is creating programmatically?

In my case situation is next -

There is custom view FormField:

class FormField extends LinearLayout { ... @Override protected void onFinishInflate() { super.onFinishInflate(); final int childCount = getChildCount(); if (childCount > 1) { throw new IllegalStateException("FormField can store only one child view"); } labelView = new TextView(getContext()); addView(labelView, 0); } 

with next target usage:

<com.korovyansk.android.views.FormField style="@style/Label" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Field label" android:layout_marginBottom="300dp" android:layout_marginRight="16dp" android:layout_marginLeft="16dp"> <com.korovyansk.android.views.EditText style="@style/TextField" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textCapSentences" android:imeOptions="actionNext"/> </com.korovyansk.android.views.FormField> 

So the idea is to transfer attributes (style, text, margins) from FormField to TextView created dynamically in onFinishInflated method.

6
  • In the constructor of FormField you get the attributes of the view and you could then, later, apply them to the TextView. Commented Jul 9, 2014 at 8:40
  • @Luksprog I tried it, it works good for layout_margin* parameters, but doesn't work for text, not sure about style. Commented Jul 9, 2014 at 8:46
  • How do you try to get the text attribute? context.obtainStyledAttributes() should allow you to get the text. Commented Jul 9, 2014 at 9:05
  • @Luksprog I tried to save AttributeSet that are passed to constructor of TextField, and later construct TextView with TextView(Context, AttributeSet) constructor. Commented Jul 9, 2014 at 9:09
  • Looks like somewhere parameters are filtered, but I cannot find exact place in the source of Android. Commented Jul 9, 2014 at 9:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.