Customize your App Theme all Yourself
So choosing a new theme every time for your Android App is quite irritating. Choosing the color combination of Status bar, Action bar, and Background for every new app we make leaves us with choosing almost the same theme for all the apps. Further, the theme liked by one user may not have an appealing effect on others. So why not give the user of the app his own independence of choosing the app’s theme.
The library “Customize-Bars” helps you to choose a theme for your app by yourself. After adding this library, you can open the theme selecting activity by shaking your mobile phone. It uses android sensors to sense the shake and send you to the theme selection activity where you can choose a theme by adjusting the color of Status, Action, and background.
In this blog, I have explained both, using the library in your app and the making of the library.
- Using Library in App : Procedure given in the Readme of this github repo.
- Making of Library: Given Below.
Making of “Customize-Bars” Library for Android
To make this library, we will use an Android Holo themed color picker library designed by Marie Schweiz to pick the color for our bars and background. So let's start with creating a new Android Project. Then go to File>New>New Module and choose Android Library. Fill the necessary details and click finish.
Now go to the build.gradle file of your library and add the following dependency :
Now make a new Activity inside the Library’s Java folder and name it, say ColorPicker.java.
Add the following code to its xml :
The ColorPiker view enables you to choose a color. The saturation, opacity and value bar helps you to adjust the color intensity of the color chosen from Color Picker. The three views show the color of indexes labeled against them.
The corresponding Java code is as given below :
So the code start with initialization of all the required variables and views used in this activity. Here we are using SharedPreferances to store the selected colors for the bars and background. The values are read from SharedPreferances files when the activity loads for the first time and their colors are set accordingly. Clicking on a given view shown against the labels set the color of corresponding bars according to the color selected in ColorPicker and save the value to SharedPreferances immediately.
Now we need to have a Java class that will help us to initialize the variables and other things to use this library into your app.
Make a new java class inside your library module and name it HandlingColorPicker.java. Add the following code to this class :
This class is used by those activities in which you want to have your selected theme implemented. Here we first declare some static variables, some of which are used by SharedPreferences to read the value and some to implement the SensorListener inside your activity.
The method “addPreviousValues(…)” take some arguments. The first one is a view or the main parent background of your Activity in which the background color will be filled. Second and third and Window and ActionBar objects which are used to set the color in Status and Action bar. And the last one is the context of the activity from which it is called.
To launch your library, push it to GitHub, release it and publish it on JitPack. Jitpack will provide you the dependency you need to add in your app to use the library. Follow this Medium guide on how to launch your library on Jitpack and Github.
TADA! There you have your complete library hosted on your GitHub and ready for use by others.