1

I have structure like that:

preferences.xml:

...

<com.example.MyCustomPreference
    ...
    myCustomMessage="@string/abc"
    android:inputType="..."
    ... />

...

preference_my_custom.xml:

<LinearLayout ...>

    <com.example.MyCustomView
        ...
        app:myCustomMessage="?????"
        ... />

</LinearLayout>

view_my_custom.xml:

<GridView ...>
    ...EditTexts, TextViews, etc.
</GridView>

I would like to pass myCustomMessage's value (I omitted other attributes for simplification) from MyCustomPreference to MyCustomView using XML. MyCustomView reads custom attributes, so I would like to avoid reading attributes in MyCustomPreference programmatically, getting TextViews from MyCustomView and setting them values. However, I really don't know what to type in place of "?????".

How can i do this using XML? Is this possible?

4
  • Possible duplicate of Include layout with custom attributes Commented May 29, 2018 at 12:13
  • I've seen it. I think my question is slightly different. I don't use <include>, and I don't want to use a reference to a resource, but a value which has already passed as an attribute. Commented May 29, 2018 at 12:27
  • 1
    Your question is slightly different but as far as I can tell the solution is the same (Data Binding) Commented May 29, 2018 at 12:37
  • Happy to help :) Commented May 30, 2018 at 11:44

2 Answers 2

2

You have to do it programmatically (unless you use data binding). For example, in your MyCustomPreference you catch de attribute myCustomMessage:

String myCustomMessage = null;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomPreference, 0, 0);
try {
  myCustomMessage = a.getString(R.styleable.MyCustomPreference_myCustomMessage);
} finally {
  a.recycle();
}

Here you got the String value of your attribute. Then, I supose you have inflated your MyCustomView inside your MyCustomPreference. As an example:

View.inflate(getContext(), R.layout.preference_my_custom, this);
MyCustomView myCustomView = (MyCustomView) findViewById(R.id.you_custom_view_id);

So, here you can set programmatically your myCustomMessage in your MyCustomView.

myCustomView.setMyCustomMessage(myCustomMessage);

You should create this method to set correctly your text, and if necessary propagate this text to other child views of your MyCustomView.

Now, changing your String resId in your preferences.xml the interface should update as expected.

P.S: Since I don't know all your resource ids, please adapt them to your project.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer and accurate explanation. Maybe there is no way to do it as simply as I expected, but this solution works perfectly and you have also mentioned data binding, so I can accept your answer.
Glad to hear that! :)
1

Create an attribute file for your customeView:

Add in attrs.xml

<declare-styleable name="CustomView">
    <attr name="width" format="dimension" />
</declare-styleable>

Used in your customView init:

 public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView, defStyle, 0);
    mWidth = a.getDimensionPixelSize(R.styleable.CustomView_width,0);
    a.recycle();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.