I have class, which extends LinearLayout, in it there are Buttons and a Spinner.
This Object gets included via my layout XML file:
<com.ics.spinn.ComboBox android:id="@+id/myautocombo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:entries="@array/suppliers" />
/>
The array suppliers is defined in strings.xml.
If this component now wouldn't be com.ics.spinn.ComboBox, but a Spinner, Android would
auto-populate the "android:entries" to the Spinner adapter.
I'd like my component com.ics.spinn.ComboBox to behave the same way: to be able to access the array defined via the xml file, so I can supply it to the Spinner inside my component, via:
ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item, ARRAYINSIDEMYXML);
s.setAdapter(a);
I now I could access the array defined in strings.xml DIRECTLY via getResources().getStringArray(R.array.suppliers)
but my code shouldn't know of the name "suppliers", since it shall be supplied via android:entries...
This + the entries in xml in João Melo solution WORK:
public ComboBox(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray b = context.obtainStyledAttributes(attrs,
R.styleable.ComboBox, 0, 0);
CharSequence[] entries = b.getTextArray(R.styleable.ComboBox_myEntries);
if (entries != null) {
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(context,
android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
}
}