Search…
Button
Buttons are an essential way to interact with and navigate through an app, and should clearly communicate what action will occur after the user taps them

Examples

To adapt the component to the style of the application it is often necessary to change its colors.
1
package com.totalcross;
2
import totalcross.ui.gfx.Color;
3
import totalcross.sys.Settings;
4
import totalcross.ui.Button;
5
import totalcross.ui.MainWindow;
6
public class HelloWorld extends MainWindow {
7
8
private Button btnRed;
9
private Button btnGreen;
10
private Button btnBlue;
11
public HelloWorld(){
12
setUIStyle(Settings.MATERIAL_UI);
13
}
14
@Override
15
public void initUI(){
16
btnRed = new Button("Red");
17
btnRed.setBackForeColors(Color.RED, Color.WHITE);
18
add(btnRed, CENTER,CENTER );
19
20
btnGreen = new Button("Green");
21
btnGreen.setBackForeColors(Color.GREEN, Color.WHITE);
22
add(btnGreen, CENTER, AFTER );
23
24
btnBlue = new Button("Blue");
25
btnBlue.setBackForeColors(Color.BLUE, Color.WHITE);
26
add(btnBlue, CENTER,AFTER);
27
}
28
}
Copied!

Full Button

Make the button the width of the screen.
1
package com.totalcross;
2
import totalcross.ui.gfx.Color;
3
import totalcross.sys.Settings;
4
import totalcross.ui.Button;
5
import totalcross.ui.MainWindow;
6
public class HelloWorld extends MainWindow {
7
8
private Button btnFull;
9
10
public HelloWorld(){
11
setUIStyle(Settings.MATERIAL_UI);
12
}
13
@Override
14
public void initUI() {
15
btnFull = new Button("Full Button");
16
btnFull.setBackForeColors(Color.BLUE, Color.WHITE);
17
add(btnFull, CENTER, CENTER, PARENTSIZE, PREFERRED);
18
19
}
20
}
Copied!

Button shapes

Modify the button edges.
1
package com.totalcross;
2
import totalcross.ui.gfx.Color;
3
import totalcross.sys.Settings;
4
import totalcross.ui.Button;
5
import totalcross.ui.MainWindow;
6
public class HelloWorld extends MainWindow{
7
8
private Button btnRounded;
9
private Button btnBorderless;
10
private Button btnOutlined;
11
12
public HelloWorld(){
13
setUIStyle(Settings.MATERIAL_UI);
14
}
15
@Override
16
public void initUI(){
17
btnRounded = new Button("Rounded Corners Button", Button.BORDER_ROUND);
18
btnRounded.setBackForeColors(Color.BLUE, Color.WHITE);
19
add(btnRounded, CENTER, CENTER );
20
21
btnBorderless = new Button("Borderless Button", Button.BORDER_NONE);
22
btnBorderless.setBackForeColors(Color.BLUE, Color.WHITE);
23
add(btnBorderless, CENTER, AFTER+5);
24
25
btnOutlined = new Button("Outlined Button", Button.BORDER_OUTLINED);
26
btnOutlined.setBackForeColors(Color.BLUE, Color.WHITE);
27
add(btnOutlined, CENTER, AFTER+5);
28
29
}
30
}
Copied!

Sizes

Change the buttons sizes.
1
package com.totalcross;
2
import totalcross.ui.gfx.Color;
3
import totalcross.sys.Settings;
4
import totalcross.ui.Button;
5
import totalcross.ui.MainWindow;
6
public class HelloWorld extends MainWindow {
7
8
private Button btnLarge;
9
private Button btnDefaultSize;
10
private Button btnSmall;
11
12
public HelloWorld() {
13
setUIStyle(Settings.MATERIAL_UI);
14
}
15
@Override
16
public void initUI() {
17
18
btnLarge = new Button("Large",Button.BORDER_ROUND);
19
btnLarge.setBackForeColors(Color.BLUE, Color.WHITE);
20
add(btnLarge, LEFT+20, CENTER,btnLarge.getPreferredWidth() <= 48 ? DP + 96: btnLarge.getPreferredWidth(),DP + 54);
21
22
btnDefaultSize = new Button("Default",Button.BORDER_ROUND);
23
btnDefaultSize.setBackForeColors(Color.BLUE, Color.WHITE);
24
add(btnDefaultSize, AFTER+5, CENTER_OF);
25
26
btnSmall = new Button("Small",Button.BORDER_ROUND);
27
btnSmall.setBackForeColors(Color.BLUE, Color.WHITE);
28
add(btnSmall, AFTER+5, CENTER_OF, btnSmall.getPreferredWidth() <= 24? DP + 48 : btnSmall.getPreferredWidth(), DP + 27,btnDefaultSize);
29
30
}
31
}
Copied!

Button image

Add an image to the button.
1
package com.totalcross;
2
import totalcross.ui.gfx.Color;
3
import totalcross.ui.image.Image;
4
import totalcross.sys.Settings;
5
import totalcross.ui.Button;
6
import totalcross.ui.MainWindow;
7
public class HelloWorld extends MainWindow {
8
9
private Button btnLeftImage;
10
11
public HelloWorld() {
12
setUIStyle(Settings.MATERIAL_UI);
13
}
14
@Override
15
public void initUI() {
16
try {
17
Image img = new Image("images/bt_info.png");
18
btnLeftImage = new Button("Left Image", img.scaledBy(0.2,0.2), RIGHT,10);
19
btnLeftImage.setBackForeColors(Color.BLUE, Color.WHITE);
20
add(btnLeftImage, CENTER, AFTER+25);
21
} catch (Exception exception) {
22
exception.printStackTrace();
23
}
24
}
25
}
Copied!
Do not forget to create a folder called "images" inside /src/main/resources and save the bt_info.png image inside it [images].

Events

Handling events with addPressListener() :
1
package com.totalcross;
2
import totalcross.ui.gfx.Color;
3
import totalcross.sys.Settings;
4
import totalcross.ui.Button;
5
import totalcross.ui.MainWindow;
6
public class HelloWorld extends MainWindow {
7
8
private Button btnRed;
9
private Button btnGreen;
10
private Button btnBlue;
11
public HelloWorld(){
12
setUIStyle(Settings.MATERIAL_UI);
13
}
14
@Override
15
public void initUI(){
16
btn = new Button("Do something");
17
btn.setBackForeColors(Color.RED, Color.WHITE);
18
btn.addPressListener((event) -> {
19
// DO SOMETHING
20
})
21
add(btn, CENTER,CENTER );
22
}
23
}
Copied!

Behind the Class

Attributes

Type
Name
Description
boolean
Button.CENTRALIZE
Center image and text on the button.

Methods

Type
Name
Description
Constructor
Button( )
Creates a simple button
Constructor
Button(Image img)
Creates a simple button with the referred image
Constructor
Button(Image img, byte border)
Creates a button with the referreds image and border
Constructor
Button(String text)
Creates a button with the referred text
Constructor
Button(String text, byte border)
Creates a button with the referreds text and border
Constructor
Button(String text, Image img, int textPosition, int gap)
Creates a button with the referred text and image
Image
getImage( )
Return the button image
String
getText( )
Return the button text
Boolean
isPressed( )
Return true if button is pressed
void
press(boolean pressed)
If true, press the button
void
setBorder(byte border)
Set the button border style
void
setImage(Image img)
Set the button image
void
setPressedColor(int newColor)
Return the button text
void
setText(String text)
Set the button text
void
simulatePress( )
Press and depress the button

References

Last modified 1yr ago