Opening Templates:

Before adding code, create a subdirectory called "raw" within your app's default res directory to place template files (.BWT).

Initializing a Template Object:

1. Create a dropdown menu on the UI to allow users to select a template.
2. Populate the dropdown menu with the available file names.
3. When the user selects a template from the dropdown menu, open the resource as an InputStream
4. Call the SDKs "TemplateFactory.getTemplate(InputStream iStream, Context MainActivity.this)" method to retrieve the Template object

After successfully initializing the Template object, iterate through its placeholders to set any desired values. (Example: An EditText view to allow the user of the application to set the value of a specific field in the template.)

private void initializeDropdownMenu(){
        //Iterates through template files in the "raw" subdirectory to create a list of the file names.
        for(Field field : R.raw.class.getFields()) {
            TypedValue value = new TypedValue();
            int id = getResources().getIdentifier(field.getName(), "raw", getPackageName());
            context.getResources().getValue(id, value, true);
            String[] nameSplit = value.string.toString().split("/");
            fileList.add(templateName);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fileList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        templatesSpinner.setAdapter(adapter);

        //Triggers when a user selects an item in the drop down menu.
        templatesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                //The name of the selected template file we want to open.
                String selectedFileName = templatesSpinner.getSelectedItem().toString();
                //Initialize an input stream by opening the file that matches this name.
                InputStream iStream = getResources().openRawResource(getResources().getIdentifier(selectedFileName.split("[.]")[0], "raw", getPackageName()));
                //Call the SDK method ".getTemplate()" to retrieve its Template Object
                template = TemplateFactory.getTemplate(iStream, MainActivity.this);

                //Simple way to iterate through any placeholders to set desired values.
                for(TemplateObjectData placeholder : template.getTemplateData()){
                    messageEditText.setText(placeholder.getValue());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

Setting a Placeholder Value:

After a successful initialization of a "Template" object, use the getTemplateData() method to set a specific value to a placeholder on the template. This example shows how to iterate through all template data until we find the placeholder named "ExamplePlaceholder" and changes it to say "Example."

for (TemplateObjectData placeholder : template.getTemplateData()) {
    if (placeholder.getName() == "ExamplePlaceholder") {
        placeholder.setValue("Example");
    }
}

Previewing the Template:

  • Call template.getPreview(labelNumber, dpi, maxPixelWidthAndHeight) where:
    • labelNumber represents the 0-based index of the label (0 unless using a multi label template).
    • dpi represents the amount of dots per inch in the preview image (usually 96).
    • maxPixelWidthAndHeight represents the size of the ImageView on your UI.
public void previewTemplate(View view) {
    //"preview" is the ImageView object in our Activity.
    double elementSize = Math.min(preview.getWidth(), preview.getHeight());
    bitPreview = template.getPreview(0, 96, elementSize);
    preview.setImageBitmap(bitPreview);
}