user3397984
user3397984

Reputation: 13

use font in Fragment

I tend to use the font fragment and fragment have control over Why the following code produces an error?

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

        View rooo = inflater.inflate(R.layout.din2, container, false);
        TextView tv1=(TextView) rooo.findViewById(R.id.tv1);
        Typeface yaghut=Typeface.createFromAsset(getActivity().getAssets(), "font/BKOODB.ttf");
        tv1.setTypeface(yaghut);
        return rooo;
    }

    }

this error enter image description here

Upvotes: 1

Views: 130

Answers (3)

Hamid Shatu
Hamid Shatu

Reputation: 9700

You are creating a view named rooo but you are returning a view named roo...now replace all roo with rooo as below...

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

    View rooo = inflater.inflate(R.layout.din2, container, false);
    TextView tv1=(TextView) rooo.findViewById(R.id.tv1);
    Typeface yaghut=Typeface.createFromAsset(getActivity().getAssets(), "font/BKOODB.ttf");
    tv1.setTypeface(yaghut);
    return rooo;
}

}

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

onCreateView is supposed only to return the view that represent the fragment. Other operations on the View should be performed in another callback, onViewCreated

    @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {       
      return inflater.inflate(R.layout.din2, container, false);
 }

 @Override 
 public void onViewCreated (View view, Bundle savedInstanceState) {
        TextView tv1=(TextView) view.findViewById(R.id.tv1);
        Typeface yaghut=Typeface.createFromAsset(getActivity().getAssets(), "font/BKOODB.ttf");
        tv1.setTypeface(yaghut);
 }

Upvotes: 3

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

you need change :

TextView tv1=(TextView) r.findViewById(R.id.tv1);

to

 TextView tv1=(TextView) rooo.findViewById(R.id.tv1);

because your TextView with tv1 id's must be on din2 layout.

Upvotes: 1

Related Questions