public class

Button

extends View
java.lang.Object
   ↳ com.neomades.ui.View
     ↳ com.neomades.ui.Button

Class Overview

A Button represents a button into screen in order to intercept events (touch and keys).

Handling click events

A typical use of Button could be :

 ClickListener clickListener = new ClickListener() {
 	public void onClick(View view) {
 		// do some stuff
 	}
 };
 Button myButton = new Button("Text of the button");
 myButton.setClickListener(clickListener);
 layout.addView(myButton);
 

Default properties

By default, the following properties are applied to a new Button:

  • focusable (limited to some devices)
  • clickable
  • enabled
  • no text
  • no icon
  • width content stretch : MATCH_PARENT.
  • height content stretch : MATCH_CONTENT.
  • content alignment : HCENTER|VCENTER.

Cross Platform Considerations

In Android, a Button is a android.widget.Button. It will contain text. Use background methods (such as setBackgroundImage(int)) to customize the visual aspect.

In iOS, a Button is a UIButton with style UIButtonStyleRoundedRect. This default style will represent the Button as a button with rounded corners. Using a customized background will replace the default style (the default border and lines will be hidden).

Summary

XML Attributes
Attribute Name Related Method Description
contentAlignment setContentAlignment(int) A combination of alignment values (eg: TOP|LEFT)  
font setFont(Font) Font resource id: Res.font.FONT_XXXX  
text setText(int) Res.string.XXXX or a text  
textColor setTextColor(Color) #AARRGGBB or a color  
textSize setTextSize(int) a size in dp  
[Expand]
Inherited XML Attributes
From class com.neomades.ui.View
[Expand]
Inherited Constants
From interface com.neomades.ui.Alignment
From interface com.neomades.ui.StretchMode
Public Constructors
Button()
Creates a button without text or image.
Button(String text)
Creates a button with text.
Button(int textId)
Creates a button with an ID representing a String.
Public Methods
int getContentAlignment()
Returns the current text alignment of the button
String getText()
Returns the button's text.
void setClickListener(ClickListener clickListener)
Sets a ClickListener to this button.
void setContentAlignment(int alignment)
Sets the alignment of the text or icon in the button.
void setFont(Font font)
Sets the button text font.
void setText(String text)
Sets the text of the button

If the text parameter is null, no text is set.

void setText(int resId)
Sets the text of the button with an ID representing a String
void setTextColor(Color textColor)
Sets the button text color.
void setTextSize(int sizeInDp)
Sets the text size to a given value (in dp).
[Expand]
Inherited Methods
From class com.neomades.ui.View
From class java.lang.Object

XML Attributes

contentAlignment

A combination of alignment values (eg: TOP|LEFT)

Related Methods

font

Font resource id: Res.font.FONT_XXXX

Related Methods

text

Res.string.XXXX or a text

Related Methods

textColor

#AARRGGBB or a color

Related Methods

textSize

a size in dp

Related Methods

Public Constructors

public Button ()

Creates a button without text or image.

For further information, see Default properties

public Button (String text)

Creates a button with text.

For further information, see Default properties

If the text parameter is null, no text is set.

Parameters
text the text content of this button

public Button (int textId)

Creates a button with an ID representing a String.

For further information, see Default properties

Parameters
textId the string id assigned to this button

Public Methods

public int getContentAlignment ()

Returns the current text alignment of the button

Related XML Attributes
Returns
  • the integer combination of values from Alignment

public String getText ()

Returns the button's text. Returns an empty string ("") if there is no text.

Related XML Attributes
Returns
  • the button's text. Returns an empty string ("") if there is no text.

public void setClickListener (ClickListener clickListener)

Sets a ClickListener to this button.

When the button is clicked, the onClick() method from ClickListener will be called, with the clicked button as parameter.

Parameters
clickListener The listener to set on the button (or null to remove the current listener)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setContentAlignment (int alignment)

Sets the alignment of the text or icon in the button. The parameter is a combination of values from Alignment

Related XML Attributes
Parameters
alignment The new alignment of the button's text
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setFont (Font font)

Sets the button text font.

Related XML Attributes
Parameters
font new font for the text
Throws
CrossThreadException if the method is called from outside the UI-Thread
NullPointerException if the font parameter is null

public void setText (String text)

Sets the text of the button

If the text parameter is null, no text is set.

Related XML Attributes
Parameters
text the new button text
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setText (int resId)

Sets the text of the button with an ID representing a String

Related XML Attributes
Parameters
resId the string id assigned to this button
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setTextColor (Color textColor)

Sets the button text color.

Related XML Attributes
Parameters
textColor new color for the text
Throws
CrossThreadException if the method is called from outside the UI-Thread
NullPointerException if the parameter is null

public void setTextSize (int sizeInDp)

Sets the text size to a given value (in dp).

Related XML Attributes
Parameters
sizeInDp size to set to the text