Search code examples
androidwear-osandroid-wear-2.0watch-face-apiandroid-wear-complication

How to add complications by default in wearOS android


Developer.android says:

void setDefaultSystemComplicationProvider (int watchFaceComplicationId, int systemProvider, int type)

but does not works for me.


Solution

  • As a beginner Android developer and a lack of community support and tutorials available for the Wear OS platform I was struggling to set complications on Wear OS by default. I have somehow managed to make that method work for me with a few adjustment to my code. I would like to share to whom it may help. This is the method i have used:

    private void initializeComplications() {
        Log.d("complication", "initializeComplications()");
    
        mActiveComplicationDataSparseArray = new SparseArray<>(COMPLICATION_IDS.length);
    
        ComplicationDrawable leftComplicationDrawable =
                (ComplicationDrawable) getDrawable(R.drawable.custom_complication_styles);
        leftComplicationDrawable.setContext(getApplicationContext());
    
        ComplicationDrawable rightComplicationDrawable =
                (ComplicationDrawable) getDrawable(R.drawable.custom_complication_styles);
        rightComplicationDrawable.setContext(getApplicationContext());
    
        ComplicationDrawable topComplicationDrawable =
                (ComplicationDrawable) getDrawable(R.drawable.custom_complication_styles);
        topComplicationDrawable.setContext(getApplicationContext());
    
        mComplicationDrawableSparseArray = new SparseArray<>(COMPLICATION_IDS.length);
        mComplicationDrawableSparseArray.put(LEFT_COMPLICATION_ID, leftComplicationDrawable);
        mComplicationDrawableSparseArray.put(RIGHT_COMPLICATION_ID, rightComplicationDrawable);
        mComplicationDrawableSparseArray.put(TOP_COMPLICATION_ID, topComplicationDrawable);
    
        // Set default complication
        // This time we don't create a setting screen so we need to set it here
        setDefaultSystemComplicationProvider (
                LEFT_COMPLICATION_ID,
                SystemProviders.STEP_COUNT,
                ComplicationData.TYPE_SHORT_TEXT);
    
        setDefaultSystemComplicationProvider (
                RIGHT_COMPLICATION_ID,
                SystemProviders.WATCH_BATTERY,
                ComplicationData.TYPE_SHORT_TEXT);
    
        setDefaultSystemComplicationProvider (
                TOP_COMPLICATION_ID,
                SystemProviders.DATE,
                ComplicationData.TYPE_SHORT_TEXT);
    
        setActiveComplications(COMPLICATION_IDS);
    }
    

    and lastly I have called it in:

    public void onCreate(SurfaceHolder holder) {
    
        setWatchFaceStyle(new WatchFaceStyle.Builder(DigitalPrayerTimes.this)
                .setAcceptsTapEvents(true)
                .build());
    
        // TODO: Step 2, intro 3
        initializeComplications();
    }