1

I want implement spinner with below 2 requests:
1. While show select items list, the text align left as below Picture.4.
2. After select one item, I want to let the selected one show align right as below Picture.1.
My spinner layout spinner layout below:

                        <Spinner
                            android:id="@+id/spinner1"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:textColor="#000000"
                            android:gravity="right"
                            android:textAppearance="?android:attr/textAppearanceMedium" />

                    </RelativeLayout>

And Adapter's getView as below:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewTag viewTag;
    if(convertView == null) {
        convertView = myInflater.inflate(R.layout.row_spinner, null);
        viewTag = new ViewTag((TextView)convertView.findViewById(R.id.textView1));
        convertView.setTag(viewTag);
    }
    else {
        viewTag = (ViewTag)convertView.getTag();
    }

    viewTag.line1.setText(gd.Lang.get(position));
    viewTag.line1.setTextColor(Color.BLACK);
    viewTag.line1.setSelected(true);
    viewTag.line1.setPadding(0, 0, 40, 0);
    viewTag.line1.setGravity(Gravity.RIGHT);

    return convertView;
}

But it will show as Picture.1 and Picture.2.
If I change line viewTag.line1.setGravity(Gravity.RIGHT); to viewTag.line1.setGravity(Gravity.LEFT);.
It will show as Picture.3 and Picture.4.
How can I modify to arrive Picture.1 and Picture.4 at the same time?

Picture.1
enter image description here
Picture.2
enter image description here
Picture.3
enter image description here
Picture.4
enter image description here

brian
  • 6,802
  • 29
  • 83
  • 124

3 Answers3

2

You need to set getView() and getDropDownView() in your adapter.

getView() will set the layout for your picture 1, and getDropDownView() sets the -as the name says- drop down view for your picture 4.

Check this well written answer

Community
  • 1
  • 1
Julio_oa
  • 570
  • 6
  • 15
1

You handle all of these logics in the adapter class, in your getView function the view that you return would have the required text alignment. Use a abase adapter and functions when item clicked store that position in your adapter class. and in your getview based on the position you can have any logic for alignment.

Pulkit Sethi
  • 1,325
  • 1
  • 14
  • 22
  • Can more detail about how to detect items list or selected item in getView function? – brian Jul 26 '13 at 02:05
  • in your spinner_itemClick function, you will get the clicked item position. inside this function call this adapter.setPosition(position). In your adapter class create a private variable selPos. Set selPos = position; (this is the content of setPosition() function. then in getView use the selPos to do your logice. Hard for me to write code as writing this from phone. for e.g in your getView function do if(selPos == 1) // rightalignment if (selPos == 2), left alignment – Pulkit Sethi Jul 26 '13 at 02:12
1

If we look at the following code, we have name and value array in getView() and getDropDownView().

private void initView() {
    SpinnerDropDownAdapter sddadapter = new SpinnerDropDownAdapter(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, sddadapter.name);

    Spinner getViewSP = (Spinner) findViewById(R.id.getview_sp);
    getViewSP.setAdapter(adapter);

    Spinner getViewWDropDownSP = (Spinner) findViewById(R.id.getview_w_drop_down_sp);
    getViewWDropDownSP.setAdapter(sddadapter);
}

static class SpinnerDropDownAdapter extends BaseAdapter implements
        SpinnerAdapter {
    Context context;

    SpinnerDropDownAdapter(Context ctx) {
        context = ctx;
    }

    String[] name = { " One", " Two", " Three", " Four", " Five", " Six",
            " Seven", " Eight" };
    String[] value = { " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };

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

    @Override
    public String getItem(int pos) {
        // TODO Auto-generated method stub
        return name[pos];
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(name[position]);
        return text;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(value[position]);
        return text;
    }
}
darkheir
  • 8,844
  • 6
  • 45
  • 66
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59