-1

I would like to use some java class constant value in my XML layout. Is that possible?

java class

public class ConstantKey {

   public static final String COUNTRY_CODE            = "+00";
   public static final STRING COUNTRY_NAME            = "USA";

}

Xml layout

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="ConstantKey.COUNTRY_NAME"/>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shomu
  • 2,734
  • 24
  • 32
  • 1
    Layouts aren't dynamic. Meaning that they don't interact with Java code. You can do the opposite, though. Define a constant in a resource file. It can be then accessed by both resorces and Java code. – Phantômaxx Feb 14 '18 at 08:48

1 Answers1

2

You could:

1) If the strings are constant all through, you could add them as string resources inside strings.xml. This way they're accessed from both layout or java code.

2) If the values depend on a network call or are dynamically computed, I suggest using DataBinding in andorid, check my answer here for small implementation example.

riadrifai
  • 1,108
  • 2
  • 13
  • 26