Define Resource Layout Id In the Activity/ Fragment Constructor — With AndroidX

Reduce the number of methods from your activity/fragment


Resource Layout Id in Activity

public class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


Resource Layout Id in Fragment

public class MainFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)


Visit my website at

With AndroidX

implementation 'androidx.appcompat:appcompat:1.1.0'

Resource Layout Id in Activity with AndroidX

public class HomeActivity :AppCompatActivity(R.layout.activity_home)

Resource Layout Id in Fragment with AndroidX

public class HomeFragment : Fragment(R.layout.fragment_home)


Senior Software Engineer(Android). Open Source Contributor, Technical Writer, Email: check my website at