[Update: New syntax] Inflating layouts with the ContentView annotation
label kotlin
label androidx
label android
label annotation

[Update: New syntax] Inflating layouts with the ContentView annotation

by Edric Chan

In this post, I’ll be covering how to simplify inflating layouts in your fragments/activities with the ContentView annotation.

Note: The ContentView annotation was introduced in version 1.1.0-alpha01 of the androidx.annotation:annotation library. See the release notes for more info.

Update (4/4/2019)

The ContentView annotation is now to be used as a constructor of AppCompatActivity or a Fragment. The former implementation has been removed in 1.0.0-alpha06 of the androidx.activity:activity dependency and in 1.1.0-alpha05 of the androidx.fragment:fragment dependency.

Instead, use the following syntax:

Java (Fragment)

import androidx.fragment.app.Fragment;
// ...

public class HomeFragment extends Fragment {
  // ...
  public HomeFragment() {
    // Pass the layout resource ID to the super constructor
    super(R.layout.frag_home);
  }
}

Kotlin (Fragment)

import androidx.fragment.app.Fragment
// ...

class HomeFragment: Fragment(R.layout.frag_home) {
  // ...
}

Java (Activity)

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
  // ...
  public MainActivity() {
    // Pass the layout resource ID to the super constructor
    super(R.layout.activity_main);
  }
}

Kotlin (Activity)

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(R.layout.activity_main) {
  // ...
}

This is probably what you use to inflate a layout - override the onCreateView to inflate a layout (for fragments) and return the instance, or to call setContentView in the onCreate method to set the layout.

Java (Fragment)

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
// ...

public class HomeFragment extends Fragment {
  // ...
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.frag_home, container, false);
  }
}

Kotlin (Fragment)

import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
// ...

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

Java (Activity)

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}

Kotlin (Activity)

import android.os.Bundle
import androidx.appcompat.AppCompatActivity

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
}

This can now be simplified with the ContentView annotation. Just specify the layout resource ID as the parameter of the annotation and use it on the class definition.

Java (Fragment)

import androidx.annotation.ContentView;
import androidx.fragment.app.Fragment;
// ...

@ContentView(R.layout.frag_home)
public class HomeFragment extends Fragment {
  // ...
}

Kotlin (Fragment)

import androidx.annotation.ContentView
import androidx.fragment.app.Fragment
// ...

@ContentView(R.layout.frag_home)
class HomeFragment: Fragment() {
  // ...
}

Java (Activity)

import androidx.annotation.ContentView;
import androidx.appcompat.app.AppCompatActivity;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
  // ...
}

Kotlin (Activity)

import androidx.annotation.ContentView
import androidx.appcompat.app.AppCompatActivity

@ContentView(R.layout.activity_main)
class MainActivity : AppCompatActivity() {
  // ...
}

Notes

  • The ability to use the ContentView annotation on a class that extends ComponentActivity (which FragmentActivity directly and AppCompatActivity indirectly inherit from) was introduced in 1.0.0-alpha04 of the androidx.activity:activity dependency. (See the release notes for more info)
  • The ability to use the ContentView annotation on a class that extends Fragment was introduced in 1.1.0-alpha04 of the androidx.fragment:fragment dependency. (See the release notes for more info)

About Edric Chan

(site owner)

An 18-year-old teenager who self-taught himself to code at the age of 13 with a 2015 Macbook Pro. Currently pursuing DIT at Singapore Polytechnic.

Share "[Update: New syntax] Inflating layouts with the ContentView annotation"

Successfully copied to the clipboard!
An error occurred while attempting to copy to the clipboard