Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style issue when overriding getDropDownView in ArrayAdapter

I'm experiencing a strange behavior when overriding getDropDownView method of ArrayAdapter. I need to override this method in order to show correct string value out from my custom object. Thats how my array adapter look like:

ArrayAdapter<NoteType> adapter = new ArrayAdapter<NoteType>(this, android.R.layout.simple_spinner_item, lstNoteTypes){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView lbl = (TextView) super.getView(position, convertView, parent);
        lbl.setText(getItem(position).getName());
        return lbl;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView lbl = (TextView) super.getView(position, convertView, parent);
        lbl.setText(getItem(position).getName());
        return lbl;
    }
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

So when I override getDropDownView, my Spinner looks as below - the item height is very small in size and which is not what I want:

enter image description here

But when I comment (or don't override) the getDropDownView method then it looks fine with the default styling but then I can't inject the required text value into drop down items. enter image description here

Note the height of items in both images just because of overriding getDropDownView.

Any suggestions? or what am I missing in my code?

like image 332
waqaslam Avatar asked Sep 19 '13 13:09

waqaslam


4 Answers

If you wrote NoteType yourself, override toString() in it. Just add the following to your NoteType-class:

@Override
public String toString() {
    return getName();
}
like image 53
PKeno Avatar answered Nov 08 '22 07:11

PKeno


I think I might know why this happens, but I have to test it, so for now here is another (quick) solution.

Since the Spinner calls toString() on every object in the ArrayAdapter, you could override the toString() method in your NoteType class and let it return your String (instead of the default toString() implementation). This way you wouldn't have to override getDropDownView() in your adapter, but still have the default styling and your data in it.

like image 31
Ahmad Avatar answered Nov 08 '22 05:11

Ahmad


I ran into this same problem with a custom BaseAdapter class. This was also mentioned in a comment, but the solution is actually really simple -- just add padding to the TextView you return from the getDropDownView method.

You shouldn't need to add any extra files for this and I was not using ActionBarSherlock (just the default Spinner), so I don't think it's related to that. Here is the code that worked for me, adapted to your example:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Create custom TextView
    TextView lbl = (TextView) super.getView(position, convertView, parent);
    lbl.setText(getItem(position).getName());

    // Add padding to the TextView, scaled to device
    final float scale = context.getResources().getDisplayMetrics().density;
    int px = (int) (10 * scale + 0.5f);
    lbl.setPadding(px, px, px, px);

    return lbl;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // Create custom TextView
    TextView lbl = (TextView) super.getView(position, convertView, parent);
    lbl.setText(getItem(position).getName());

    // Add padding to the TextView, scaled to device
    final float scale = context.getResources().getDisplayMetrics().density;
    int px = (int) (10 * scale + 0.5f);
    lbl.setPadding(px, px, px, px);

    return lbl
}
like image 34
Sam Avatar answered Nov 08 '22 05:11

Sam


Had to do the same thing so made my own view to be used as checkedTextView

in this the most important thing is setting the height like so android:layout_height="?attr/dropdownListPreferredItemHeight" I was using a base adapter

like image 1
Salil Kaul Avatar answered Nov 08 '22 06:11

Salil Kaul