You can create custom preference with additional attributes, in your case with limit
attribute.
res/values/attrs.xml
<declare-styleable name="CustomPreference">
<attr name="limit" format="integer"/>
</declare-styleable>
CustomPreference.java
public class CustomPreference extends DialogPreference {
private int limit;
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomPreference, 0, 0);
limit = a.getInteger(R.styleable.CustomPreference_limit, 0);
a.recycle();
}
In preference layout:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.package">
<PreferenceCategory>
<com.example.package.CustomPreference
android:key="@string/pref_key"
android:title="@string/pref_title"
app:limit="6" />
</PreferenceCategory>
</PreferenceScreen>
Also you can set an attribute value programmatically