Skip to main content

Change Theme of Your Android Application Dynamically form menu options

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="transparent">#7FFFFFFF</color>

<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.



If you need professional help for your project do let us know. 
You can directly contact us on developers.omitech@gmail.com




















Comments

  1. 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

Post a Comment

Popular posts from this blog

How to align Title at center of ActionBar in Android

How to Align Title At Center of Action Bar in Android                                                                                                                                                @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("DashBoard"); }   Activity  public void setTitle ( String title ){ getSupportActionBar (). setHomeButtonEnabled ( true ); getSupportActionBar (). setDisplayHomeAsUpEnabled ( true ); TextView textView = new TextView ( this ); textView . setText ( title ); textView . setTextSize ( 20 ); textView . setTypeface ( null , Typeface . BOLD ); textView . setLayoutParams ( new LinearLayout . LayoutParams ( LinearLayout . LayoutParams . FILL_PARENT , LinearLayout . LayoutParams . WRAP_CONTENT )); textView . setGravity ( Gravity . CENTER ); textView . setTextColor ( getResources (). ge

Difference Between Pending Intent And Intent in Android

                       Normal Intent       Normal Intent will die as soon as the app being killed.    An Android Intent is an object carrying an intent, i.e a message from one Component to another     Component either inside or outside of the application.Intent can communicate message among     any of the three core Components of an application -- Activities, Services,and BroadcastReceivers.     Two types of Intent in Android   1. Explicit Intent.   2.Implicit Intent  Explicit Intent is an Intent which is used to Call the another component Explicitly in your application  Like :We are calling  Next activity on button click of First activity using Intent Example  // Explicit Intent by specifying its class name Intent i = new Intent ( this , TargetActivity . class ); i . putExtra ( "Key1" , "ABC" ); i . putExtra ( "Key2" , "123" ); // Starts TargetActivity startActivity ( i );  Implicit Intent Intent

Expected a key while parsing a block mapping (Flutter)

Flutter makes use of the Dart packaging system, pub. Via your applications  pubspec.yam l file (or simple pubspec), you can pull down packages from the flutter ecosystem, or the broader dart community. Anyway, i need to add some images to my flutter application, and so had to add an assets section to the pubspec .the default Android Studio generated apps pubspec has a lot of commented out code with explainations about what is going on, e.g # To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.io/assets-and-images/#resolution-aware. So I uncommented these lines # assest : - - - #  -  images/a_dot_ham.jpeg with the idea from these comment.  that i would just edit it to suit my particular needs. Once you have edited your  pubspec, you need to click on the "Get dependencies"