Help make event extension

@SimpleFunction
   public void Regsentuh (AndroidViewComponent view)
   {
       final int x =0;
       final int y =0;
       final View sentuh = view.getView();
   

    OnTouchListener mOnTouch = new OnTouchListener()
{
    @Override
    public boolean onTouch(sentuh, MotionEvent event)
    {            
       final int action = ev.getAction();
       switch (action & MotionEvent.ACTION_MASK) {
       case MotionEvent.ACTION_DOWN: {
           x = (int) ev.getX();
           y = (int) ev.getY();
       break;
    }
}

@SimpleEvent
  public void Sentuhxy(int x, int y)
  {
    EventDispatcher.dispatchEvent(this, "Sentuhxy", x, y);
 
 }
i want make this extension

There are a few problems in your extension.

  1. There is no such class name as OnTouchListener, it is called View.OnTouchListener.

  2. You have never called the Sentuhxy event in your code, so it would never be invoked.

  3. Also, you have mistakes here.


OnTouchListener mOnTouch = new OnTouchListener()
{
    @Override
    public boolean onTouch(sentuh, MotionEvent event)
    {            
       final int action = ev.getAction();
       switch (action & MotionEvent.ACTION_MASK) {
       case MotionEvent.ACTION_DOWN: {
           x = (int) ev.getX();
           y = (int) ev.getY();
       break;
    }

  • You have not defined what component the touch listener should listen to, and;

  • You have some formatting issues. So, I would suggest you to change this to:


sentuh.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View view, MotionEvent event)
    {            
       final int action = event.getAction();
       if (action == MotionEvent.ACTION_DOWN) {
           x = (int) ev.getX();
           y = (int) ev.getY();
           Sentuhxy(x, y);
}
    });

1 Like

i ll try thanks

Sorry, I found it to be wrong in some places. Can you try this?

package io.test.eventextension;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;

import com.google.appinventor.components.runtime.AndroidViewComponent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;

@DesignerComponent(
        version = 1,
        description = "",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "")

@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")

public class EventExtension extends AndroidNonvisibleComponent {

    //Activity and Context
    private Context context;
    private Activity activity;

    public EventExtension(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

    @SimpleFunction(description = "Regsentuh")
    public void Regsentuh (AndroidViewComponent view) {
       final View sentuh = view.getView();  
        sentuh.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {            
                final int action = event.getAction();
                if (action == MotionEvent.ACTION_DOWN) {
                    Sentuhxy(event.getX(), event.getY());
                }
                return false;
            }});
}

    @SimpleEvent (description = "Sentuhxy")
    public void Sentuhxy(float x, float y){
        EventDispatcher.dispatchEvent(this, "Sentuhxy", x, y);
    }
}
1 Like

package sentuh.com;

import android.app.Activity;

import android.content.Context;

import com.google.appinventor.components.annotations.*;

import com.google.appinventor.components.common.ComponentCategory;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.EventDispatcher;

import com.google.appinventor.components.runtime.AndroidViewComponent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.view.MotionEvent;

@DesignerComponent(

    version = 1,

    description = "",

    category = ComponentCategory.EXTENSION,

    nonVisible = true,

    iconName = "")

@SimpleObject(external = true)

//Libraries

@UsesLibraries(libraries = “”)

//Permissions

@UsesPermissions(permissionNames = “”)

public class EventExtension extends AndroidNonvisibleComponent {

//Activity and Context

private Context context;

private Activity activity;

public EventExtension(ComponentContainer container){

    super(container.$form());

    this.activity = container.$context();

    this.context = container.$context();

}

@SimpleFunction(description = "Regsentuh")

public void Regsentuh (AndroidViewComponent view) {

   final View sentuh = view.getView();  

    sentuh.setOnTouchListener(new View.OnTouchListener() {

        @Override

        public boolean onTouch(View view, MotionEvent event) {            

            final int action = event.getAction();

            if (action == MotionEvent.ACTION_DOWN) {

                Sentuhxy(event.getX(), event.getY());

            }

            return false;

        }});

}

@SimpleEvent (description = "Sentuhxy")

public void Sentuhxy(float x, float y){

    EventDispatcher.dispatchEvent(this, "Sentuhxy", x, y);

}

}

}

but i get error

Started Compiling Project Sentuh
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/XBinM/classes
[javac] Compiling 1 source file to /compiler/android/build/XBinM/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/XBinM/sentuh/com/Sentuh.java:60: error: class, interface, or enum expected
[javac] }
[javac] ^
[javac] 1 error
[javac] 1 warning

Not for me, but I compiled the extension for you. Can you try it?

EventExtension.aix (5.8 KB)

EDIT: Ah, I see. You have an extra } at the bottom of your code.

1 Like

@SimpleFunction (description = “Regsentuh”)

public void Regsentuh (AndroidViewComponent view) {

   

    view.getView().setOnTouchListener(new View.OnTouchListener() {

        @Override

        public boolean onTouch(View view, MotionEvent event) {            

            final int action = event.getAction();

            if (action == MotionEvent.ACTION_DOWN) {

                Sentuhxy(event.getX(), event.getY());

            }

            return false;

        }});

}

@SimpleEvent (description = "Sentuhxy")

public void Sentuhxy(float x, float y){

    EventDispatcher.dispatchEvent(this, "Sentuhxy", x, y);

}

}
and now i get

Started Compiling Project Sentuh
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/KvGkB/classes
[javac] Compiling 1 source file to /compiler/android/build/KvGkB/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/KvGkB/sentuh/com/Sentuh.java:29: error: class EventExtension is public, should be declared in a file named EventExtension.java
[javac] public class EventExtension extends AndroidNonvisibleComponent {
[javac] ^
[javac] 1 error
[javac] 1 warning

It is because of this section.


public class EventExtension extends AndroidNonvisibleComponent {

//Activity and Context

private Context context;

private Activity activity;

public EventExtension(ComponentContainer container){

    super(container.$form());

    this.activity = container.$context();

    this.context = container.$context();

}

You have to change EventExtension to Sentuh.

thanks Gordon_Lu

1 Like