Calligraphy icon indicating copy to clipboard operation
Calligraphy copied to clipboard

Fragment Issue with Data Binding

Open pratikbutani opened this issue 9 years ago • 18 comments

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.

pratikbutani avatar Oct 25 '16 04:10 pratikbutani

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));
    }

af-abe avatar Oct 26 '16 12:10 af-abe

@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.

pratikbutani avatar Oct 26 '16 13:10 pratikbutani

@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'

af-abe avatar Oct 26 '16 13:10 af-abe

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

pratikbutani avatar Oct 26 '16 13:10 pratikbutani

@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;
}

af-abe avatar Oct 26 '16 14:10 af-abe

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

pratikbutani avatar Oct 26 '16 14:10 pratikbutani

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

chrisjenx avatar Nov 01 '16 00:11 chrisjenx

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 !

deshario avatar Apr 08 '17 18:04 deshario

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 [email protected] 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 .

chrisjenx avatar Apr 09 '17 00:04 chrisjenx

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

sathamkhussain avatar Jul 18 '19 18:07 sathamkhussain

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?

sathamkhussain avatar Jul 18 '19 18:07 sathamkhussain

Did you override this in the Activity NOT the Application?

chrisjenx avatar Jul 18 '19 19:07 chrisjenx

@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);

}

sathamkhussain avatar Jul 19 '19 10:07 sathamkhussain

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

chrisjenx avatar Jul 19 '19 13:07 chrisjenx

@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;
 }

sathamkhussain avatar Jul 19 '19 14:07 sathamkhussain

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

chrisjenx avatar Jul 19 '19 15:07 chrisjenx

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);
//    }

chrisjenx avatar Jul 19 '19 15:07 chrisjenx

@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;
  }

sathamkhussain avatar Jul 19 '19 15:07 sathamkhussain