

So update your activity_main as the code shown below (higlighted in black): Now in our activity_main.xml, we will make LinearLayout and the 2 TextViews point to these attributes instead of some fixed color. #000000 #606060 #ffffff #ffffff #aeaeae #000000 Īs you can see, in both the themes we make use of the attributes we created in our attrs.xml to set colors. Paste the below two styles in your styles.xml.

Now, we need to create two themes, light and dark in our styles.xml (you can find it in the directory res/values ).

So we have created 3 attributes ( controllers) to control the colors of the LinearLayout and its two TextViews in activity_main.xml. Paste the below snippet in your attrs.xml file We’ll be adding three attributes in our attrs.xml file where format of each attribute tag is as follows Right now, you can think of attributes as controllers for colors (in our example) which can then be set to widgets and layouts. Inside attrs.xml file we created, we will be specifying some attributes of our own which then will be used for setting colors for widgets and layouts. Dark Theme ImplementationĬreate a new values resource file inside the directory res/values (Right Click or Alt + Insert on values folder) and name it attrs.xml. So let’s now dive into actual dark theme implementation. In order to change the color depending upon the current theme (broadly speaking light or dark), our approach has to be dynamic.

This approach directly binds the color of widget or layout to a specific color which makes changing of colors with change in theme less flexible. Only reason I told you to paste the same code as mine is to make sure both of us are on the same page and the rest of the tutorial makes perfect sense.īefore we actually begin with our dark theme implementation, let us see the usual way we set colors(highlighted in below snippet) to widgets and layouts. It doesn’t really matter what the layout is. Once the project is ready, clear the boiler plate code in activity_main.xml and paste the following code in the same file. Go ahead and create a new Android Studio Project. So today I am going to explain you how you can implement dark theme in your app in the easiest way possible by giving a short and to the point example. It is an healthy option as it emits less blue light (and people like me find it cool as well 😺). dependencies else if (Utility.Dark Theme in the YouTube UI clone I createdĭark theme, nowadays, is one of the most demanding features that interests the audience of an app or a website. This image show different style tags and meaning for Android 5.+.įirstly need to add dependency for ade file in our project.
