0

I have a form with a lot of EditTexts. When clicking an EditText (focus), I want the scrollview to scroll to that location. I implemented onFocusChangeListener in my Fragment, but it doesn't seem to work.An other problem is that onFocusChange is never getting called.. Any ideas?

Here's my code:

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
        int[] loc = new int[2]; 
        v.getLocationOnScreen(loc);
        sw.scrollTo(loc[0], loc[1]);
    }
}
David
  • 840
  • 6
  • 17
  • 37

3 Answers3

0

edittext.requestFocus();

EditText editText = new EditText(getActivity());

    editText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            arg0.requestFocus();
        }
    });
tizbn
  • 1,907
  • 11
  • 16
0

Assuming we are inside the fragment with onFocusChangeListener implemented try this:

editText.setOnFocusChangeListener(this);
.....

@Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId()==editText.getId())
{
if(hasFocus){
    int[] loc = new int[2]; 
    v.getLocationOnScreen(loc);
    sw.scrollTo(loc[0], loc[1]);
}
}
}
Sarpe
  • 5,747
  • 2
  • 28
  • 26
0

You can try like this. such as

@Override
public void onCreate(Bundle savedInstanceState) {
    .....  
    EditText et = (EditText)findViewById(R.id.wo_task_comments);
    et.setOnTouchListener(this);
    .....
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if(view.getId() == R.id.wo_task_comments){
        view.getParent().requestDisallowInterceptTouchEvent(true);
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                view.getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
    }
    return false;
}

In your case:

public class MyActivity extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initComments(findViewById(R.id.YOUR_MAIN_LAYOUT_ID));  
}


public void initComments(final View view) {
    EditText comment = (EditText) view.findViewById(R.id.wo_task_comments);

    comment.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(final View v, final MotionEvent motionEvent) {
            if (v.getId() == R.id.wo_task_comments) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_UP:
                        v.getParent().requestDisallowInterceptTouchEvent(
                                false);
                        break;
                }
            }
            return false;
        }
    });

    comment.setText("very very long comment"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment");
}
}

[Reference] [Scrolling editbox inside scrollview]1

Community
  • 1
  • 1
kablu
  • 629
  • 1
  • 7
  • 26