Reputation: 596
I'm trying to make a compound view, whereby I can set attributes in XML and have them be passed to the children in the compound view. In the code below, I want to set the android:text
and have it passed to the EditText
.
Is this possible without having to set every attribute as a custom attribute?
Activity.xml:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.app.CustomLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="child_view_text" />
</FrameLayout>
custom_view.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.TextInputLayout>
</merge>
CustomView.java:
public ValidationTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
View v = inflate(context, R.layout.custom_view, this);
mEditText = (EditText) v.findViewById(R.id.editText);
mTextInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
}
Upvotes: 15
Views: 2806
Reputation: 270
Instead of having the EditText inside CustomLayout.xml you can instantiate it in CustomView.java passing the AttributeSet:
public class ValidationTextInputLayout extends LinearLayout
{
public ValidationTextInputLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs)
{
View v = inflate(context, R.layout.custom_view, this);
mTextInputLayout = (TextInputLayout)findViewById(R.id.textInputLayout);
mEditText = new EditText(context, attrs);
mTextInputLayout.AddView(mEditText);
}
/* Properties etc... */
}
For anyone still having this issue.
Upvotes: 4
Reputation: 5880
com.app.CustomLayout extends a Layout, meaning it can't take android:text as an attribute. You'll need to create a custom attribute for your CustomLayout class and in said CustomLayout class, inflate your custom_view (as you're doing above), parse the attrs for your custom text string, then call mEditText.setText(customLayoutText).
Upvotes: 0
Reputation: 1201
I think probably you can put the custom text (i.e., child_view_text) in a theme, then use the theme on the parent layout or view. With doing that, all child views will have the attribute android:text passed in with the custom text.
In your case, it might look like:
<string name="child_view_text">Child View Text</string>
<style name="CustomTheme" parent="Theme.AppCompat">
<item name="android:text">@string/child_view_text</item>
</style>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/CustomTheme"/>
<com.app.CustomLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Upvotes: 0