In many Android Application we are use that feature .User can set their theme color of Android Application Dynamically .This feature helps to make your App more Attractive.So, In this Tutorial we are create a basic application which helps to know about how it works and helps to use in your application
So, Let's Get Start Now
1 . Create a new Project and take An Blank Activity name as MainActivity
2 . Create a One new Java Class named as themeUtils.java where we are creating Dynamic theme
on menu option click
We are provided step by guide which helps to create dynamic themes on one click in Android Application
Step-1
First we define different theme colors in color.xml file which helps to create styles of themes in your style.xml your project
Insert this code in color.xml inside the <resource></resource> (res->values->color.xml)
<resources>
<color name="white">#FFFFFF</color>
<color name="Olive">#556B2F</color>
<color name="colorPrimaryPinkDark">#E91E63</color>
<color name="colorPrimaryTeal">#00897B</color>
<color name="colorPrimaryTealDark">#009688</color>
<color name="colorPrimaryBlue">#3949AB</color>
<color name="colorPrimaryBlueDark">#3F51B5</color>
<color name="colorPrimaryGreen">#4CAF50</color>
<color name="colorPrimaryGreenDark">#4CAF50</color>
</resources>Step-2
After insert colors in color.xml file the we are creating style in style.xml file of your project
(res->values->style.xml) inside <resources></resources> Tag
<style name="SkyTheme" > <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorPrimary</item> </style>
<style name="PinkTheme" > <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimaryPinkDark</item> <item name="colorPrimaryDark">@color/colorAccent</item> <item name="colorAccent">@color/colorPrimaryPinkDark</item> </style>
<style name="TealTheme"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimaryTeal</item> <item name="colorPrimaryDark">@color/colorPrimaryTealDark</item> <item name="colorAccent">@color/colorPrimaryTeal</item> </style> <style name="GreenTheme"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimaryGreen</item> <item name="colorPrimaryDark">@color/colorPrimaryGreenDark</item> <item name="colorAccent">@color/colorPrimaryGreen</item> </style> <style name="BlueTheme"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimaryBlue</item> <item name="colorPrimaryDark">@color/colorPrimaryBlueDark</item> <item name="colorAccent">@color/colorPrimaryBlue</item> </style>
Step-3
Create menu option in menu.xml file (res->menu->menu.xml) inside the <menu></menu> Tag
<item android:id="@+id/color_green" android:orderInCategory="100" android:title="Green" app:showAsAction="never" />
<item android:id="@+id/color_teal" android:orderInCategory="100" android:title="Teal" app:showAsAction="never" />
<item android:id="@+id/color_blue" android:orderInCategory="100" android:title="Blue" app:showAsAction="never" />
<item android:id="@+id/color_pink" android:orderInCategory="100" android:title="Pink" app:showAsAction="never" />
<item android:id="@+id/color_sky" android:orderInCategory="100" android:title="SkyBlue" android:icon="@mipmap/ic_launcher" app:showAsAction="never" />
Step-4
Then move to themeUtils.java file where we create theme at runtime on menu option click
Inside the themeutils.java insert these lines of code
public class themeUtils { private static int cTheme; public final static int SKYBLUE = 0; public final static int TEAL = 1; public final static int BLUE = 2; public final static int GREEN = 3; public final static int PINK = 4; public static void changeToTheme(Activity activity, int theme) { cTheme = theme; activity.finish(); activity.startActivity(new Intent(activity, activity.getClass())); } public static void onActivityCreateSetTheme(Activity activity) { switch (cTheme) { default: activity.setTheme(R.style.PinkTheme); break; case PINK: activity.setTheme(R.style.PinkTheme); break; case TEAL: activity.setTheme(R.style.TealTheme); break; case BLUE: activity.setTheme(R.style.BlueTheme); break; case GREEN: activity.setTheme(R.style.GreenTheme); break; case SKYBLUE: activity.setTheme(R.style.SkyTheme); } } }
Step-5
After insert all the code in a proper way we are edit MainActivity.java file . Override onOptionsItemSelected() Method
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); themeUtils.onActivityCreateSetTheme(this); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.color_green) { themeUtils.changeToTheme(this, themeUtils.GREEN); return true; } else if (id == R.id.color_teal) { themeUtils.changeToTheme(this, themeUtils.TEAL); return true; } else if (id == R.id.color_blue) { themeUtils.changeToTheme(this, themeUtils.BLUE); return true; } else if (id == R.id.color_pink) { themeUtils.changeToTheme(this, themeUtils.PINK); return true; } else if (id == R.id.color_sky) { themeUtils.changeToTheme(this, themeUtils.SKYBLUE); return true; } return super.onOptionsItemSelected(item); } }
This is overall which is help you to create runtime themes in your Android Application.
Download SourceCode link :- https://drive.google.com/open?id=1xCmZLCzG98XsJ4yW-Wmgp_68CxMcCbBp
If you need professional help for your project do let us know.
You can directly contact us on developers.omitech@gmail.com
Navigate to settings, access the "Themes" option, and explore various themes or customize your own. How Win Game This simple process lets you personalize your device's appearance, enhancing aesthetics and reflecting your unique style.
ReplyDelete