1

I am building a HarmonyOS application and want to load color values that I have placed in the resources folder in resources/base/element/color.json. How can I load this color in my Java class?

In Android we can use the getColor() function for this:

context.getResources().getColor(R.color.colorID);

What is the alternative for this in HarmonyOS?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Soham
  • 37
  • 5
  • In HarmonyOS you can get color as follow: `new Color(getColor(ResourceTable.Color_purple));` – C2C Jul 28 '21 at 05:16

1 Answers1

0

You may can refer to the following implementations:

  1. color.json
{

  "color": [

    {

      "name": "primary",

      "value": "#FF0000"

    }

  ]

}

  1. MainAbilitySlice
    public void onStart(Intent intent) {

        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_ability_main);



        ResourceManager resManager = this.getResourceManager();

        try {

            int color = resManager.getElement(ResourceTable.Color_primary).getColor();

            Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);

            text.setTextColor(new Color(color));

        } catch (IOException e) {



        } catch (NotExistException e) {



        } catch (WrongTypeException e) {



        }

    }

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108