0

I have made a Custom Multiselect Spinner by extending AppCompatSpinner in my project. When I select an item,it shows the selected items. I don't have clue to use typeface for this selected item that is appeared to the user. please anyone enlighten me. How do I use typeface using style? Is it possible?

  <com.sws.ain.helpers.MultiSelectionSpinner
        android:id="@+id/industries_spinner"
        android:layout_width="200dp"
        style="@style/mySpinnerItemStyle"
        android:layout_height="40dp"
        android:layout_below="@+id/select_country_btn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:paddingBottom="10dp"
        android:background="@drawable/interested_button_shape"
        android:gravity="center"
        android:textAlignment="center" />

Here is the Custom Spinner which works fine.

public class MultiSelectionSpinner extends AppCompatSpinner implements
    OnMultiChoiceClickListener {
String[] _items = null;
boolean[] mSelection = null;
boolean[] mSelectionAtStart = null;
String _itemsAtStart = null;
ArrayAdapter<String> simple_adapter;
private OnMultipleItemsSelectedListener listener;

public MultiSelectionSpinner(Context context) {
    super(context);

    simple_adapter = new ArrayAdapter<>(context,
            R.layout.custom_spinner);

    super.setAdapter(simple_adapter);
}

public MultiSelectionSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);

    simple_adapter = new ArrayAdapter<>(context,
            R.layout.custom_spinner);
    super.setAdapter(simple_adapter);
}

public void setListener(OnMultipleItemsSelectedListener listener) {
    this.listener = listener;
}

public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (mSelection != null && which < mSelection.length) {
        mSelection[which] = isChecked;
        //simple_adapter.clear();
        //simple_adapter.add(buildSelectedItemString());
    } else {
        throw new IllegalArgumentException(
                "Argument 'which' is out of bounds.");
    }
}

@Override
public boolean performClick() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), 
R.style.AlertDialogCustom);
    builder.setTitle("Please select!!!");
    builder.setMultiChoiceItems(_items, mSelection, this);
    _itemsAtStart = getSelectedItemsAsString();
   AlertDialog alertDialog= builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length);
            listener.selectedIndices(getSelectedIndices(), simple_adapter.getItem(0));
            listener.selectedStrings(getSelectedStrings(), simple_adapter.getItem(0));
        }
    }).create();


    //2. now setup to change color of the button
    alertDialog.setOnShowListener( new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface arg0) {
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorPrimary));
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getContext(),R.color.white));
        }
    });

    alertDialog.show();
    return true;
}



public void setItems(List<String> items, String title) {
    _items = items.toArray(new String[0]);
    mSelection = new boolean[_items.length];
    mSelectionAtStart = new boolean[_items.length];
    simple_adapter.clear();
    simple_adapter.add(title);
    Arrays.fill(mSelection, false);
    mSelection[0] = false;
}

public void setSelection(String[] selection) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
        mSelectionAtStart[i] = false;
    }
    for (String cell : selection) {
        for (int j = 0; j < _items.length; ++j) {
            if (_items[j].equals(cell)) {
                mSelection[j] = true;
                mSelectionAtStart[j] = true;
            }
        }
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public void setSelection(List<String> selection) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
        mSelectionAtStart[i] = false;
    }
    for (String sel : selection) {
        for (int j = 0; j < _items.length; ++j) {
            if (_items[j].equals(sel)) {
                mSelection[j] = true;
                mSelectionAtStart[j] = true;
            }
        }
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public void setSelection(int index) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
        mSelectionAtStart[i] = false;
    }
    if (index >= 0 && index < mSelection.length) {
        mSelection[index] = true;
        mSelectionAtStart[index] = true;
    } else {
        throw new IllegalArgumentException("Index " + index
                + " is out of bounds.");
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public void setSelection(int[] selectedIndices) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
        mSelectionAtStart[i] = false;
    }
    for (int index : selectedIndices) {
        if (index >= 0 && index < mSelection.length) {
            mSelection[index] = true;
            mSelectionAtStart[index] = true;
        } else {
            throw new IllegalArgumentException("Index " + index
                    + " is out of bounds.");
        }
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public List<String> getSelectedStrings() {
    List<String> selection = new LinkedList<>();
    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            selection.add(_items[i]);
        }
    }
    return selection;
}

public List<Integer> getSelectedIndices() {
    List<Integer> selection = new LinkedList<>();
    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            selection.add(i);
        }
    }
    return selection;
}

private String buildSelectedItemString() {
    StringBuilder sb = new StringBuilder();
    boolean foundOne = false;

    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            if (foundOne) {
                sb.append(", ");
            }
            foundOne = true;

            sb.append(_items[i]);
        }
    }
    return sb.toString();
}

public String getSelectedItemsAsString() {
    StringBuilder sb = new StringBuilder();
    boolean foundOne = false;

    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            if (foundOne) {
                sb.append(", ");
            }
            foundOne = true;
            sb.append(_items[i]);
        }
    }
    return sb.toString();
}

public interface OnMultipleItemsSelectedListener {
    void selectedIndices(List<Integer> indices, String spinnerName);

    void selectedStrings(List<String> strings, String spinnerName);
}

}

Jiten Basnet
  • 1,623
  • 15
  • 31
  • 1
    See also https://stackoverflow.com/questions/6093404/customizing-spinner-font and https://stackoverflow.com/questions/52129212/spinner-with-custom-text-font-and-color and https://stackoverflow.com/questions/19150588/android-spinner-change-font-typeface and https://stackoverflow.com/questions/25398296/custom-font-for-spinner-in-android. All of those were found by searching for `spinner fonts site:stackoverflow.com` on DuckDuckGo. – CommonsWare Mar 16 '19 at 16:19
  • Every answer you mention used some Textview for the Spinner items. I have made a custom Spinner. So using typeface on it is the problem. – Jiten Basnet Mar 16 '19 at 16:29
  • "I have made a custom Spinner" -- I do not know what you mean by "a custom Spinner". Perhaps you should edit your question to provide a [mcve], showing us what you mean by "a custom Spinner" and showing where in it you have text that you wish to customize with a different typeface. Usually, a `Spinner` itself has no text -- that text is provided by the `SpinnerAdapter`, and the duplicate questions show how to create a custom adapter where you set the typeface. – CommonsWare Mar 16 '19 at 16:33
  • Alright I think I got the clue. I need to use typeface for the textview used here simple_adapter = new ArrayAdapter<>(context, R.layout.custom_spinner); Thankyou :) – Jiten Basnet Mar 16 '19 at 16:44
  • Yes. Also, with a `Spinner`, there are usually two row layouts: one for the closed state and one for the open state. In many cases, you will want to have the typeface the same for both. – CommonsWare Mar 16 '19 at 16:54

0 Answers0