applibgroup / HarmonyOS-Knowledgebase

This repository contains code samples of HarmonyOS Training
Apache License 2.0
17 stars 6 forks source link

What is the alternative in Harmony Os for function setBackgroundColor() on View class in Android? #36

Closed chakrichokkaku closed 3 years ago

chakrichokkaku commented 3 years ago

I am working on a HarmonyOs project in that I wanted to set the background color of the component. In Android we have a function setBackgroundColor() in the View class, this can be done as shown below.

View titleLayout = view.findViewById(R.id.titleLayout);
titleLayout.setBackgroundColor(calendarTitleBackgroundColor);

How can I set background color to a component in HarmonyOs?

Link to StackOverflow https://stackoverflow.com/questions/68588609/what-is-the-alternative-in-harmony-os-for-function-setbackgroundcolor-on-view

Additional information Developer Platform: Windows DevEco Studio version: 2.1.0.303 SDK API version: 5 SDK version: 2.1.1.21 Device: Not required Device OS version: Not required

GowthamRayar commented 3 years ago

First you have to build an Element using some color,

    public static Element buildElementByColor(int color) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(ShapeElement.RECTANGLE);
        drawable.setRgbColor(RgbColor.fromArgbInt(color));
        return drawable;
    }

later you have set the built element using setBackground API

    component.setBackground(buildElementByColor((Color.DKGRAY).getValue()));