Recent

Author Topic: LAMW - spinner set custom font from asset addition  (Read 2974 times)

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
LAMW - spinner set custom font from asset addition
« on: November 10, 2023, 05:24:49 am »
 
Code: Java  [Select][+][-]
  1. package org.monyet.wedus;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.content.Context;
  6. import android.graphics.Color;
  7. import android.graphics.PorterDuff;
  8. import android.graphics.Typeface;
  9. import android.util.TypedValue;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.AdapterView;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.Spinner;
  15. import android.widget.TextView;
  16. import android.view.Gravity;
  17.  
  18. //-------------------------------------------------------------------------
  19. // jSpinner
  20. // Reviewed by ADiV on 2022/03/28
  21. //-------------------------------------------------------------------------
  22.  
  23. class CustomSpinnerArrayAdapter<T> extends ArrayAdapter<String>{
  24.  
  25.     Context ctx;
  26.     private int mTextColor = Color.BLACK;
  27.     private int mTexBackgroundColor = Color.TRANSPARENT;
  28.     private int mSelectedTextColor = Color.BLACK;  
  29.     private int flag = 0;
  30.     private boolean mLastItemAsPrompt = false;
  31.     private int mTextFontSize = 0;
  32.     private int mTextSizeTypedValue;
  33.  
  34.     private int mTextAlignment;
  35.     private Typeface mFontFace;
  36.     private int mFontStyle;
  37.        
  38.     private int mSelectedPadTop = 15;
  39.     private int mSelectedPadBottom = 5;
  40.    
  41.     public CustomSpinnerArrayAdapter(Context context, int simpleSpinnerItem, ArrayList<String> alist) {
  42.         super(context, simpleSpinnerItem, alist);        
  43.         ctx = context;
  44.         mTextSizeTypedValue = TypedValue.COMPLEX_UNIT_SP;
  45.         mTextAlignment = Gravity.CENTER;        
  46.     }
  47.  
  48.     public void SetFontSizeUnit(int _unit) {
  49.         switch (_unit) {
  50.             case 0: mTextSizeTypedValue = TypedValue.COMPLEX_UNIT_SP; break; //default
  51.             case 1: mTextSizeTypedValue = TypedValue.COMPLEX_UNIT_PX; break;
  52.             case 2: mTextSizeTypedValue = TypedValue.COMPLEX_UNIT_DIP; break;
  53.             case 3: mTextSizeTypedValue = TypedValue.COMPLEX_UNIT_MM; break;
  54.             case 4: mTextSizeTypedValue = TypedValue.COMPLEX_UNIT_PT; break;
  55.             case 5: mTextSizeTypedValue = TypedValue.COMPLEX_UNIT_SP; break;
  56.         }
  57.     }
  58.  
  59.     public void SetTextAlignment(int _alignment) {
  60.         mTextAlignment = _alignment;
  61.     }
  62.  
  63.     public void SetFontAndTextTypeFace(Typeface fontFace, int fontStyle) {
  64.         mFontFace = fontFace;
  65.         mFontStyle = fontStyle;
  66.     }
  67.  
  68.     public void SetFont(Typeface fontFace) {
  69.         mFontFace = fontFace;
  70.     }
  71.  
  72.  
  73.     //This method is used to display the dropdown popup that contains data.
  74.     @Override
  75.     public View getDropDownView(int position, View convertView, ViewGroup parent)
  76.     {
  77.        
  78.         View view = super.getView(position, convertView, parent);
  79.  
  80.         //we know that simple_spinner_item has android.R.id.text1 TextView:
  81.         //TextView text = (TextView)view.findViewById(android.R.id.text1);
  82.  
  83.         ((TextView) view).setPadding(20, mSelectedPadTop+15, 20, mSelectedPadBottom+15);  //padTop, padBottom
  84.         ((TextView) view).setTextColor(mTextColor);
  85.         ((TextView) view).setBackgroundColor(mTexBackgroundColor);
  86.        
  87.         if (mTextFontSize != 0) {
  88.  
  89.             if (mTextSizeTypedValue != TypedValue.COMPLEX_UNIT_SP)
  90.                 ((TextView) view).setTextSize(mTextSizeTypedValue, mTextFontSize);
  91.             else
  92.                 ((TextView) view).setTextSize(mTextFontSize);
  93.         }
  94.        
  95.         ((TextView) view).setTypeface(mFontFace, mFontStyle);
  96.         ((TextView) view).setGravity(mTextAlignment);
  97.         //((TextView) view).setGravity(Gravity.CENTER);
  98.  
  99.         return view;
  100.     }
  101.  
  102.     //This method is used to return the customized view at specified position in list.
  103.     @Override
  104.     public View getView(int pos, View cnvtView, ViewGroup prnt) {
  105.  
  106.         View view = super.getView(pos, cnvtView, prnt);
  107.  
  108.         ((TextView)view).setPadding(20, mSelectedPadTop, 20, mSelectedPadBottom+5);        
  109.         ((TextView)view).setTextColor(mSelectedTextColor);        
  110.         ((TextView) view).setBackgroundColor(mTexBackgroundColor);
  111.        
  112.         if (mTextFontSize != 0) {
  113.  
  114.             if (mTextSizeTypedValue != TypedValue.COMPLEX_UNIT_SP)
  115.                 ((TextView) view).setTextSize(mTextSizeTypedValue, mTextFontSize);
  116.             else
  117.                 ((TextView) view).setTextSize(mTextFontSize);
  118.         }
  119.  
  120.         ((TextView)view).setTypeface(mFontFace, mFontStyle);
  121.         ((TextView)view).setGravity(mTextAlignment);
  122.        
  123.         if (mLastItemAsPrompt) flag = 1;
  124.  
  125.         return view;
  126.     }
  127.  
  128.     @Override
  129.     public int getCount() {
  130.         if (flag == 1)
  131.             return super.getCount() - 1; //do not show last item
  132.         else return super.getCount();
  133.     }
  134.  
  135.     public void SetTextColor(int txtColor){
  136.         mTextColor = txtColor;
  137.     }
  138.  
  139.     public void SetBackgroundColor(int txtColor){
  140.         mTexBackgroundColor = txtColor;
  141.     }
  142.  
  143.     public void SetSelectedTextColor(int txtColor){
  144.         mSelectedTextColor = txtColor;
  145.     }
  146.  
  147.     public void SetLastItemAsPrompt(boolean _hasPrompt) {
  148.         mLastItemAsPrompt = _hasPrompt;
  149.     }
  150.  
  151.     public void SetTextFontSize(int txtFontSize) {
  152.         mTextFontSize = txtFontSize;
  153.     }
  154.    
  155.     public void SetSelectedPadTop(int _top) {
  156.         mSelectedPadTop = _top;
  157.     }
  158.      
  159.     public void SetSelectedPadBottom(int  _bottom) {
  160.         mSelectedPadBottom = _bottom;
  161.     }
  162.  
  163. }
  164.  
  165. /*Draft java code by "Lazarus Android Module Wizard" [6/11/2014 22:00:44]*/
  166. /*https://github.com/jmpessoa/lazandroidmodulewizard*/
  167. /*jVisualControl template*/
  168.  
  169. public class jSpinner extends  Spinner { //androidx.appcompat.widget.AppCompatSpinner
  170.  
  171.     private long       pascalObj = 0;    // Pascal Object
  172.     private Controls   controls  = null; // Control Class for events
  173.  
  174.     private Context context = null;
  175.     private Boolean enabled  = true;           // click-touch enabled!
  176.  
  177.     private ArrayList<String>  mStrList;
  178.     private ArrayList<String>  mTagList;
  179.    
  180.     private CustomSpinnerArrayAdapter<String> mSpAdapter;
  181.     private boolean mLastItemAsPrompt = false;
  182.     private int mTextAlignment;
  183.    
  184.     private String mSelectedText  = "";    
  185.     private int    mSelectedIndex = -1;
  186.  
  187.     private int mSelectedPadTop = 15;
  188.     private int mSelectedPadBottom = 5;
  189.    
  190.     private jCommons LAMWCommon;
  191.  
  192.  
  193.     //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ...
  194.     public jSpinner(Controls _ctrls, long _Self) { //Add more others news "_xxx" params if needed!
  195.         super(_ctrls.activity);
  196.         context   = _ctrls.activity;
  197.         pascalObj = _Self;
  198.         controls  = _ctrls;
  199.        
  200.         LAMWCommon = new jCommons(this,context,pascalObj);
  201.  
  202.         mTextAlignment = Gravity.CENTER;
  203.         mStrList = new ArrayList<String>();
  204.         mTagList = new ArrayList<String>();
  205.         mSpAdapter = new CustomSpinnerArrayAdapter<String>(context, android.R.layout.simple_spinner_item, mStrList);
  206.  
  207.         mSpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  208.  
  209.         setAdapter(mSpAdapter);
  210.         setOnItemSelectedListener(spinnerListener);
  211.        
  212.     } //end constructor
  213.  
  214.     public void jFree() {
  215.         //free local objects...
  216.         mStrList = null;
  217.         mTagList = null;
  218.         setOnItemSelectedListener(null);
  219.         mSpAdapter = null;
  220.         LAMWCommon.free();
  221.     }
  222.  
  223.     //implement action listener type of OnItemSelectedListener
  224.     private OnItemSelectedListener spinnerListener =new OnItemSelectedListener() {
  225.  
  226.         @Override  
  227.    /*.*/public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  228.             ((TextView) parent.getChildAt(0)).setGravity(mTextAlignment); //Gravity.CENTER
  229.            
  230.             String caption = mStrList.get(position).toString();          
  231.             setSelection(position);
  232.            
  233.             mSelectedIndex = position;
  234.             mSelectedText  = caption;
  235.            
  236.             controls.pOnSpinnerItemSelected(pascalObj,position,caption);
  237.         }
  238.  
  239.         @Override
  240.    /*.*/public void onNothingSelected(AdapterView<?> parent) { }
  241.  
  242.     };
  243.  
  244.     public void SetViewParent(ViewGroup _viewgroup) {
  245.         LAMWCommon.setParent(_viewgroup);
  246.     }
  247.    
  248.         public ViewGroup GetParent() {
  249.                 return LAMWCommon.getParent();
  250.         }
  251.  
  252.         public void RemoveFromViewParent() {   //TODO Pascal
  253.                 LAMWCommon.removeFromViewParent();
  254.         }
  255.        
  256.     public void SetLParamWidth(int _w) {
  257.         LAMWCommon.setLParamWidth(_w);
  258.     }
  259.  
  260.     public void SetLParamHeight(int _h) {
  261.         LAMWCommon.setLParamHeight(_h);
  262.     }
  263.  
  264.     public void SetLGravity(int _g) {
  265.         LAMWCommon.setLGravity(_g);
  266.     }
  267.  
  268.     public void setLWeight(float _w) {
  269.         LAMWCommon.setLWeight(_w);
  270.     }
  271.  
  272.     public void SetLeftTopRightBottomWidthHeight(int _left, int _top, int _right, int _bottom, int _w, int _h) {
  273.         LAMWCommon.setLeftTopRightBottomWidthHeight(_left,_top,_right,_bottom,_w,_h);
  274.     }
  275.  
  276.     public void AddLParamsAnchorRule(int _rule) {
  277.                 LAMWCommon.addLParamsAnchorRule(_rule);
  278.     }
  279.  
  280.     public void AddLParamsParentRule(int _rule) {
  281.         LAMWCommon.addLParamsParentRule(_rule);
  282.     }
  283.  
  284.     public void SetLayoutAll(int _idAnchor) {
  285.         LAMWCommon.setLayoutAll(_idAnchor);
  286.     }
  287.  
  288.     public void ClearLayoutAll() {
  289.         LAMWCommon.clearLayoutAll();
  290.     }
  291.  
  292.     //write others [public] methods code here......
  293.     //GUIDELINE: please, preferentially, init all yours params names with "_", ex: int _flag, String _hello ..
  294.  
  295.     public int GetSelectedItemPosition() {
  296.         if(mStrList.size() <= 0) return -1;
  297.        
  298.         return mSelectedIndex;    
  299.     }
  300.  
  301.     public String GetSelectedItem() {
  302.         if(mStrList.size() <= 0) return "";
  303.        
  304.         return mSelectedText;
  305.     }
  306.  
  307.     //ELERA_04032015
  308.     public void Clear() {
  309.         mStrList.clear();
  310.         mTagList.clear();
  311.         mSpAdapter.notifyDataSetChanged();
  312.        
  313.         mSelectedText  = "";    
  314.         mSelectedIndex = -1;
  315.     }
  316.  
  317.     public void SetSelectedTextColor(int _color) {         
  318.         mSpAdapter.SetSelectedTextColor(_color);
  319.     }
  320.  
  321.     public void SetDropListTextColor(int _color) {     
  322.          mSpAdapter.SetTextColor(_color);
  323.     }
  324.  
  325.     public void SetDropListBackgroundColor(int _color) {
  326.         mSpAdapter.SetBackgroundColor(_color);
  327.     }
  328.  
  329.     public void SetLastItemAsPrompt(boolean _hasPrompt) {
  330.         mLastItemAsPrompt = _hasPrompt;
  331.         mSpAdapter.SetLastItemAsPrompt(_hasPrompt);
  332.         if (mLastItemAsPrompt) {
  333.             if (mStrList.size() > 0) setSelection(mStrList.size()-1);
  334.         }
  335.     }
  336.  
  337.     public int GetSize() {
  338.         return mStrList.size();
  339.     }
  340.  
  341.     public void Delete(int _index) {
  342.         if (mStrList.size() <= 0) return;
  343.         if ((_index < 0) || (_index >= mStrList.size())) return;
  344.                
  345.         mStrList.remove(_index);
  346.         mTagList.remove(_index);
  347.                  
  348.         mSpAdapter.notifyDataSetChanged();
  349.     }
  350.    
  351.     public void SetSelection(int _index) {
  352.         if (mStrList.size() <= 0) return;
  353.         if ((_index < 0) || (_index >= mStrList.size())) return;
  354.        
  355.         mSelectedIndex = _index;
  356.         setSelection(_index);                                    
  357.     }
  358.    
  359.     public void SetTextFontSize(int _txtFontSize) {
  360.         mSpAdapter.SetTextFontSize(_txtFontSize);
  361.     }
  362.    
  363.    /*
  364.    public void SetChangeFontSizeByComplexUnitPixel(boolean _value) {
  365.            mSpAdapter.SetChangeFontSizeByComplexUnitPixel(_value);
  366.         }
  367.    */
  368.  
  369.     public void SetFontSizeUnit(int _unit) {
  370.         mSpAdapter.SetFontSizeUnit(_unit);
  371.     }
  372.  
  373.     //TTextAlignment = (alLeft, alCenter, alRight);   //Pascal
  374.    
  375.     public void SetTextAlignment(int _alignment) {
  376.                
  377.         switch(_alignment) {
  378.        
  379.         //[ifdef_api14up]
  380.             case 0 : mTextAlignment = android.view.Gravity.START; break;
  381.             case 1 : mTextAlignment = android.view.Gravity.END; break;
  382.         //[endif_api14up]
  383.            
  384.         /* //[endif_api14up]
  385.             case 0 : mTextAlignment = android.view.Gravity.LEFT; break;
  386.             case 1 : mTextAlignment = android.view.Gravity.RIGHT; break;
  387.            //[ifdef_api14up] */
  388.                        
  389.             case 2 : mTextAlignment = android.view.Gravity.CENTER; break;
  390.            
  391.          //[ifdef_api14up]
  392.             default : mTextAlignment = android.view.Gravity.START ; break;
  393.          //[endif_api14up]
  394.          
  395.          /* //[endif_api14up]
  396.             default : mTextAlignment = android.view.Gravity.LEFT; break;
  397.          //[ifdef_api14up] */
  398.            
  399.         }
  400.                
  401.         mSpAdapter.SetTextAlignment(mTextAlignment);
  402.     }
  403.  
  404.     public void SetFontAndTextTypeFace(int _fontFace, int _fontStyle) {
  405.         Typeface t = null;
  406.         switch (_fontFace) {
  407.             case 0: t = Typeface.DEFAULT; break;
  408.             case 1: t = Typeface.SANS_SERIF; break;
  409.             case 2: t = Typeface.SERIF; break;
  410.             case 3: t = Typeface.MONOSPACE; break;
  411.         }
  412.         mSpAdapter.SetFontAndTextTypeFace(t, _fontStyle);
  413.     }
  414.  
  415.  
  416.     public void SetFont(String _fontName) {   //   "font/font.ttf"
  417.         Typeface customfont = Typeface.createFromAsset( controls.activity.getAssets(), _fontName);
  418.         mSpAdapter.SetFont(customfont);
  419.     }
  420.  
  421.     public String GetText() {
  422.         if (mStrList.size() <= 0) return "";
  423.        
  424.         return mSelectedText;
  425.     }
  426.    
  427.     public void SetText(int _index) {
  428.         if (mStrList.size() <= 0) return;
  429.        
  430.         SetSelection(_index);
  431.     }
  432.        
  433.     public void SetSelectedIndex(int _index) {
  434.         if (mStrList.size() <= 0) return;
  435.         if ((_index < 0) || (_index >= mStrList.size())) return;
  436.        
  437.         mSelectedIndex = _index;
  438.         setSelection(_index);          
  439.     }
  440.  
  441.     public int GetSelectedIndex() {
  442.         if (mStrList.size() <= 0) return -1;
  443.        
  444.         return mSelectedIndex;      //or -1  
  445.     }
  446.    
  447.    
  448.     public void SetItem(int _index, String _item) {
  449.         if (mStrList.size() <= 0) return;
  450.         if ((_index < 0) || (_index >= mStrList.size())) return;
  451.                
  452.         mStrList.set(_index,_item);
  453.        
  454.         mSpAdapter.notifyDataSetChanged();      
  455.     }
  456.    
  457.     public void SetItem(int _index, String _item,  String _strTag) {
  458.         if (mStrList.size() <= 0) return;
  459.         if ((_index < 0) || (_index >= mStrList.size())) return;
  460.                
  461.         mStrList.set(_index,_item);
  462.         mStrList.set(_index,_strTag);
  463.        
  464.         mSpAdapter.notifyDataSetChanged();          
  465.     }    
  466.    
  467.     public void Add(String _item, String _strTag, boolean runEvent) {
  468.         mStrList.add(_item);
  469.         mTagList.add(_strTag);
  470.         //Log.i("Spinner_Add: ",_item);
  471.         mSpAdapter.notifyDataSetChanged();
  472.        
  473.         if(mSelectedIndex == -1){
  474.                 mSelectedIndex = mStrList.size() - 1;
  475.                 mSelectedText  = _item;
  476.                
  477.                 if( runEvent )
  478.                  controls.pOnSpinnerItemSelected(pascalObj, mSelectedIndex, mSelectedText);
  479.         }
  480.     }    
  481.    
  482.         public void SetItemTagString(int _index, String _strTag) {
  483.                 if (mStrList.size() <= 0) return;
  484.         if ((_index < 0) || (_index >= mStrList.size())) return;
  485.                        
  486.         mStrList.set(_index,_strTag);                                                  
  487.         }
  488.  
  489.         public String GetItemTagString(int _index){
  490.                 if (mStrList.size() <= 0) return "";
  491.                 if ((_index < 0) || (_index >= mStrList.size())) return "";
  492.                                        
  493.         return mTagList.get(_index);
  494.         }    
  495.    
  496.         public void SetSelectedPaddingTop(int _paddingTop) {       
  497.             mSpAdapter.SetSelectedPadTop(_paddingTop);
  498.         }
  499.        
  500.         public void SetSelectedPaddingBottom(int _paddingBottom) {
  501.             mSpAdapter.SetSelectedPadBottom(_paddingBottom);
  502.         }
  503.  
  504.   /* Pascal:
  505.      TFrameGravity = (fgNone,
  506.                    fgTopLeft, fgTopCenter, fgTopRight,
  507.                    fgBottomLeft, fgBottomCenter, fgBottomRight,
  508.                    fgCenter,
  509.                    fgCenterVerticalLeft, fgCenterVerticalRight
  510.                    );    
  511.    */
  512.    public void SetFrameGravity(int _value) {       
  513.       LAMWCommon.setLGravity(_value);
  514.    }
  515.  
  516.    public void SetColorFilter(int _color) {
  517.        this.getBackground().setColorFilter(_color, PorterDuff.Mode.SRC_ATOP);
  518.    }
  519.  
  520. }  //end class
  521.  
  522.  
  523.  
« Last Edit: November 10, 2023, 05:31:22 am by Mongkey »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: LAMW - spinner set custom font from asset addition
« Reply #1 on: November 17, 2023, 06:29:02 pm »
Hi, Mongkey!

What about  ".pas"  change?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: LAMW - spinner set custom font from asset addition
« Reply #2 on: December 09, 2023, 05:01:47 am »
 :), sorry it was not right, i attach correct unit:

usage:
Code: Pascal  [Select][+][-]
  1.   spinner1.SetFont('Courier-prime.ttf');
  2.  
« Last Edit: December 09, 2023, 11:31:29 am by Mongkey »

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: LAMW - spinner set custom font from asset addition
« Reply #3 on: December 09, 2023, 11:33:11 am »
Have fun  8)

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: LAMW - spinner set custom font from asset addition
« Reply #4 on: December 17, 2023, 05:54:30 pm »

committed!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: LAMW - spinner set custom font from asset addition
« Reply #5 on: December 18, 2023, 04:02:46 am »
Ok

 

TinyPortal © 2005-2018