Android Tutorial Designer Scripts Tutorial

Background

One of the most common issues that Android developers face is the need to adopt the user interface to devices with different screen sizes.
As described in the visual designer tutorial, you can create multiple layout variants to match different screens.
However it is not feasible nor recommended to create many layout variants.

Basic4android v1.9 introduces a new tool named "designer scripts" that will help you fine tune your layout and easily adjust it to different screens and resolutions.

The idea is to combine the usefulness of the visual designer with the flexibility and power of programming code.

You can write a simple script to adjust the layout based on the dimensions of the current device and immediately see the results. No need to compile and install the full program each time.

You can also immediately see the results on the abstract designer. This allows you to test your layout on many different screen sizes.

How to

Every layout file can include script code. The script is written inside the visual designer under the new Designer Scripts tab:

SS-2012-03-20_12.55.44.png


Their are two types of scripts: the general script that will be applied to all variants, and a script specific to the current variant.

Once you press on the Run Script button (or F5), the script is executed and the connected device / emulator and abstract designer will show the updated layout.

The same thing happens when you run your compiled program. The (now compiled) script is executed after the layout is loaded.

The general script is first executed followed by the variant specific script.

The script language is very simple and is optimized for managing the layout.

Lets start with an example.

Example

In this example we will build the following layout:
SS-2012-03-20_13.08.05.png


btnLeft and btnRight should be located in the top corners.
btnDown should be located at the bottom and fill the entire width.
ListView1 should fill the entire available area.
ToggleButton1 should be located exactly in the center.

The first step is to add the views and position them with the visual designer (you do not need to be 100% accurate).
Now we will select the designer scripts tab and add the code.
Note that the views are locked when the designer scripts tab is selected.

The code in this case is:
B4X:
'All variants script
btnRight.Right = 100%x

btnDown.Bottom = 100%y
btnDown.Width = 100%x

EditText1.Width = 100%x
EditText1.Bottom = btnDown.Top - 5dip

ListView1.Width = 100%x
ListView1.SetTopAndBottom(btnLeft.Bottom, EditText1.Top)

ToggleButton1.HorizontalCenter = 50%x
ToggleButton1.VerticalCenter = 50%y

The result:

SS-2012-03-20_15.10.06.png


10'' tablet:
SS-2012-03-20_15.11.16.png


SS-2012-03-20_15.35.16.png


Reference

The following properties are supported:

- Left / Right / Top / Bottom / HorizontalCenter / VerticalCenter - Gets or sets the view's position. The view's width or height will not be changed.

- Width / Height - Gets or Sets the view's width or height.

- TextSize - Gets or sets the text size. You should not used 'dip' units with this value as it is already measured in physical units.

- Text - Gets or sets the view's text.
TextSize and Text properties are only available to views that show text.

- Image - Sets the image file (write-only). Only supported by ImageView.

- Visible - Gets or sets the view's visible property.

Methods:

- SetLeftAndRight (Left, Right) - Sets the view's left and right properties. This method changes the width of the view based on the two values.

- SetTopAndBottom (Top, Bottom) - Sets the view's top and bottom properties. This method changes the height of the view based on the two values.

Keywords:

- Min / Max - Same as the standard Min / Max keywords.
- AutoScale - Autoscales a view based on the device physical size.
Example:
B4X:
AutoScale(Button1)
- AutoScaleAll - Autoscales all layout views.
Example:
B4X:
AutoScaleAll
- AutoScaleRate - Sets the scaling rate, a value between 0 to 1. The default value is 0.3
Example:
B4X:
AutoScaleRate(0.2)

- ActivitySize - Returns the approximate activity size measured in inches.

- If ... Then condition blocks - Both single line and multiline statements are supported. The syntax is the same as the regular If blocks.

- Landscape (v3.20) - Returns True if the current orientation is landscape.
- Portrait (v3.20) - Returns True if the current orientation is portrait.

Notes and tips

- %x and %y values are relative to the view that loads the layout.
Usually it will be the activity. However it you use Panel.LoadLayout then it will be relative to this panel.
Note that ScrollView inner panel width is set to -1. This is a special value that causes the panel to fill its parent available size.
If you want to load a layout file (with a script) to the inner panel then you will need to first set the panel width: ScrollView.Panel.Width = ScrollView.Width.

- Use 'dip' units for all specified sizes (except of TextSize). By using 'dip' units the values will be scaled correctly on devices with higher or lower resolution.

- In most cases it is not recommended to create variants with scales other than 1.0. When you add such a variant you will be given an option to add a normalized variant instead with a scale of 1.0.

- Variables - You can use variables in the script. You do not need to declare the variables before using them (there is no Dim keyword in the script).

- Activity.RerunDesignerScript (LayoutFile As String, Width As Int, Height As Int) - In some cases it is desirable to run the script code again during the program. For example you may want to update the layout when the soft keyboard becomes visible. Activity.RerunDesignerScript method allows you to run the script again and specify the width and height that will represent 100%x and 100%y. In order for this method to work all the views referenced in the script must be declared in Sub Globals.
Note that this method should not be used to handle screen orientation changes. In that case the activity will be recreated and the script will run during the Activity.LoadLayout call.

It is recommended to continue to this tutorial: http://www.b4x.com/forum/basic4andr...ing-multiple-screens-tips-best-practices.html
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
If you click on the Designer scripts and you don't see anything is because you have those views as Visibility = False

For example, say you have a panel and 3 buttons on the panel, if you set the panel's visibility on the designer to FALSE, when you click on the designer script tab you won't see anything.

If that's the case, I even requested a wish a little while ago as posted HERE, I'm assuming is on the To Do list.
 
Last edited:

Rusty

Well-Known Member
Licensed User
Longtime User
You are so right!
The views are set to visible = false and when I toggle them to true, they show up.
It would be good if this didn't happen, so I support your request for a fix.
Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
For example, say you have a panel and 3 buttons on the panel, if you set the panel's visibility on the designer to FALSE, when you click on the designer script tab you won't see anything.

If that's the case, I even requested a wish a little while ago as posted HERE, I'm assuming is on the To Do list.
This is actually "by design". In many cases showing the hidden views will hide the views in the back.
 

Rusty

Well-Known Member
Licensed User
Longtime User
Maybe you could toggle the hidden views on by holding the Alt key or something like that so you could see the views when someone wants to, but allow them to hide ordinarily.
Rusty
 

Dave O

Well-Known Member
Licensed User
Longtime User
designer-script-only properties

If I understand correctly, there are certain properties (e.g. Bottom) that are only valid in designer scripts, and not in the main code?

I only discovered this when I wrote "foobarLabel.Bottom = 10dip" in an activity module and got a compile-time error.

I scratched my head and read the docs, saw there was no actual Bottom property for views, and figured that Bottom (and some other properties) must have been added to designer scripting as a convenience.

Correct? :eek:
 

roarnold

Active Member
Licensed User
Longtime User
Thanks for the insight Erel. Helps with my current project and the ones upcoming.

R
 

ostau

Member
Licensed User
Longtime User
Get Error-Meassge when compiling

Hi Erel,
I switched from 1.7 to 2.3.
I adde a simple layout for tets purposes.
When I compile the program which has a layout with the script inside, I get an error message "javac: file not found: src\android\pmds\s2\designerscripts\*.java".
I can see the correct layout on the device , when I run F5 in the designer.
Any hints?
Thanks in advance, oskar
 

ostau

Member
Licensed User
Longtime User
Where to download?

This is related to a bug in Java compiler. A workaround was implemented in B4A v2.50. You can either use a different Java version or switch to v2.50.

Hi Erel,
thank you for the info. I didn´t find a link to download a full version of 2.50?
 

ostau

Member
Licensed User
Longtime User
Hi Erel,
thank you for the info. I didn´t find a link to download a full version of 2.50?

Thank you for the link. I updated to 2.50 and now I get the error during compilation:

src\android\pmds\s2\designerscripts\LS_optionen.java:51: error: ';' expected
views.get("btnok").setTop((int)((100d / 100 * height)-(10d * scale) - (views.get("btnok").getHeight())))
^

My Disignerscript is:

AutoScalerate(0.3)
AutoScaleAll

pnlURL.SetLeftAndRight(1%x, 99%x)
pnlURL.SetTopAndBottom(1%y, 1%y + 100dip)

lblUrl.SetLeftAndRight(0, pnlURL.Width)
lblUrl.SetTopAndBottom(0, 30dip)

editBaseURL.SetLeftAndRight(0 , pnlURL.width)
editBaseURL.SetTopAndBottom(lblUrl.Bottom + 5dip, pnlURL.Bottom -5dip)

pnlSeekHours.SetLeftAndRight (pnlURL.Left, pnlURL.Right)
pnlSeekHours.SetTopAndBottom (pnlURL.Bottom + 10dip, pnlURL.Bottom + 140dip)

lblSeekHours.SetLeftAndRight(0, pnlSeekHours.Width)
lblSeekHours.SetTopAndBottom(0, 40dip)

rad4h.SetLeftAndRight(0, pnlSeekHours.width /2)
rad4h.SetTopAndBottom(lblSeekHours.Bottom, lblSeekHours.Bottom + 40dip)

rad8h.SetLeftAndRight(pnlSeekHours.Width /2, pnlSeekHours.width)
rad8h.SetTopAndBottom(rad4h.Top, rad4h.Bottom)

rad16h.SetLeftAndRight(rad4h.Left, rad4h.Right)
rad16h.SetTopAndBottom(rad4h.Bottom, rad4h.Bottom + 40dip)

rad24h.SetLeftAndRight(rad8h.Left, rad8h.Right)
rad24h.SetTopAndBottom(rad16h.Top, rad16h.Bottom)

btnOK.HorizontalCenter = 50%x
btnOK.Height = 40dip
btnOK.Bottom(100%y - 10dip)
 

moster67

Expert
Licensed User
Longtime User
Thank you for the link. I updated to 2.50 and now I get the error during compilation:

src\android\pmds\s2\designerscripts\LS_optionen.java:51: error: ';' expected
views.get("btnok").setTop((int)((100d / 100 * height)-(10d * scale) - (views.get("btnok").getHeight())))
^

My Disignerscript is:

...
btnOK.Bottom(100%y - 10dip)

Change last one to:
B4X:
btnOK.Bottom = (100%y - 10dip)
 

Allan Cameron

Member
Licensed User
Longtime User
Designer Script

I would like to vary the height of a label in the designer script according to the length of the text--for different language versions. What is the best way to achieve this as it does not seem possible to use If Then based on a value within the main program.
 

lock255

Well-Known Member
Licensed User
Longtime User
Hello everyone today I was setting my layout (buttons, labels, etc.) according to this thread, but every time that I update with F5, my items are changed in everything except in VerticalCenter.

This is one of my codes, I can not figure out where mistake, they could help me?

B4X:
btnClear.HorizontalCenter=80%x
btnClear.VerticalCenter=60%y
btnClear.Width=22%x
btnClear.Bottom=8%y
 

lock255

Well-Known Member
Licensed User
Longtime User
B4X:
btnClear.VerticalCenter=60%y
btnClear.Bottom=8%y
are you sure that it should't be
B4X:
btnClear.Height=8%y
btnClear.VerticalCenter=60%y
Best regards

I solved thanks, please can you explain what is VerticalCenter?
 
Top