I have created a class CustomPreference
, which inherits from the androidx Preference class, for the purpose of setting a custom layout made of my own components. And I defined a styleable attr on that custom Preference class. But now I'm trying to understand how to pass along the value from that custom styleable attr on to the attribute on my underlying SpecialCustomComponent
.
CustomPreference.java
public class CustomPreference extends Preference {
private Context context;
private AttributeSet attrs;
public CustomPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context = context;
this.attrs = attrs;
setLayoutResource(R.layout.preference_widget_custom);
}
// other required constructor overloads omitted
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
// calling this method here results in an error because
// a.getResourceId(R.styleable.PreferenceWidgetCustom_preferenceTitle, 0)
// returns no resource Id at this point
setPreferenceTitle(holder);
}
@Override
protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
super.onAttachedToHierarchy(preferenceManager);
this.preferenceManager = preferenceManager;
// alternatively, calling this method here would result in a
// different error, since I don't have access to the viewHolder
// here. To make this work, I tried preserving "viewHolder" in a
// field inside onBindViewHolder (which I don't think I'm
// supposed to do anyways), but the viewHolder reference was
// null, so it didn't work.
setPreferenceTitle();
}
// This is my incorrect attempt at setting "myText" to the value of "preferenceTitle"
private void setPreferenceTitle(PreferenceViewHolder holder) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PreferenceWidgetSwitchCustom, 0, 0);
try {
int preferenceTitleId = a
.getResourceId(R.styleable.PreferenceWidgetCustom_preferenceTitle, 0);
((SpecialCustomComponent) holder.itemView).setText(a.getResources().getString(preferenceTitleId));
}
finally {
a.recycle();
}
}
}
I declared a custom attribute for my CustomPreference in attrs.xml that I am hoping to use to set the underlying "myText" attribute of my custom component in the layout.
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PreferenceWidgetCustom">
<attr name="preferenceTitle" format="string" />
</declare-styleable>
</resources>
The layout for each CustomPreference is defined here in preference_widget_custom.xml. Notice the attribute "myText", which is what I would like to set to the value of "preferenceTitle" defined above.
preference_widget_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<SpecialCustomComponent
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:myText="this is the field I would like to be set by 'preferenceTitle'" />
I am using these preferences in a PreferenceScreen layout like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="Settings">
<CustomPreference
android:key="pref_one"
app:preferenceTitle="Preference #1"/>
<CustomPreference
android:key="pref_two"
app:preferenceTitle="Preference #2"/>
</PreferenceScreen>
from How to set view attribute from custom attr on a custom Preference class?
No comments:
Post a Comment