UrbanPro
true

Learn Android Application Development Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Android : Support multiple themes in an android application

Gaurav Bansal
11/01/2017 0 0

If you are developing a theme based application to enhance user experience, then following steps needs to follow.

We are taking example of an android application having 2 themes white and black.

Step 1:
Define 2 themes in your application's styles.xml file

<style name="SampleAppBlackTheme">
    <item name="ic_action_accept">@drawable/ic_action_accept</item>
    <item name="ic_action_cancel">@drawable/ic_action_cancel</item>
    <item name="ic_action_delete">@drawable/ic_action_delete</item>
</style>
<style name="SampleAppWhiteTheme">
    <item name="ic_action_accept">@drawable/ic_action_accept_white</item>
    <item name="ic_action_cancel">@drawable/ic_action_cancel_white</item>
    <item name="ic_action_delete">@drawable/ic_action_delete_white</item>
</style>
Step 2:
Define a default theme for MainActuvity in android manifest:
<activity    
    android:name=".MainActivity"    
    android:screenOrientation="portrait"    
    android:theme="@style/SampleAppBlackTheme"
    android:windowSoftInputMode="stateHidden|adjustNothing" >
.........
</activity>
 
Step 3:
Based on User choice of theme, set theme programmatically in onCreate() method of respective activity:
@Overrideprotected void onCreate(Bundle savedInstanceState) {
    overridePendingTransition(0, 0);
    if (Constants.GO_WHITE)
        setTheme(R.style.SampleAppWhiteTheme);
    else
       setTheme(R.style.SampleAppBlackTheme);
    ........
}
P.s.
In case of multiple activities, define 2 themes for each activity
 
Step 4:
Define the common items in the attrs.xml as below :
<attr name="ic_action_accept" format="reference" />
<attr name="ic_action_cancel" format="reference" />
<attr name="ic_action_delete" format="reference" />
 
Step 5:
Use the defined attributes in the layout xml file or use programmatically in any view as per below example.
Layout example :
<ImageView    
    android:id="@+id/accept_icon"    
    android:layout_width="@dimen/abc_action_button_min_width"    
    android:layout_height="@dimen/abc_action_bar_default_height_material"
    android:src="?attr/ic_action_accept"    
    android:visibility="visible" />
 
Fragment or View example :
int[] attrs = new int[]{R.attr.ic_action_accept};
TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
Drawable ic_action_accept = typedArray.getDrawable(0);
 
These attribute will be resolved in styles.xml file and return the drawable or color as per the current theme of the activity. 
 
That's how multiple themes can be supported. In case of any questions please comment.
0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Crux Of The Basic Interview Question Asked Around It Professionals
Question: Explain PSVM() in Java. Answer: This question is asked from a differnet persctive. Person asking this question is looking for deep java concepts. If answer in your mind is something like...

Google's Android training programs for India
Earlier in May I attended a Google program called "Train The Trainer" where Google had invited Android trainers from all over India. It was a great opportunity to interact with Android people from every...

Interview Tip : Q1) Why Strings are immutable in java ? What happen if it was mutable in java?
As we all know that Strings in java are immutabe in nature, now the question comes why the creator made it immutable in nature, although this field used maximum in any java program. The answer to this...

Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...

What Is Applet & Its Life Cycle?
What is Applet & its life cycle?Applet is a Java programme that can be embedded into HTML page.Java Applet runs on the java enables web browsers such as Mozilla & Internet Explorer.Applets are...
I

ICreative Solution

0 0
0
X

Looking for Android Application Development Training Classes?

The best tutors for Android Application Development Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Android Application Development Training with the Best Tutors

The best Tutors for Android Application Development Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more