Class ShareButton

All Implemented Interfaces:
Animation, Editable, ActionListener, ActionSource<ActionEvent>, StyleListener, IconHolder, ReleasableComponent, SelectableIconHolder, TextHolder

public class ShareButton extends Button implements ActionListener

The share button allows sharing a String or an image either thru the defined sharing services or thru the native OS sharing support. On Android & iOS the native sharing API is invoked for this class.

The code below demonstrates image sharing, notice that an image must be stored using the com.codename1.io.FileSystemStorage API and shouldn't use a different API like com.codename1.io.Storage!

Form hi = new Form("ShareButton");
ShareButton sb = new ShareButton();
sb.setText("Share Screenshot");
hi.add(sb);

Image screenshot = Image.createImage(hi.getWidth(), hi.getHeight());
hi.revalidate();
hi.setVisible(true);
hi.paintComponent(screenshot.getGraphics(), true);

String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "screenshot.png";
try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
    ImageIO.getImageIO().save(screenshot, os, ImageIO.FORMAT_PNG, 1);
} catch(IOException err) {
    Log.e(err);
}
sb.setImageToShare(imageFile, "image/png");

Notice that share looks different on a device

  • Constructor Details

    • ShareButton

      public ShareButton()
      Default constructor
  • Method Details

    • getTextToShare

      public String getTextToShare()
      Gets the text to share
    • setTextToShare

      public void setTextToShare(String textToShare)

      Sets the information to share

      Parameters
      • textToShare
    • setImageToShare

      public void setImageToShare(String imagePath, String imageMimeType)

      Sets the image to share. Notice some sharing services cannot share image and a text, therefore if setTextToShare(...) is also used, the sharing service gives image sharing higher priority.

      Parameters
      • imagePath: the full file path

      • imageMimeType: the image mime type e.g. image/png, image/jpeg

    • getImagePathToShare

      public String getImagePathToShare()
      Gets the image path to share
    • addShareService

      public void addShareService(ShareService share)

      Adds a sharing service.

      Parameters
      • share: ShareService
    • actionPerformed

      public void actionPerformed(ActionEvent evt)

      invoked when the button is pressed

      Parameters
      • evt
      Specified by:
      actionPerformed in interface ActionListener
    • getPropertyNames

      public String[] getPropertyNames()

      A component may expose mutable property names for a UI designer to manipulate, this API is designed for usage internally by the GUI builder code

      Returns

      the property names allowing mutation

      Overrides:
      getPropertyNames in class Label
    • getPropertyTypes

      public Class[] getPropertyTypes()

      Matches the property names method (see that method for further details).

      Returns

      the types of the properties

      Overrides:
      getPropertyTypes in class Label
    • getPropertyValue

      public Object getPropertyValue(String name)

      Returns the current value of the property name, this method is used by the GUI builder

      Parameters
      • name: the name of the property
      Returns

      the value of said property

      Overrides:
      getPropertyValue in class Label
    • setPropertyValue

      public String setPropertyValue(String name, Object value)

      Sets a new value to the given property, returns an error message if failed and null if successful. Notice that some builtin properties such as "$designMode" might be sent to components to indicate application state.

      Parameters
      • name: the name of the property

      • value: new value for the property

      Returns

      error message or null

      Overrides:
      setPropertyValue in class Label