androidfontsbundletypeface

How to pass Typeface through bundle?


I'm wondering how to put typeface through bundle ?

It seems typeface isn't parcelable or serializable so it can't be put in bundle.

I've found the tips of pass the typeface's hashcode with putInt() and getInt() but I don't know if it's a clean code.

Sorry for my bad english, and thank you for your help

EDIT:

Here are some details to understand what I search to do :

I've two fragment :

  1. In the first I've an EditText with a Typeface that I can change.
  2. In the second, I've a recyclerView with a list of Typeface.

My purpose is to get the typeface of the EditText from the 1st fragment in the 2nd fragment, to highlight the correct Typeface in the list.

I succeeded to do this by sending the hashcode of the typeface. But I'm not feeling that is a good practice.


Solution

  • In first fragment where the edittext is present, put

    String selectedEditTextFont = "Roboto-Medium.ttf";

    mEditText.setFont(selectedEditTextFont);

    public class ForexTextView extends AppCompatEditText { 
    
        public void setFont(String fontName) {
            Typeface myTypeface = Typeface.createFromAsset(
                    getContext().getAssets(), "fonts/" + fontName);
            setTypeface(myTypeface);
        }
    }
    

    Now, selectedEditTextFont string has the final selectedFont value. You need to pass it through bundle.

     Bundle bundle = new Bundle();
     bundle.putString("SelectedFont", selectedEditTextFont);
     Fragment fragment = SecondFragment.newInstance();
    

    In the second fragment where the recyclerview is located you need to check with this selectedEditTextFont like this in adapter class.

    if(selectedEditTextFont.equals(arrRecyclerViewRow.get(ADAPTER_POSITION_HERE).getFontType){
    //To highlight particular row
    lLaylist.setBackGroundColor(getResources.getColor(R.color.yellow));
    }
    

    You need to make setFontType with associated list and need to put it into model class. So you can getFontType() in adapter class.