Back

CreateText

Hello World
Content:
- Methods

If you want to display text in your app you can use the Text object.

txt = app.CreateText( text, width, height, options ) → app object: Text

You can use the “Multiline” option to allow multi-line text and the “Left” or “Right” options to align the text. The default is single line centered text.
Other options you will see used in the samples include “AutoScale”, “Bold”, “Html” and “FontAwesome”.
Use the SetText and GetText functions of the Text object to set and get it's text.

Example - Single line

function OnStart()
{
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

    txt = app.CreateText( "Hello" );
    txt.SetTextSize( 22 );
    lay.AddChild( txt );


    app.AddLayout( lay );
}
    Copy     Copy All       Run      

You can change the look of a Text using the SetBackColor and SetTextColor functions on the Text object.

Example - Multi line

function OnStart()
{
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

    txt = app.CreateText( "Hello\nWorld", 0.8, 0.2, "Multiline" );
    txt.SetTextSize( 12 );
    txt.SetTextColor( "#ff6666ff" );
    txt.SetBackColor( "#ffffffff" );
    lay.AddChild( txt );


    app.AddLayout( lay );
}
    Copy     Copy All       Run      

Text can be shortened using ellipsis (...) when the size of the control is too small to show all the text. Use the SetEllipsize to control where the ellipsis is placed. Available modes are “start”, “middle”, “end” and “marq”.

Example - Ellipsize

function OnStart()
{
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

    txt = app.CreateText( "The quick brown fox jumps over the lazy dog", 0.75 );
    txt.SetTextSize( 28 );
    txt.SetEllipsize( "end" );
    lay.AddChild( txt );


    app.AddLayout( lay );
}
    Copy     Copy All       Run      

You can also set a background image/pattern or background gradient for the Text using the SetBackground and SetBackGradient functions. See Layouts for examples of how to use these functions.

Methods

The following methods are available on the Text object:

Focus()
GetAbsHeight() → number: integer
GetAbsWidth() → number: integer
GetHeight( options ) → number
GetHtml() → string: html code
GetLeft( options ) → number
GetLineCount() → number: integer
GetLineStart( line ) → number: integer
GetLineTop( line ) → number: fraction (0..1)
GetMaxLines() → number: integer
GetParent() → app object
GetPosition( options ) → object: { left, top, right, bottom }
GetText() → string
GetTextSize( mode ) → number
GetTop( options ) → number
GetType() → string: “Text”
GetVisibility() → string: “Show” or “Hide” or “Gone”
GetWidth( options ) → number
Gone()
Hide()
IsEnabled() → boolean
IsOverlap( obj, depth ) → boolean
IsVisible() → boolean
Method( name, types, p1, p2, p3, p4 ) → all types
SetScale( x, y )
Show()
boolean
app object
number
string
unknown
number: factor
number: fraction (0..1)
number: integer
number: milliseconds
number: -180..180
number: -100..100
number: 0..100
number: 0..0.99 or 1..256
string:
  hexadecimal: “#rrggbb”, “#aarrggbb”
  colourName: “red”, “green”, ...
string: comma “,” separated
string: html code
string: path to file or folder ( “/absolute/...” or “relative/...” )
string: comma “,” separated: “AutoScale”, “AutoSize”, “Multiline”, “Left” or “Right”, “Html”, “Bold”, “Monospace”, “NoWrap”, “Log”, “FillX/Y”
string: “px”
string: “screen”, “px”
string: comma “,” separated: Clear, “Red” or “Green” or “Blue”
string: “left-right” or “right-left” or “top-bottom” or “bottom-top” or “bl-tr” or “br-tl” or “tl-br” or “tr-bl”
string: “repeat”
string: “Add” or “Multiply” or “clear” or “darken” or “lighten” or “overlay” or “screen” or “xor” or “src_in” or “src_out” or “src_atop” or “src_over” or “dst_in” or “dst_out” or “dst_atop” or “dst_over”
string: “start” or “middle” or “end”
string: “px” or “sp” or “dip” or “mm” or “pt”
string: “px” or “sp” or “dip” or “dp” or “mm” or “pt”
string: “px” or “dip” or “sp” or “mm” or “pt” or pl or ps
string: “Show” or “Hide” or “Gone”
string: “Linear.None” or “Quadratic.In/Out” or “Cubic.In/Out” or “Quartic.In/Out” or “Quintic.In/Out” or “Sinusoidal.In/Out” or “Exponential.In/Out” or “Circular.In/Out” or “Elastic.In/Out” or “Back.In/Out” or “Bounce.In/Out”
object: { source, action, count, x: [ x1, x2, x3 ], y: [ y1, y2, y3 ] }
object: { source, action, count, x: [ x1, x2, x3 ], y: [ y1, y2, y3 ] }
object: { source, action, count, x: [ x1, x2, x3 ], y: [ y1, y2, y3 ] }
object: { source, action, count, x: [ x1, x2, x3 ], y: [ y1, y2, y3 ] }
object: { x, y, w, w, sw, sh, rot }
list: boolean,char,byte,short,int,long,float,double,String,CharSequence,...
function( src )
function( event )
function( event )
function( event )
function( event )
function()
txt.AdjustColor
Adjust the visual color effect of the control by setting the Hue (by angle in degrees in a color circle), the saturation, brightness and contrast of the control.
txt.ClearFocus
Removes the focus of the control so that the user no longer has immediate access to it.
txt.Focus
Set the focus to the control so that the user can interact with it immediately.
txt.GetAbsHeight
Get the absolute height of the control in pixels.
txt.GetAbsWidth
Get the absolute width of the control in pixels.
txt.GetHeight
Get the height of the control as screen height relative float or in pixels with the px option.
txt.GetHtml
Returns the html formatted content.
txt.GetLeft
Get the distance from the control to the left parent border as width relative float or in pixels with the px option.
txt.GetLineCount
Returns the amount of lines in the text even with html formatting
txt.GetLineStart
Returns at which character in the text a certain line begins.
txt.GetLineTop
Returns the top position of a given line on the screen.
txt.GetMaxLines
Returns the maximum amount of lines the control can hold with the current control and font height
txt.GetParent
Returns the parent control object where the object was added to - commonly a layout.
txt.GetPosition
Returns data about the position and size of the control.
If the screen option is given the position on the screen will be returned. Otherwise relative to the parent control.
The px options turns the relative values into pixels.
txt.GetText
Returns the current displayed text of the control.
txt.GetTextSize
Returns the current size of the contained text. If the px option is given the size will be retured in pixels.
txt.GetTop
Get the distance from the control to the upper parent border as height relative float or in pixels with the px option.
txt.GetType
Returns the control class name.
txt.GetVisibility
Returns the current visibility state of the control. The Values are:
Show: visible
Hide: invisible but still consuming space
Gone: invisible and not consuming space
txt.GetWidth
Get the width of the control as screen width relative float or in pixels with the px option.
txt.Gone
Hides the control without consuming any more layout space as if it were never there.
txt.Hide
Hide the control but keep the layout space free.
txt.IsEnabled
Returns whether the control is currently useable by the user.
txt.IsOverlap
Returns whether the control overlaps with another by a given distance.
txt.IsVisible
Returns whether the control is currently visible to the user, ignoring overlaying controls.
txt.Log
Append a string to the text separated with a line break
txt.Method
Allows access to other functions defined on the object in Java via reflection.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.
txt.SetBackAlpha
Set the transparency of the background by an alpha value between 0 (no transparency) and 0.99 (full transparent) or 1 (no transparency) and 256 (full transparent)
txt.SetBackColor
Changes the background color of the control.
txt.SetBackGradient
Define the background color of the control with a gradient. The default gradient direction is from top to bottom, but you can change it from left to right and the reversed versions of course.
txt.SetBackGradientRadial
Define a radial color gradient for the background of control.
txt.SetBackground
Changes the background to an image which can be repeated using the repeat option.
An image which is often used with that option is '/res/drawable/pattern_carbon' - try it out!
txt.SetColorFilter
Adjust the visual color effect with a color and a given BlendMode. More information about BlendMode can be found in the Android Developer page.
txt.SetEllipsize
Will cause the inner text to be broken with ... at the start or the end if it cannot fit in the control.
txt.SetEnabled
En/Disable the control physically and visually so that the user can/can not access the control. Events like OnTouch will still be fired.
txt.SetFontFile
Change the font style by defining a font file.
txt.SetHtml
Change the current text of the control to html-formatted text.
txt.SetLog
Define a maximum amount of log lines
txt.SetMargins
Define a distance to other controls on each side of the control.
txt.SetOnLongTouch
Define a callback function which is called when the object has been long pressed.
txt.SetOnTouch
Define a callback function that is called when the user touches the control. In addition, an event object is passed to the callback function to obtain information about the touch type, the touch position(s), the amount of touches and the control that was touched.
txt.SetOnTouchDown
Define a callback function which is called when the user started toching the control.
txt.SetOnTouchMove
Define a callback function which is called when the user drags his finger over the screen.
txt.SetOnTouchUp
Define a callback function which is called when the users finger leaves the screen.
txt.SetPadding
Define distances that elements within the control are to maintain from the control borders.
txt.SetPosition
Defines the position and size for the control if the parent is an absolute layout.
txt.SetScale
Scales the control along with its contents by the factors passed to the function.
txt.SetSize
Change the size of the control in either screen relative values or in pixels if the px option was given.
txt.SetText
Change the currently displayed text in the control.
txt.SetTextShadow
Define a shadow displayed around the control.
The sun is always shining so there has to be one. Always.
txt.SetTextSize
Change the size of the contained text.
txt.SetTouchable
En/Disables touch events to be fired on the control. Other events like OnChange will still be fired.
txt.SetVisibility
Change the visibility of the control to one of the available modes:
Show: visible
Hide: invisible but still consuming space
Gone: invisible and not consuming space
txt.Show
Set the visibility of the control to “Show”.
txt.Tween
Performs an animation on the control.
The target object is for the position, size and rotation that the control has at the end of the animation.

The type specifies the behavior and the speed of the animation. Separated by a dot, you must also specify whether you want to apply this behavior to the beginning (In), end (Out), or to both (InOut) times of the animation.

With the amount of repeats you can control how many times you want to play the animation.

If you have jojo activated, the animation will alternate between forward and backward playback, so that if the repetition value is odd, the control will be at the start position again at the end of the animation.

Finally the callback function will be called after the animation has finished. Well, it's about time!