0

I have one EditText and one FrameLayout. I made it in a way where if the user drags within the FrameLayout, the background changes the color (RGB) based on the pixel from (X,Y) coordinate and also updates the EditText with the Hex value converted from the RGB values.

Here is the Pastebin link for my entire code: http://pastebin.com/7P3QSmb4

The issue I am having is with the following code:

if (s.length() == 6) {
    int color = Integer.parseInt(etHexVal.getText().toString(), 16);
    int r = (color >> 16) & 0xFF;
    int g = (color >> 8) & 0xFF;
    int b = (color >> 0) & 0xFF;
    getXY(r, g, b);
    hexToRGB();
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(etHexVal.getWindowToken(), 0);
}

I want to call the function getXY(r, g, b) only when the text of the EditText is changed and I am not dragging within the FrameLayout, because otherwise it would make my app lag. How can I modify my existing code?

ntv1000
  • 626
  • 6
  • 16
Si8
  • 9,141
  • 22
  • 109
  • 221
  • So you are getting this called many times with the same value? Why don't you just do a check in your code that sets the value to make sure that you are not writing the same value to the editText? ie: if (!newHexValue.equals(etHexVal.getText().toString()) then setText. This will stop you from unnecessary processing. – natez0r Feb 13 '14 at 22:54
  • The function should be called only if that text changes inside but the FrameLayout MotionEvent also calls the TextWatcher function. If you look at the pastebin code you will understand better. – Si8 Feb 13 '14 at 23:03

2 Answers2

1

You can create a TextWatcher object and add it to the EditText object using EditText.addTextChangedListener(TextWatcher watcher). This will cause the EditText object to call your TextWatcher's methods to be called when the text changes. You can call getXY from the afterTextChanged method you implement in your TextWatcher. This way it will be called only when the text in the EditText object changes.

sources:

Chronio
  • 757
  • 3
  • 8
  • I only included partial code but in the pastebin which has the full code, I am using TextWatcher. The only issue is the getXY gets called each time the text change which is happening as i move around the layout. so while im dragging the function is trying to set X,Y which is why my app is causing ANR. – Si8 Feb 13 '14 at 23:00
1

Use a variable to lock the call of getXY()

Declare a new variable

Boolean callGetXY = true;

Set it to false on ACTION_DOWN and back to true on ACTION_UP

View.OnTouchListener flTouch = new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
       // your current content of the method here
       // ...
       if (event.getAction() == MotionEvent.ACTION_DOWN) {
           callGetXY = false;
       }

       if (event.getAction() == MotionEvent.ACTION_UP) {
           callGetXY = true;
       }

    }
};

Change line 82 to:

if (callGetXY) {
    getXY()
}
ntv1000
  • 626
  • 6
  • 16
  • 1
    I am actually trying to prevent the `getXY()` being called if I am dragging within the FrameLayout. I only want to call it if I change the EditText value && not dragging. – Si8 Feb 13 '14 at 23:34
  • I actually added a boolean for each... ACTION_MOVE and ACTION_DOWN, `isDrag = true;` ACTION_UP `isDrag = false;` and inside the TextWatcher function I added `Toast.makeText(getApplicationContext(), String.valueOf(isDrag), 2000).show(); if (isDrag == false) { getXY(r, g, b); }` which will fire if I am not dragging and isDrag is false. For some reason the isDrag equals true and not false; – Si8 Feb 13 '14 at 23:38
  • 1
    Just had the same idea when I understood your question right. – ntv1000 Feb 13 '14 at 23:47
  • Nope that is not working. For some reason the `callGetXY` is false; for some reason. I can send you my app package if you want :) – Si8 Feb 13 '14 at 23:54
  • Just noticed an issue. When i let of the view it will be TRUE and cause itself to call getXY :/ Damn it... not working. – Si8 Feb 15 '14 at 04:43