Search code examples
findviewbyid

what do i use instead of findViewById in onCreateView using fragment


I'm new to Android developing and of course on Fragments.

i have the code below and i want to use findViewById in fragment but because it's on onCreateView it has error

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_main,null);

        x2=(Button)getView().findViewById (R.id.btnX2);
        x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
        showResult = (EditText)getView().findViewById(R.id.display);

    }

Solution

  • You may try this.

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v= inflater.inflate(R.layout.activity_main,container,false);
            x2=(Button)v.findViewById (R.id.btnX2);
            x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
            showResult = (EditText)v.findViewById(R.id.display);
            return v;
        }