Which is best way to define constants in android, either static class, interface or xml resource ?

Which is best way to define constants in android, either static class, interface or xml resource ?

Asked on January 5, 2019 in XML.
Add Comment


  • 7 Answer(s)

    To define constants in android:

         There are more difference that can able to reference the project resources in the XML layouts. Also they are present in the application context and are accessible across the global application. Using project resources is the ease of access and allowing to organize the project significantly are the main advantages.

    The static final constants can be compiled into the java bytecode; where the project resources are compiled into a binary format within the apk. The accessing process is extremely efficient if there is a difference between the two, it is little at most.

    There is no rules on that using resources/constants in the project. In another way, mostly use static final constants for values which will be used by our java code only and are specific to our implementation.

    Note that it is possible to load XML resources at runtime which is depending on the device’s current configuration. So take this into consideration when got deciding whether it is or not declare the constant in XML or directly in your .java files.

    Answered on January 5, 2019.
    Add Comment

         To use a class to define the constants and call any where,

    Constant.java

       package org.nrum.nrum;
    /**
    * Created by rajdhami on 5/23/2017.
    */
       public class Constant {
       public static final String SERVER = "http://192.168.0.100/bs.dev/nrum";
    //     public static final String SERVER = "http://192.168.100.2/bs.dev/nrum";
       public static final String API_END = SERVER + "/dataProvider";
       public static final String NEWS_API = API_END + "/newsApi";
       public static final String BANNER_API = API_END + "/bannerApi/lists";
       public static final String NOTICE_API = API_END + "/noticeApi/lists";
       public static final String UPLOAD_PATH = SERVER + "/uploads";
       public static final String UPLOAD_PATH_BANNER = UPLOAD_PATH + "/company_1/banner";
       public static final String UPLOAD_PATH_NEWS = UPLOAD_PATH + "/company_1/news";
       public static final int BANNER_TRANSITION_DURATION = 5000;
       public static final int NOTICE_BUTTON_BLINK_DURATION = 5000;
       public static final int BANNER_FETCH_LIMIT = 3;
    }
    

         Now use the above constants in following way.

    Constant.NOTICE_BUTTON_BLINK_DURATION
    
    Answered on January 5, 2019.
    Add Comment

    XML values have the advantages,

    • of accessbilty in layout file and manifest file over Constants in java file
    • for multi language support over Constants in java file
    Answered on January 5, 2019.
    Add Comment
    <integer name="config_build_type">0</integer>
    <string-array name="url_authentication">
        <item >http://development.com/xxxx</item>
        <item >http://test.com/xxx</item>
        <item >http://example.com/xxx</item>
    </string-array>
    Answered on February 28, 2019.
    Add Comment

    There is a big difference between the two in that you can reference project resources in your XML layouts. They are available in the application context and are therefore accessible across the global application. The biggest advantages of using project resources is the ease of access and that they allow you to organize your project significantly.

    static final constants are compiled into the java bytecode; project resources are compiled into a binary format within the apk. Accessing either is extremely efficient… if there is a difference between the two, it is trivial at most.

    There isn’t a set rule on how you should be using resources/constants in your project. That said, I personally use resources for values that I might need to use in my XML or java code. On the other hand, I typically use static final constants for values that will only be used by my java code and are specific to my implementation.

    Also note that it is possible to load XML resources at runtime depending on the device’s current configuration (i.e. screen size, locale, etc.). So you should take this into consideration when deciding whether or not you should declare the constant in XML or directly in your .java files

    Answered on February 28, 2019.
    Add Comment
    package org.nrum.nrum;
    
    /**
     * Created by rajdhami on 5/23/2017.
     */
    public class Constant {
        public static final String SERVER = "http://192.168.0.100/bs.dev/nrum";
    //    public static final String SERVER = "http://192.168.100.2/bs.dev/nrum";
        public static final String API_END = SERVER + "/dataProvider";
        public static final String NEWS_API = API_END + "/newsApi";
        public static final String BANNER_API = API_END + "/bannerApi/lists";
        public static final String NOTICE_API = API_END + "/noticeApi/lists";
        public static final String UPLOAD_PATH = SERVER + "/uploads";
        public static final String UPLOAD_PATH_BANNER = UPLOAD_PATH + "/company_1/banner";
        public static final String UPLOAD_PATH_NEWS = UPLOAD_PATH + "/company_1/news";
        public static final int BANNER_TRANSITION_DURATION = 5000;
        public static final int NOTICE_BUTTON_BLINK_DURATION = 5000;
        public static final int BANNER_FETCH_LIMIT = 3;
    }
    Answered on February 28, 2019.
    Add Comment
    i thinik both way seems to be good but thing is that it depends on your requirements.

    If you have your values(web service link) in your XML and suppose there is any change in your values(web service link) , you can easily change only in XML file.

    But if you use inside classes as static variables you have to change in all class files.

    So my suggestion is that separate constants from source file and put into resource and access it..

    Answered on February 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.