Android: An example onCreateView method in a Fragment class

This Java code shows how to implement a couple of things in an onCreateView method inside a Fragment class:

public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_single_image, parent, false);
    ImageView imageView = (ImageView)rootView.findViewById(;
    return rootView;

What I’m doing here is:

  1. Enabling the Android Back/Up button in the ActionBar
  2. Getting a reference to the “root view” in my layout file
  3. Getting a reference to an ImageView that I know is in that layout file
  4. Setting a bitmap image on that ImageView

If you wanted to see how to do any of these things in a fragment’s onCreateView method, I hope this source code is helpful.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.