1

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);
            }
}
1
  • Your TypedArray is defined in the attrs.xml file inside res/values folder. Check my answer below. Commented Dec 6, 2012 at 17:36

3 Answers 3

1

I don't know if it's possible to do that with android:entries attribute unless your component extends Spinner, but I'm only guessing.

You can achieve that creating your own custom attribute in attrs.xml

<declare-styleable name="ComboBox">
    <attr name="myEntries" format="reference"></attr>
</declare-styleable>

Then you can access this reference (int) inside your component and set the ArrayAdapter into your spinner.

TypedArray customAttrs = context.obtainStyledAttributes(attrs, R.styleable.ComboBox);
    for (int i = 0; i < customAttrs.length(); i++) {
        int attrValue = customAttrs.getIndex(i);
        switch (attrValue) {
            case R.styleable.ComboBox_myEntries:
                mArrayId = customAttrs.getResourceId(attrValue, 0);
                ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item, mArrayId);
                s.setAdapter(a);
                break;
        }
    }

On your layout, add this line xmlns:app="http://schemas.android.com/apk/res/yourPackageName" below xmlns:android="http://schemas.android.com/apk/res/android" in the root view:

Then you can instantiate your component and custom attrs via xml:

<com.ics.spinn.ComboBox android:id="@+id/myautocombo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
app:myEntries="@array/suppliers" />
/>

Don't know if this answer is exactly what you're looking for but it would behave just like android:entries. Hope it helps.

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

1 Comment

Thank you, with your help I was able to solve the problem, see above. Only downer is the entry in attr.xml + that it isn't called android:entries...
0

Try to load your array like this:

String[] array = getResources().getStringArray(R.array.recipes_string_array);

ArrayAdapter spinnerAdapter = new ArrayAdapter<String>(getActivity(), R.layout.simple_spinner_dropdown_item) {

    @Override
    public int getCount() {
       return array.length;
    }

    @Override
        public String getItem(int position) {
        return array[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Create your item for the spinner and return it         
        return spinnerItemview; 
    }

}

spinner.setAdapter(spinnerAdapter);

1 Comment

He's trying to get the array from the android:entries attribute of the custom component.
0

Joala's answer above is technically correct, but there is a much simpler way of expressing it.

Instead of iterating over the StyledAttributes you can just ask for the resourceId of the StringArray directly.

// Get the DisplayValues from the XML config.
final TypedArray customAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.ComboBox);
final int resourceId = customAttrs.getResourceId(R.styleable.ComboBox_myEntries, -1);
if (resourceId == -1) {
    throw new IllegalArgumentException("ComboBox requires a myEntries attribute that points to a string-array resource");
}

final ArrayAdapter<String> a = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_dropdown_item, resourceId);
s.setAdapter(a);

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.