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

Reduce the number of methods from your activity/fragment

Credits

Resource Layout Id in Activity

public class MainActivity : AppCompatActivity() {


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

}
}

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)
}

}

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)

Conclusion

Senior Software Engineer(Android). Open Source Contributor, Technical Writer, Email: Dani.amjad12@gmail.com check my website at https://danishamjad.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store