chrisjenx / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
8.59k stars 1.1k forks source link

Fragment Issue with Data Binding #342

Open pratikbutani opened 7 years ago

pratikbutani commented 7 years ago

I saw #29 Fragment Issue for loading fonts. May Its working fine with LayoutInflater like

LayoutInflater mInflater = getActivity().getLayoutInflater();

But I have used Data Binding Library with Fragment so I am inflating view like:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        orderListBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_order_list, container, false);

        // Inflate the layout for this fragment
        return orderListBinding.getRoot();
    }

How can I solve it for Data Binding? Any Help please.

af-abe commented 7 years ago

Hi, i am using databinding too. I dont get this issue. Did you init the lib the right way in the parent activity?

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setFontAttrId(R.attr.fontPath)
                .build()
        );
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
pratikbutani commented 7 years ago

@af-abe Thanks for reply, How did you have used in Fragment. Can you post your Fragment class.

As I mentioned, If I use orderListBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_order_list, container, false); this one to get View in fragment then its not applying styles.

af-abe commented 7 years ago

@pratikbutani in general i used it like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_measure, container, false);
    mBinding = DataBindingUtil.bind(view);
    ...
    return view;
}

but your way also workes for me. tested with 'uk.co.chrisjenx:calligraphy:2.2.0'

pratikbutani commented 7 years ago

@af-abe I dont know whats the problem actually but I am not getting actual font after changing fragment after navigation from NavigationView.

af-abe commented 7 years ago

@pratikbutani did you try this?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = getActivity().getLayoutInflater().inflate(fragment_order_list, container, false);
    orderListBinding = DataBindingUtil.bind(view);
    ...
    return view;
}
pratikbutani commented 7 years ago

@af-abe Yes I tried that code too but its not working for me.

chrisjenx commented 7 years ago

@pratikbutani can you post more full examples of your setup as there is not enough to go off on here. Thanks.

deshario commented 7 years ago

How to use this function on a fragment !

@Override protected void attachBaseContext(Context newBase) { // Set Default Font super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); }

Can you guyz provide more information ! Its not enough to move on !

chrisjenx commented 7 years ago

This is only applied to the Activity. Fragments extend from the Activity. No need to do anything else.

On Sat, 8 Apr 2017 at 19:21 Deshar Sunil notifications@github.com wrote:

How to use this function on a fragment !

@Override https://github.com/Override protected void attachBaseContext(Context newBase) { // Set Default Font super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); }

Can you guyz provide more information ! Its not enough to move on !

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/342#issuecomment-292736266, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHRsZL13DIret3PqBmoDLn0lcINxefvks5rt9AegaJpZM4Kfi14 .

sathamkhussain commented 5 years ago

@chrisjenx Still showing default font in fragments. Any idea?

sathamkhussain commented 5 years ago

How to use this function on a fragment !

@override protected void attachBaseContext(Context newBase) { // Set Default Font super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); }

Can you guyz provide more information ! Its not enough to move on !

How did you solve this?

chrisjenx commented 5 years ago

Did you override this in the Activity NOT the Application?

sathamkhussain commented 5 years ago

@chrisjenx Yes did it like above. And the class extends AppCompatActivity

@Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

chrisjenx commented 5 years ago

OK how do you inflate fragments (it shouldn't matter) but you might need to override the Activity inflater too if you inflate in xml

sathamkhussain commented 5 years ago

@chrisjenx Check below

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
    @Nullable Bundle savedInstanceState) {
        LayoutInflater mInflater = getActivity().getLayoutInflater();
        View rootView = mInflater.inflate(R.layout.activity_dashboard, null);
        TextView userEmail = rootView.findViewById(R.id.user_email_ID);
        return rootView;
 }
chrisjenx commented 5 years ago

Yeah don't do that, always use the inflater given to you, or at worse LayoutInflater.from(context).

chrisjenx commented 5 years ago

You may need to override this in the activity too:

    /*
        Uncomment if you disable PrivateFactory injection. See CalligraphyConfig#disablePrivateFactoryInjection()
     */
//    @Override
//    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
//    public View onCreateView(View parent, String name, @NonNull Context context, @NonNull AttributeSet attrs) {
//        return CalligraphyContextWrapper.onActivityCreateView(this, parent, super.onCreateView(parent, name, context, attrs), name, context, attrs);
//    }
sathamkhussain commented 5 years ago

@chrisjenx I have added it on my activity, still, the fragment shows default font. And also I tried changing like below in my fragment.

 @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
@Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_dashboard, container, false);
   return rootView;
  }