This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Responsive Design in HarmonyOS – DEV Community


Image description
Introduction
Hi there everybody. On this article, I’ll attempt to present details about the looks of purposes on HarmonyOS units. Most individuals have heard of Huawei’s 1+8+N technique. HarmonyOS performs an enormous position on this technique.
Whereas growing an utility for this ecosystem, you possibly can run your utility on a number of units by writing code as soon as. Easy methods to develop completely different UI designs for a number of units? I’ll reply this.
For extra about design:
HarmonyOS Design : https://developer.harmonyos.com/en/docs/design/des-guides/overview-0000001053563071

Image description

Implementation
First, I’ll clarify within the Java venture. It is advisable to create a venture in DevEco Studio. Whereas creating the venture, you possibly can select the units that your utility will help as follows.

Image description

You can also make your base design in response to a tool you specify within the XML file within the venture. I designed a easy UI based mostly on the telephone system.

Image description

UI Design for Cellphone
You possibly can verify the XML codes under.

<?xml model="1.0" encoding="utf-8"?>
<StackLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:top="match_parent"
    ohos:width="match_parent">

    <DirectionalLayout
        ohos:id="$+id:welcomeText_logo_wearable_layout"
        ohos:top="match_content"
        ohos:width="match_parent"
        ohos:layout_alignment="prime"
        ohos:orientation="vertical"
        ohos:visibility="disguise">

        <Textual content
            ohos:id="$+id:welcome_text_wearable"
            ohos:top="match_content"
            ohos:width="match_content"
            ohos:layout_alignment="heart"
            ohos:margin="10vp"
            ohos:textual content="WELCOME"
            ohos:text_size="40fp"/>

        <Picture
            ohos:id="$+id:logo_image_wearable"
            ohos:top="match_content"
            ohos:width="match_content"
            ohos:image_src="https://style-tricks.com/hdgurkey/$media:emblem"
            ohos:layout_alignment="heart"/>

    </DirectionalLayout>


    <Textual content
        ohos:id="$+id:welcome_text"
        ohos:top="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="prime|heart"
        ohos:margin="10vp"
        ohos:textual content="WELCOME"
        ohos:text_size="40fp"/>

    <Picture
        ohos:id="$+id:logo_image"
        ohos:top="match_content"
        ohos:width="match_content"
        ohos:image_src="https://style-tricks.com/hdgurkey/$media:emblem"
        ohos:layout_alignment="heart"/>


    <DirectionalLayout
        ohos:id="$+id:huawei_id_button_layout"
        ohos:top="match_content"
        ohos:width="match_parent"
        ohos:bottom_margin="20vp"
        ohos:layout_alignment="backside"
        ohos:orientation="vertical">

        <Button
            ohos:id="$+id:btn_hwid_sign_in"
            ohos:top="match_parent"
            ohos:width="match_content"
            ohos:background_element="$graphic:capsule_button_element"
            ohos:element_left="$media:icon_huawei"
            ohos:layout_alignment="horizontal_center"
            ohos:left_padding="16vp"
            ohos:min_width="280vp"
            ohos:padding="2vp"
            ohos:right_padding="16vp"
            ohos:textual content="Check in with HUAWEI ID"
            ohos:text_color="#ffffff"
            ohos:text_size="20fp"
            ohos:top_margin="5vp"/>

        <Button
            ohos:id="$+id:btn_hwid_sign_out"
            ohos:top="match_parent"
            ohos:width="match_content"
            ohos:background_element="$graphic:capsule_button_element"
            ohos:layout_alignment="horizontal_center"
            ohos:left_padding="16vp"
            ohos:min_width="280vp"
            ohos:padding="2vp"
            ohos:right_padding="16vp"
            ohos:textual content="Signal Out"
            ohos:text_color="#ffffff"
            ohos:text_size="20fp"
            ohos:top_margin="5vp"/>

        <Button
            ohos:id="$+id:btn_hwid_cancel_authorization"
            ohos:top="match_parent"
            ohos:width="match_content"
            ohos:background_element="$graphic:capsule_button_element"
            ohos:layout_alignment="horizontal_center"
            ohos:left_padding="16vp"
            ohos:min_width="280vp"
            ohos:padding="2vp"
            ohos:right_padding="16vp"
            ohos:textual content="Cancel Authorization"
            ohos:text_color="#ffffff"
            ohos:text_size="20fp"
            ohos:top_margin="5vp"/>
    </DirectionalLayout>

</StackLayout>
Enter fullscreen mode

Exit fullscreen mode

I can provide you a trick right here.
If the XML web page is open in DevEco Studio, the view in Previewer is predicated on the XML web page. If a Java class is open, the view within the Previewer additionally consists of the UI modifications made within the class.
Once I selected watch because the system on the previewer web page, I noticed that nothing was regular 🙂

Image description

Then I attempted to make modifications within the UI by writing the required codes within the Java class. You possibly can verify the code snippet under.

personal void responsiveDesign() {
    int deviceType = getResourceManager().getDeviceCapability().deviceType;
    if (deviceType == DeviceCapability.DEVICE_TYPE_WEARABLE) {
        // Wearable Format
        welcomeTextLogoWearableLayout.setVisibility(DirectionalLayout.VISIBLE);
        welcomeTextLogoWearableLayout.setMarginTop(10);
        huaweiIdButtonLayout.setMarginBottom(80);

        // Wearable Welcome Textual content
        welcome_text_wearable.setTextSize(15, Textual content.TextSizeType.FP);
        welcome_text_wearable.setMarginTop(35);
        welcome_text_wearable.setMarginBottom(5);
        welcome_text_wearable.setText("Welcome to Wearable");

        // Wearable Emblem
        logo_wearable.setHeight(80);
        logo_wearable.setWidth(80);
        logo_wearable.setScaleMode(Picture.ScaleMode.CLIP_CENTER);

        // Welcome Textual content
        welcome_text.setVisibility(DirectionalLayout.HIDE);

        // Wearable Emblem
        emblem.setVisibility(DirectionalLayout.HIDE);

        // Buttons
        signInWithHuaweiButton.setTextSize(12, Textual content.TextSizeType.FP);
        signInWithHuaweiButton.setHeight(60);
        signInWithHuaweiButton.setMinWidth(360);
        signInWithHuaweiButton.setPadding(20, 2, 20, 2);

        signOutWithHuaweiButton.setTextSize(12, Textual content.TextSizeType.FP);
        signOutWithHuaweiButton.setHeight(60);
        signOutWithHuaweiButton.setMinWidth(360);
        signOutWithHuaweiButton.setPadding(20, 2, 20, 2);

        cancelAuthWithHuaweiButton.setTextSize(12, Textual content.TextSizeType.FP);
        cancelAuthWithHuaweiButton.setHeight(60);
        cancelAuthWithHuaweiButton.setMinWidth(360);
        cancelAuthWithHuaweiButton.setPadding(20, 2, 20, 2);

    } else if (deviceType == DeviceCapability.DEVICE_TYPE_TABLET) {
        // Wearable Format
        welcomeTextLogoWearableLayout.setVisibility(DirectionalLayout.HIDE);

        // Welcome Textual content
        welcome_text.setVisibility(DirectionalLayout.VISIBLE);
        welcome_text.setText("Welcome to Pill");
        welcome_text.setTextSize(50, Textual content.TextSizeType.FP);
        welcome_text.setMarginTop(40);

        // Wearable Emblem
        emblem.setVisibility(DirectionalLayout.VISIBLE);

    } else if (deviceType == DeviceCapability.DEVICE_TYPE_PHONE) {
        // Wearable Format
        welcomeTextLogoWearableLayout.setVisibility(DirectionalLayout.HIDE);

        // Welcome Textual content
        welcome_text.setVisibility(DirectionalLayout.VISIBLE);
        welcome_text.setText("Welcome to Cellphone");
        welcome_text.setTextSize(35, Textual content.TextSizeType.FP);
        welcome_text.setMarginTop(60);

        // Wearable Emblem
        emblem.setVisibility(DirectionalLayout.VISIBLE);

    } else if (deviceType == DeviceCapability.DEVICE_TYPE_TV) {
        // Wearable Format
        welcomeTextLogoWearableLayout.setVisibility(DirectionalLayout.HIDE);

        // Welcome Textual content
        welcome_text.setVisibility(DirectionalLayout.VISIBLE);
        welcome_text.setText("Welcome to TV");
        welcome_text.setTextSize(65, Textual content.TextSizeType.FP);
        welcome_text.setMarginTop(60);

        // Wearable Emblem
        emblem.setVisibility(DirectionalLayout.VISIBLE);
        emblem.setHeight(400);
        emblem.setWidth(400);
        emblem.setScaleMode(Picture.ScaleMode.CLIP_CENTER);
    }
}
Enter fullscreen mode

Exit fullscreen mode

Right here is the ultimate design for all units:

Image description

After all, I labored on a quite simple design. You possibly can design way more complicated, significantly better UIs.
Now I’ll give details about responsive design on the JavaScript aspect. In JS initiatives, every web page consists of three constructions. HML, CSS and JS.
CSS properties are linked to the elements we use on the HML aspect with the category tag. For every device-specific design, we have to use the “media” tag on the CSS web page. You possibly can verify the code snippet under.

@media display screen and (device-type: pill) and (orientation: panorama) {
    .container {
        show: flex;
        justify-content: heart;
        align-items: heart;
    }
    .title {
        font-size: 80px;
    }
}

@media display screen and (device-type: wearable) {

    .container {
        show: flex;
        justify-content: heart;
        align-items: heart;
    }
    .title {
        font-size: 30px;
    }

}

@media display screen and (device-type: television) {
    .container {
        show: flex;
        justify-content: heart;
        align-items: heart;
    }
    .title {
        font-size: 120px;
    }
}

@media display screen and (device-type: telephone) and (orientation: panorama) {
    .container {
        show: flex;
        justify-content: heart;
        align-items: heart;
    }
    .title {
        font-size: 100px;
    }
}
Enter fullscreen mode

Exit fullscreen mode

Output
I shared screenshots of units utilizing Previewer above. However there can be those that surprise how it’s on actual units. So you possibly can try Watch3 and MatePad11.

Conclusion
You possibly can simply configure your app for any system within the HarmonyOS ecosystem. Additionally you might be giving your customers the possibility to have a pleasant end-to-end expertise. I hope will probably be a helpful article.

References
Java UI framework :
https://developer.harmonyos.com/en/docs/documentation/doc-guides/ui-java-overview-0000000000500404
JS UI framework :
https://developer.harmonyos.com/en/docs/documentation/doc-references/js-framework-file-0000000000611396

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?