Issue
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1, container, false);
}
I have this code for my fragment but it doesn't let me use findViewById so I want to make it onViewCreated but it has some container and tab in it so can anyone help me?
Solution
Use getView() or the View parameter from implementing the onViewCreated method. It returns the root view for the fragment (the one returned by onCreateView() method). With this also you can use findViewById().
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
ImageView imageView = (ImageView) getView().findViewById(R.id.foo);
}
As getView() works only after onCreateView(), you can't use it inside onCreate() or onCreateView() methods of the fragment.
Answered By - Karthik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.