-1

I didn't found any code which meets my requirement .in getview and getdropdown view i applied the font but changes were not reflected in my design please solve my problem thanks in advance.

public class HintSpinnerAdapter  implements SpinnerAdapter, ListAdapter {

    protected static final int PIVOT = 1;

    protected SpinnerAdapter adapter;

    protected Context context;

    protected int hintLayout;

    protected int hintDropdownLayout;

    protected LayoutInflater layoutInflater;

    public HintSpinnerAdapter(
            SpinnerAdapter spinnerAdapter,
            int hintLayout, Context context) {

        this(spinnerAdapter, hintLayout, -1, context);
    }

    public HintSpinnerAdapter(SpinnerAdapter spinnerAdapter,
                              int hintLayout, int hintDropdownLayout, Context context) {
        this.adapter = spinnerAdapter;
        this.context = context;
        this.hintLayout = hintLayout;
        this.hintDropdownLayout = hintDropdownLayout;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        // This provides the View for the Selected Item in the Spinner, not
        // the dropdown (unless dropdownView is not set).
        if (position == 0) {
            return getHintView(parent);
        }
        return adapter.getView(position - PIVOT, null, parent); // Could re-use
                                                 // the convertView if possible.
    }

    protected View getHintView(ViewGroup parent) {
        return layoutInflater.inflate(hintLayout, parent, false);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        // Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
        // Spinner does not support multiple view types
        if (position == 0) {
            return hintDropdownLayout == -1 ?
              new View(context) :
              getHintDropdownView(parent);
        }

        // Could re-use the convertView if possible, use setTag...
        return adapter.getDropDownView(position - PIVOT, null, parent);
    }

    protected View getHintDropdownView(ViewGroup parent) {
        return layoutInflater.inflate(hintDropdownLayout, parent, false);
    }

    @Override
    public int getCount() {
        int count = adapter.getCount();
        return count == 0 ? 0 : count + PIVOT;
    }

    @Override
    public Object getItem(int position) {
        return position == 0 ? null : adapter.getItem(position - PIVOT);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position >= PIVOT ? adapter.getItemId(position - PIVOT) : position - PIVOT;
    }

    @Override
    public boolean hasStableIds() {
        return adapter.hasStableIds();
    }

    @Override
    public boolean isEmpty() {
        return adapter.isEmpty();
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        adapter.registerDataSetObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        adapter.unregisterDataSetObserver(observer);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return position != 0; // Don't allow the 'hint' item to be picked.
    }
}

related question

I followed the answer provided in above link but that didn't worked for me.i didn't understand what to do changes in this code which solve my problem.

Komal12
  • 3,340
  • 4
  • 16
  • 25
vinay
  • 149
  • 7

1 Answers1

0

A simple way around is Make a Font Text View. which will accept font custom attributes .

public class CustomTextView extends AppCompatTextView {
    public CustomTextView(Context context) {
        super(context);
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        handleStyleable(context, attrs);
    }
    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        handleStyleable(context, attrs);
    }
    private void handleStyleable(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
        FONT_VAL font_val= FONT_VAL.NONE;
        try {
            for (FONT_VAL mode : FONT_VAL.values()) {
                if (ta.getInt(R.styleable.CustomFont_typeface, 3) == mode.getId()) {
                    font_val = mode;
                    break;
                }
            }
            if (font_val == FONT_VAL.MEDIUM_FONT) {
                setTypeface(AppUtil.getTypeface(context,"medium.ttf"));
            }else if(font_val== FONT_VAL.REGULAR_FONT){
                setTypeface(AppUtil.getTypeface(context, "regular.ttf"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

Create attributes .

 <declare-styleable name="CustomFont">
    <attr name="typeface" format="enum">
        <enum name="medium_font" value="1" />
        <enum name="regular_font" value="2" />
    </attr>
</declare-styleable>

And an Enum to have font values .

 public enum FONT_VAL {
    NONE(0),MEDIUM_FONT(1), REGULAR_FONT(2);
    private final int ID;
    FONT_VAL(final int id) {
        this.ID = id;
    }
    public int getId() {
        return ID;
    }
}

Then use it in xml font will apply in similar manner you can set fonts to ant textual widget.

 <com.example.views.CustomTextView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="start|center_vertical"
    app:typeface="medium_font"/>

This looks like the long way than setting typeface at runtime but its very useful when there are bunch of views needs to set fonts on .

ADM
  • 20,406
  • 11
  • 52
  • 83