3

I have a custom view (DrawFigures) which has an onTouch method to perform a series of actions. There's a layout that has a few buttons, a TextView and a RelativeView to add the custom view dynamically. Then, in the MainActivity.class, I need to receive the value of some attributes in the DrawFigures when the onTouch method is called in this custom view, in order to show them in TextView components.

Here is the code of the custom view:

public abstract class DrawFigures extends View{
    private float value1;
    public boolean onTouch(Motion Event){
        int action = event.getAction();
        if(action==MotionEvent.ACTION_MOVE){
            // ... methods that provoke that value1 changes
        }else if(action==MotionEvent.ACTION_DOWN){
            // ...        
        }else if(action==MotionEvent.ACTION_UP){
            // ...
        }
    }
    // ...
    return true;
}

I haven't been able to receive the value1 to show it in the TextView of the MainActivity while it's changing because of the onTouch method of the DrawFigures. How could be this problem solved? Implement an OnTouch method in the MianActivity? How to do it? The MainActivity.class looks like this:

public class MainActivity extends Activity implements View.OnTouchListener{
    DrawFigures fig;
    TextView txtInfo;
    // ...
    public void btnSecRec(View v){
        int id = v.getId();
        if (id == R.id.btnSecRec){
            RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
            parent.removeAllViews();
            fig = new DrawRectangleWithDimensions(this);
            fig.setOnTouchListener(this);
            fig.setFigure(10, 40);
            parent.addView(fig);
        }
    }
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_MOVE) {

            if (fig.touching == true) {
                String value = Float.toString(fig.getChangingDimensionValue());

                txtInfo.setText(value);
            }
        }
        return true;
    }
}
daalgi
  • 169
  • 1
  • 14
  • Why not make `DrawFigures ` an inner class of `MainActivity`, and make `value1` a global class member? – Marcus Feb 20 '15 at 11:35
  • Alternatively, persist the values you wish to access in your `MainActivity` in `SharedPreference` – Marcus Feb 20 '15 at 11:36
  • The DrawFigures class is a pretty big one, which also is extended by other subclasses, so it'd be not convinient to put it in the MainActivity. About the SharedPreference, where to use it? The main problem is to use the value1 when the onTouch method of the DrawFigures is called. – daalgi Feb 20 '15 at 15:28

3 Answers3

0

You could implement the onTouchListener in the activity and after the calling the super class' onTouchEvent, you update the values through a getter. something like this:

@Override
public boolean onTouch(final View v, final MotionEvent event) {
    super.onTouchEvent(event)
    float value1 = fig.getUpdatedValues()
    return true;
}

Remember to also assign the OnTouchListener for that view in your activity

harrane
  • 959
  • 11
  • 24
  • "fig" is an instance of DrawFigures. This class has the getter method to retrieve the value1. But I tried this code and it doesn't work. – daalgi Feb 20 '15 at 12:51
  • oops missed that. The edited version should work, basically float value1 = fig.getUpdatedValues(). So from the Custom View "fig" you get the value1 which is updated for every MotionEvent.ACTION_MOVE – harrane Feb 20 '15 at 13:00
  • Still not working. It seems like the code inside the onTouch method of DrawFigures is not loaded. – daalgi Feb 20 '15 at 15:12
  • are you calling the super?? super.onTouchEvent(event) – harrane Feb 20 '15 at 16:56
0

You could declare value1 as static in your non-activity class like this.

static float value1;

then get the value in your Activity class like this:

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    String value = Double.toString(DrawFigures.value1);
    if (action == MotionEvent.ACTION_MOVE) {

        if (fig.touching == true) {
            String value = Float.toString(fig.getChangingDimensionValue());

            txtInfoc.setText(value);
        }
    }

Or since your value is private, you can use setter and getter methods to set and retrieve the value with an object of DrawFigures which you can then use across your app.

If you want the value to be persisted, you can use SharedPreferences

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
  • I need to get the value1 when the onTouch method of the DrawFigure.class is called. Where should the code you wrote put in the Activity.class? I tried to use the onTouch method in the Activity class without success. – daalgi Feb 20 '15 at 12:46
  • Make sure value1 in Drawfigures class is assigned a value before you try to use it in your Activity class – Ojonugwa Jude Ochalifu Feb 20 '15 at 13:15
  • Still not working. Now it seems like the code inside the onTouch method of DrawFigures is not loaded. I just edited my question adding the method called when a button is preshed, it might have influence. – daalgi Feb 20 '15 at 15:04
  • Did you initialize your button? – Ojonugwa Jude Ochalifu Feb 20 '15 at 15:38
  • the button is created in XML, I haven't initialized it in the MainActivity. The buttons (there are four) are working properly. – daalgi Feb 20 '15 at 15:51
  • Okay.I don't know man, wish I knew the problem.But another option you can try is SharedPreferences.If you give me some time, I will give you a sample you can try. – Ojonugwa Jude Ochalifu Feb 20 '15 at 15:54
  • I forgot to mention that the DrawFigures is an abstract class. Then there are four subclasses that extend the DrawFigures class. Again, the onTouch method is in DrawFigures, not in the subclasses. I am not sure this can have influence. – daalgi Feb 20 '15 at 15:56
  • Am not sure that will make any difference. – Ojonugwa Jude Ochalifu Feb 20 '15 at 16:02
0

After trying for a while, I found out a solution. Simply call the onTouchEvent method in the DrawFigures class using the instance "fig" inside the onTouch method of the MainActivity.

I'd like to thank the people who spent time trying to help, thanks!

Here's the code:

public class MainActivity extends Activity implements View.OnTouchListener{
DrawFigures fig;
TextView txtInfo;
// ...
public void btnSecRec(View v){
    int id = v.getId();
    if (id == R.id.btnSecRec){
        RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
        parent.removeAllViews();
        fig = new DrawRectangleWithDimensions(this);
        fig.setOnTouchListener(this);
        fig.setFigure(10, 40);
        parent.addView(fig);
    }
}
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    fig.onTouchEvent(event);
    if (action == MotionEvent.ACTION_MOVE) {

        if (fig.touching == true) {
            String value = Float.toString(fig.getChangingDimensionValue());
            txtInfo.setText(value);
        }
    }
    return true;
}

}

daalgi
  • 169
  • 1
  • 14