How to change text

417 views
Skip to first unread message

OrrinOliver

unread,
Sep 24, 2018, 6:12:23 PM9/24/18
to Blockly
So we have a block that has a left connection and we are having that block be created for each of our connected devices during run-time.  Each of the devices have their own names and I'm having trouble figuring out if we can change the text during run-time.  I am able to get each block to be created with no problems it's just figuring out how to change the text.

I have an example of the block in the image below (The part we want changed is the text that says "How to change me").  What I've noticed is that in the Block definition area it is not even mentioning the text part.

We are using Android Studio for our mobile app if that makes a difference.
textBlock.png

Neil Fraser

unread,
Sep 25, 2018, 12:34:17 AM9/25/18
to blo...@googlegroups.com
You current JSON definition is:

{
"type": "block_type",
"message0": "Change this text %1",
"args0": [
{
"type": "input_value",
"name": "NAME"
}
],
"output": null
}

The above code implicitly creates a label field with the text "Change
this text". However, this label field is unnamed, and thus cannot be
referenced if you wish to change it. In order to name a label field,
you can create it explicitly:

{
"type": "block_type",
"message0": "%1 %2",
"args0": [
{
"type": "field_label",
"text": "Change this text",
"name": "LABEL"
},
{
"type": "input_value",
"name": "NAME"
}
],
"output": null
}

Now you can set the text with:

block.setFieldValue('Hello', 'LABEL');

Hmm, named labels ought to be added to the documentation:
https://developers.google.com/blockly/guides/create-custom-blocks/define-blocks#label
> --
> You received this message because you are subscribed to the Google Groups "Blockly" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Neil Fraser
https://neil.fraser.name

OrrinOliver

unread,
Sep 25, 2018, 2:16:35 PM9/25/18
to Blockly
Awesome, So I have it now set up that the label is in the JSON portion and am able to set the text from there, but when I'm trying to have it change the label in the onInitBlankWorkspace() portion of the code I get this error:
"java.lang.IllegalStateException: Label field text cannot be set after construction."

Here is the part of the code when I'm adding the block to the toolbox:

@Override
protected void onInitBlankWorkspace(){
try {
        getController().loadToolboxContents("<xml>"
            +   "<block type=\"bot\">"
+ "<field name=\"BotName\">" + "NAME GOES HERE" + "</field>"
+ "</block>";
            + "</xml>");

} catch (BlockLoadingException e) {
e.printStackTrace();
    }
}
If I comment out the line with the field name it will work fine.

Andrew n marshall

unread,
Sep 26, 2018, 12:30:27 PM9/26/18
to blo...@googlegroups.com

The Java Android implementation is more restrictive than the JavaScript implementation. You will need to rebuild the Input with a new FieldLabel. The new Input can reuse the fields that did not change.

Further, if you expect to save this info in the workspace (whether Java Android or JavaScript for the web implementation) the such a block definition will need to have a mutator in order to save and restore correctly. Otherwise, upon restoring the workspace, the block will not know what label it should have.

For this reason, if the list of possible devices is known and finite, you may want to consider just creating a block definition for each device type.  This would avoid the mutator.

A third option would be to use a dropdown list of devices, possibly dynamically populated. Such an editable field would know how to save itself, and also avoid the mutator. This approach is advisable if there is sufficient similarity in how devices are used, such that a user may want to copy an algorithm, changing only the device connected.

Regardless of the approach you take for the blocks, it sounds like you will need a dynamic toolbox to list the correct block for the currently available devices.



--

OrrinOliver

unread,
Oct 29, 2018, 10:04:00 PM10/29/18
to Blockly
Okay so if we were to go with the dropdown menu how would we be able to populate the list either when you drag the block onto the workspace or when it is first loaded into the toolbox? Is there an example somewhere that can help me out?

Erik Pasternak

unread,
Oct 30, 2018, 1:06:13 PM10/30/18
to Blockly
Hi Orrin,

Unfortunately, we never finished building dynamic dropdowns in the Android library. You could use mutations to add it in, but it'll be a bit messy.

Also, in case you missed the announcement, the Android library has been deprecated so this feature won't be added unless someone in the community makes a PR for it (we are still reviewing PRs on Android).

Cheers,
Erik
Reply all
Reply to author
Forward
0 new messages