brian
brian

Reputation: 6912

Spinner usage on Android

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

Upvotes: 1

Views: 233

Answers (3)

Pankaj Arora
Pankaj Arora

Reputation: 10274

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;
    }
}

Upvotes: 1

Julio_oa
Julio_oa

Reputation: 580

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

Upvotes: 2

Pulkit Sethi
Pulkit Sethi

Reputation: 1325

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.

Upvotes: 1

Related Questions