AndroidX Part 1: AppCompat

S M Mohi-Us Sunnat
sunnat629.dev
Published in
3 min readMar 7, 2020

You can read in Bangla blog: অ্যান্ড্রয়েডএক্স পর্ব ১ঃ অ্যাপ-কমপ্যাক্ট

As we all know, after releasing AndroidX, there is a huge change in the Android project. Nowadays every developer is very concerned about reducing the code line, easier to read, robust and fast projects.

Thanks to the Android JetPack, we can now develop a project with less code.

Today, we will learn what is the new features of AppCompat of AndroidX

Let’s start by creating a new project. After creating an AndroidX based project, if you check the dependency of the appgradle file —

Here, we can see implementation ‘androidx.appcompat:appcompat:1.1.0’, which is a library of androidx. 1.1.0 is the current latest version of androidx.appcompat.

Now, if we check the MainActivity class, we can see the standard version of the activity code. Here is the code snippet -

Here if you notice the AppCompatActivity , it has been imported from androidx.appcompat.app.AppCompatActivity. This standard version of code, we set a layout of an activity by using setContentView() function.

setContentView() is basically what this function does is display the Layout created through XML or the Dynamically created layout view in an Activity Screen.

R means Resource

layout means design

activity_main is the xml you have created under res->layout->activity_main.xml

It’s really simple, isn’t it?

OK, now let’s check what does the AndroidX brings?

Let’s go to the MainAcitivity class. In the androidx.appcompat:appcompat:1.1.0 . Now just remove the setContentView() and add this R.layout.activity_main in the AppCompatActivity() as a parameter like -

It does the same thing as setContentView() does. So from now we can use AppCompatActivity() to set the xml layout.

This means that we can now directly use the layout as a parameter of AppCompatActivity.
If we go inside AppCompatActivity a little bit. Then we can see that the parent class of AppCompatActivity [appcompat: appcompat] is FragmentActivity [androidx.fragment] which is again subclass of ComponentActivity [androidx.activity]> androidx.core.app.ComponentActivity> Activity . Inandroidx.appcompat: 1.1.0 has a new constructor for AppCompatActivity (@LayoutRes int contentLayoutId). Because of this, we can now launch the xml layout directly in the AppCompatActivity as a parameter.

Here is the changelog of AppCompat: LINK

Next time, we will see the androidx.activity.

If you are benefited by this blog, please clap to support me, share it with others and follow the channel for the upcoming blogs.

Happy Coding…

--

--