Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: assigning a constant value to a xml-element

Tags:

I'm currently programming an android app. There I am stuck with xml-layout. Is it possible to assign a value to an xml-tag using a variable (constant) defined in a class?

i have a class called Constants.java for all my program constants. i do this for better maintainability. now i would like to use one of this constants, e.g. VAL as defined below as value for an ui-widged.

public class Constants { public static final int VAL = 10; ... } 

in my case, the widget is a progressbar (horizontal style) and i would like to define the "android:max" value. normally one can write:

android:max="10" android:max="@Integer/val 

but i would like to use the value defined in my Constants class, something like:

android:max="Constants.VAL" 

is there a solution for that?

thanks

like image 460
grAPPfruit Avatar asked Dec 13 '11 16:12

grAPPfruit


People also ask

What is use of named constants in XML?

Constants. Prefix to use to represent the default XML Namespace. Feature for secure processing. Namespace URI to use to represent that there is no Namespace.

What is const in Android?

The const keyword is used to declare those properties which are immutable in nature i.e. these properties are read-only properties. But, the value of these properties must be known at the compile-time only and this is the reason const is also known as Compile-time constant.

Does Android still use XML?

Android applications use XML to create layout files. Unlike HTML, XML is case-sensitive, requires each tag be closed, and preserves whitespace.

What is a values XML Android?

The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc. Below explained are few basic files, contained in the res/values folder: colors. xml: The colors.


1 Answers

No, you can't. Constant values in classes are only available at runtime, and the XML files are compiled and generated before runtime.

The next-best thing to do is declare the XML constants you want to use in res/values/integers.xml. Here's an example integers.xml file:

<?xml version="1.0" encoding="utf-8"?> <resources>     <integer name="max">10</integer> </resources> 

to use this value in your XML, do this:

<YourComponent     android:yourattr="@integer/max"/> 
like image 185
Rose Perrone Avatar answered Sep 20 '22 19:09

Rose Perrone