How to strike specific text in Spinner android?

140 Views Asked by At

I want to strike specific text in Spinner, My code look like below code

    packSpinner = findViewById(R.id.packSpinner);
    String spinnerArray[] = new String[10];

    for(int i=0; i<8; i++){
      spinnerArray[i] = packArray[i] + mrpArray[i] + sellingPriceArray[i];
    }

    packSpinner.setAdapter(new ArrayAdapter<String>(getActivity(),                 
    android.R.layout.simple_list_item_1, spinnerArray));

In above code products coming from server and i already extracted that data into packArray, mrpArray and sellingPriceArray, now i want to mrp text should be striked.

2

There are 2 best solutions below

0
On BEST ANSWER

you can use StrikethroughSpan or just set textView.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); but for both you must have access to TextView, so you can't achieve your purposes with defaut ArrayAdapter, it was designed to show simple lists without custom effects (most common usage). you have to extend it and override getView method. some examples HERE, HERE and HERE

0
On

You can do using

textview.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);

Adapter Specific location

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    R.layout.spinner_text, list) {
                @Override
                public boolean isEnabled(int position) {
                    if (position == 0) {

                        return false;
                    } else {
                       
                    }
                }

                @Override
                public View getDropDownView(int position, View convertView, ViewGroup parent) {
                    View view = super.getDropDownView(position, convertView, parent);
                    TextView textview = (TextView) view;


                    if (position == 0) {
                        textview.setPadding(0, 0,0,0);
                        textview.setTextColor(ContextCompat.getColor(RegistrationActivity.this,R.color.textColor_gray));
                        textview.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    } else {
                        textview.setPadding((int) getResources().getDimension(R.dimen.paddingformleft), (int) getResources().getDimension(R.dimen.paddingform), (int) getResources().getDimension(R.dimen.paddingform), (int) getResources().getDimension(R.dimen.paddingform));

                        textview.setTextColor(ContextCompat.getColor(RegistrationActivity.this,R.color.dark_light_txtColor));
                    }



                    return view;
                }
            };

            dataAdapter.setDropDownViewResource(R.layout.spinner_text);
            spinner.setAdapter(dataAdapter);