Activity and Fragment Layouts with AndroidX

For as long as Android development has been around, we’ve been using setContentView to inflate layouts in our Activity classes. And since the introduction of Fragments, we’ve had to override onCreateView and use a layout inflater to get our view from a layout ID. They’re so common to write that it’s easy to not think about them much. At the same time, they feel a bit like boilerplate, right? Wouldn’t it be nice if there was a simpler way to inflate our views?

I’m glad to say that with a few recent AndroidX library updates, there is a simpler way.

Show me the code!

During development of AndroidX Fragment 1.1.0 and AndroidX Activity 1.0.0, the kind folks at Google added a new way to inflate layouts. To start using these, add the following lines to the dependency block of your module-level build.gradle file:

implementation 'androidx.appcompat:appcompat:1.1.0-rc01' // this is needed to use the updated AppCompatActivity implementation 'androidx.activity:activity-ktx:1.0.0-rc01' // or remove -ktx if not using kotlin implementation 'androidx.fragment:fragment-ktx:1.1.0-rc04' // or remove -ktx if not using kotlin

Note that these versions are the latest as of this writing, but update them as needed. You can find the latest versions here.

These new versions add the ability to pass the layout ID to the constructor of the Activity or Fragment base class, and it will handle inflation behind-the-scenes. So, instead of something like:

class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.my_activity_layout) } // ... }

… you can instead write:

class MyActivity : AppCompatActivity(R.layout.my_activity_layout) { // ... }

This works similarly with Fragments as well. Make sure to import AndroidX Fragments ( androidx.fragment.app.Fragment ) rather than the framework version ( android.app.Fragment ).

Old way:

class MyFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return layoutInflater.inflate(R.layout.my_fragment_layout, container, false) } // ... } ```

New way:

class MyFragment : Fragment(R.layout.my_fragment_layout) { // ... }

If the only code that was inside of the Activity’s onCreate or the Fragment’s onCreateView method was layout inflation code (like in the examples above), then you can completely remove those overridden methods. This is especially useful in Fragments, since you can put any logic for after the view is inflated in the onViewCreated method.

Wrapping up

This new way of using layouts isn’t going to change the landscape of UI development on Android (I’m lookin’ at you, Jetpack Compose), but it’s a small step that can save us from writing a little bit of extra code when making our Activities and Fragments, which I can certainly appreciate.

Need an introduction to Kotlin? Download our free eBook to learn what this language means for your Android App.