Android: Make multiline edittext scrollable, disable in vertical scroll view
Asked Answered
G

4

4

I am developing an application in which i am struct at a point.

As according to my application requirement i created horizontal scrollview in xml and then vertical scrollview in .java as :

// Vertical Scroll view in Linear layout
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Then i created a table view programatically and added it in scrollview. I created multiline edit text and disable it because i want to set text in it run time and added this in table view as a row..

EditText editText = new EditText(this);
editText.setId(1);
editText.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setGravity(Gravity.TOP|Gravity.LEFT);
editText.setHint("Comment");
editText.setSingleLine(false);
editText.setLines(5);
editText.setMaxLines(5);
editText.setText(CommentFromDB);
editText.setEnabled(false);

tableLayout.addView(editText);

// Add table in Horizontal scroll view
scrollView.addView(tableLayout);

Now i want to make edit text scrollable which i achieve by code:

editText.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {
            if (view.getId() == 1) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (event.getAction()&MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                case MotionEvent.ACTION_DOWN:
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }
            }
            return false;
        }
    });

But the edittext is not scrolling easily. I need the smooth scrolling.

How to do that please guide me.

Gumbo answered 27/4, 2014 at 4:33 Comment(2)
Why don't you try to using TextView , since any way your disabling the EditTextSafier
@Libin- can i make Textview scrollable in same case having fix size???Gumbo
P
9

You can do one thing.

Just make edit text focusable false. And apply on touch listener.

So user is not able to edit text and it will scroll as:

EditText editText = new EditText(this);
editText.setId(1);
editText.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setGravity(Gravity.TOP|Gravity.LEFT);
editText.setHint("Comment");
editText.setSingleLine(false);
editText.setLines(5);
editText.setMaxLines(5);
editText.setText(CommentFromDB);
editTextRemark.setFocusable(false);

Apply onTouchListner as:

editTextRemark.setOnTouchListener(touchListener);

and

OnTouchListener touchListener = new View.OnTouchListener(){
    public boolean onTouch(final View v, final MotionEvent motionEvent){
        if(v.getId() == 1){
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
        }
        return false;
    }
};

Hope this answer will help you.

Phrenic answered 30/4, 2014 at 6:21 Comment(0)
M
7
EditText editText = findViewById(R.id.editText);
editText.setOnTouchListener(new OnTouchListener() {
               public boolean onTouch(View view, MotionEvent event) {
                    // TODO Auto-generated method stub
                    if (view.getId() ==R.id.common_remark) {
                        view.getParent().requestDisallowInterceptTouchEvent(true);
                        switch (event.getAction()&MotionEvent.ACTION_MASK){
                        case MotionEvent.ACTION_UP:
                            view.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                        }
                    }
                    return false;
                }
        });

And in xml add this line in EditText:

android:scrollbars = "vertical"
Mythopoeic answered 12/11, 2014 at 9:51 Comment(0)
T
1

add to EditText

 android:inputType="textMultiLine"
Trouveur answered 26/10, 2014 at 19:38 Comment(0)
S
1

in kotlin use this

editText.setOnTouchListener { v, event ->
    v.parent.requestDisallowInterceptTouchEvent(true)
    when (event.action and MotionEvent.ACTION_MASK) {
        MotionEvent.ACTION_UP ->  v.parent.requestDisallowInterceptTouchEvent(false)
    }
    false
}
Saltern answered 2/2, 2021 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.