access widgets in code while the application is running

36 views
Skip to first unread message

Александр Страшко

unread,
Oct 6, 2017, 12:30:46 AM10/6/17
to App Inventor Open Source Development
Hi

Is there a way to access the button on Screen1 while the application is running?  Using container.$form () I can change the properties of Screen1, but how can I change the property of a button on Screen1?

Moliata

unread,
Oct 6, 2017, 8:56:34 AM10/6/17
to App Inventor Open Source Development
Well, actually I don't think so, but you can use getView() to get Screen and theb yiu can change it's background color, title bar visibility, other properties...

Moliata

unread,
Oct 6, 2017, 8:58:33 AM10/6/17
to App Inventor Open Source Development
Also to change button properties do this:

@SimpleFunction
public void SetButtonText(Button button, String text) {
Button mButton = (Button) button.getView();
mButton.setText(text);
}

Александр Страшко

unread,
Oct 6, 2017, 10:00:39 AM10/6/17
to App Inventor Open Source Development
Thanks, Moliata

I figured out the situation.  At first it seemed to me that the built-in App Inventor objects are java-objects.

But it turned out that these are just structural elements created on runtime and referring to them as objects does not make sense.

The object is only Screen methods which can be called at the stage of the application execution.

Evan Patton

unread,
Oct 6, 2017, 12:56:32 PM10/6/17
to App Inventor Open Source Development
The most appropriate thing here would be to make your extension take the Button component as a property, and then users of your extension will set that property in the designer. This way you can use @Moliata's suggestion of getting the view from the button in order to manipulate it at runtime.

Evan

Александр Страшко

unread,
Oct 6, 2017, 2:36:05 PM10/6/17
to App Inventor Open Source Development
Thanks, Evan

Once the code @Moliata is not compiled, but I'll look it closely.

Evan Patton

unread,
Oct 6, 2017, 2:46:18 PM10/6/17
to App Inventor Open Source Development
If you get an error about Button not convertible to a YAIL type, then you may need to rebase your code onto the ucr branch, which fixes this problem.

Evan

Александр Страшко

unread,
Oct 6, 2017, 3:15:57 PM10/6/17
to App Inventor Open Source Development
Thanks, Evan

I understood!

Moliata

unread,
Oct 6, 2017, 3:22:32 PM10/6/17
to App Inventor Open Source Development
If you are building not an extension, but a component, make sure to check following commit & make sure, to add following stuff to ComponentProcessor.java: https://github.com/ewpatton/appinventor-sources/commit/4b9aad2a3edaf049e02406e8f36da9babaf1ef08

2017 m. spalis 6 d., penktadienis 22:15:57 UTC+3, Александр Страшко rašė:
Thanks, Evan

I understood!

Александр Страшко

unread,
Oct 6, 2017, 3:38:51 PM10/6/17
to App Inventor Open Source Development
Hmm.. When compiling the code


@SimpleFunction
public void SetButtonText(Button button, String text) {
Button mButton = (Button) button.getView();
mButton.setText(text);
}

an error occurs: cannot find symbol in line Button mButton = (Button) button.getView();
                                                                                                              ^

Александр Страшко

unread,
Oct 6, 2017, 3:40:19 PM10/6/17
to App Inventor Open Source Development
I'll check it now

Moliata

unread,
Oct 6, 2017, 3:45:15 PM10/6/17
to App Inventor Open Source Development
Did you import com.google.appinventor.runtime.Button ?

Александр Страшко

unread,
Oct 6, 2017, 4:05:16 PM10/6/17
to App Inventor Open Source Development
Yes add import com.google.appinventor.components.runtime.Button

import com.google.appinventor.runtime.Button - I have no such package in source dir.

I'm creating an extension



Evan Patton

unread,
Oct 6, 2017, 4:07:25 PM10/6/17
to App Inventor Open Source Development
Be careful of your imports. There are two Button types involved with this. There is android.widget.Button and com.google.appinventor.components.runtime.Button. The latter is the argument to the function, and the other is the thing being operated on. Because they both have the same name, to prevent a collision you will need to reference one of them by the fully qualified class name.

Alternatively, at least for this particular instance you could use the Text() method of the App Inventor Button type to change the text without having to mix the two types.

Evan

Александр Страшко

unread,
Oct 6, 2017, 4:24:08 PM10/6/17
to App Inventor Open Source Development
Yes, I understood with the import Button.

Александр Страшко

unread,
Oct 6, 2017, 4:55:14 PM10/6/17
to App Inventor Open Source Development
Many thanks to Moliata and Evan!  The following code is executed:

    @SimpleFunction
    public void SetButText(Button button, String text) {
    Button mButton =  button;
        mButton.Text(text);
    }   

Moliata

unread,
Oct 6, 2017, 5:07:59 PM10/6/17
to App Inventor Open Source Development
This will only allow to change properties that Button have, whether you will want to change something more, make sure to use getView() & example I showed before.

Александр Страшко

unread,
Oct 7, 2017, 2:00:52 AM10/7/17
to App Inventor Open Source Development
Yes it's understandable

Andres Cotes

unread,
Oct 12, 2017, 9:50:55 PM10/12/17
to App Inventor Open Source Development
You need put buttonbase class
Reply all
Reply to author
Forward
0 new messages