4

I am trying to display a spinner inside a pop up window. And I want a portion of the text of each drop down item to be green. More specifically, an option would look like : "Transaction Type: Pay Cash, Meet Up". And I want the words "Transaction Type" to be in green and the rest of the words to be in black. I tried the font color attribute, but that is not working.

Code for string-array xml file:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
   <string-array name="payment_list">
    <item><font color="#00ff00">Transaction Type: </font>Pay Cash, Meet Up</item>
    <item><font color="#00ff00">Transaction Type: </font>Credit Card, Meet Up</item>
    <item><font color="#00ff00">Transaction Type: </font>Credit Card, Ship to Me</item>
</string-array>

Code for the pop up window:

final Button makeoffer = (Button) view.findViewById(R.id.make_offer);
    makeoffer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.offer_popup, null);
            final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            EditText mEditText = (EditText) popupView.findViewById(R.id.payment_field);
            popupView.clearFocus();
            mEditText.requestFocus();
            popupWindow.setFocusable(true);
            popupWindow.update();

            final Spinner spinner1 = (Spinner) popupView.findViewById(R.id.transaction_spinner);
            spinner1.setAdapter(new ArrayAdapter<CharSequence>(getActivity(), R.layout.support_simple_spinner_dropdown_item,getActivity().getResources().getTextArray(R.array.payment_list)));

            Button btnDismiss = (Button)popupView.findViewById(R.id.cancel2);
            btnDismiss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            popupWindow.showAtLocation(popupView, Gravity.NO_GRAVITY, 0, 0);
        }
    });
2
  • This question should help you. Commented May 26, 2015 at 19:50
  • @SirGregg I added the CDATA block, but how do I get it to actually render the html attributes in the spinner? Commented May 26, 2015 at 20:05

1 Answer 1

1

For anyone else reading this: Thanks to @SirGregg for pointing me in in the right direction. The first thing I did was add the CDATA block to my string array XML file like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="payment_list">
    <item><![CDATA[<font color="#00ff00">Transaction Type: </font>]]>Pay Cash, Meet Up</item>
    <item><![CDATA[<font color="#00ff00">Transaction Type: </font>]]>Credit Card, Meet Up</item>
    <item><![CDATA[<font color="#00ff00">Transaction Type: </font>]]>Credit Card, Ship to Me</item>
  </string-array>
</resources>

Then, I went back to the code for adding the spinner and I changed it to:

final Spinner spinner1 = (Spinner) popupView.findViewById(R.id.transaction_spinner);
String[] array = getActivity().getResources().getStringArray(R.array.payment_list);
Spanned[] spannedStrings = new Spanned[3];
for(int i=0; i<array.length; i++){
                spannedStrings[i] = Html.fromHtml(array[i]);
            }
spinner1.setAdapter(new ArrayAdapter<CharSequence>(getActivity(), R.layout.support_simple_spinner_dropdown_item,spannedStrings));
Sign up to request clarification or add additional context in comments.

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.