Sammm
Sammm

Reputation: 531

Enabling scrollbar in EditText Android

I have an EditText on my layout. Below are the attributes I currently have:

<EditText
   android:id="@+id/entryIdea"
   android:layout_width="fill_parent"
   android:layout_height="225sp"
   android:gravity="top"
   android:background="@android:drawable/editbox_background"
   android:scrollbars="vertical"/>

However, I can see the scrollbar but can't scroll it with mouse/touch. I thought that it may works if I put the corresponding listener since it works on TextView. Apparently, it isn't.

EditText et = (EditText)findViewById(R.id.entryIdea);
et.setMovementMethod(new ScrollingMovementMethod());

Can you guys help me on this?

Thank you so much in advance. Sammy

Upvotes: 7

Views: 30616

Answers (7)

Pooja Solanki
Pooja Solanki

Reputation: 29

in xml file use:

android:maxLines="5" android:scrollbars = "vertical"

and in .java file add

edt_text.setMovementMethod(new ScrollingMovementMethod());

Upvotes: 1

Rajkumar prajapati
Rajkumar prajapati

Reputation: 11

editText1.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub
        if (view.getId() ==R.id.editText1) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction()&MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_UP:
                view.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
        }
        return false;
    }
});

Upvotes: 1

mahone
mahone

Reputation: 1

your xml file use:

android:maxLines="5"

Upvotes: -1

Patel Miral
Patel Miral

Reputation: 31

editText1.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View view, MotionEvent event) {
            // TODO Auto-generated method stub
            if (view.getId() ==R.id.editText1) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (event.getAction()&MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }
            }
            return false;
        }
    });

Upvotes: 3

user2574723
user2574723

Reputation: 225

use this:

 android:maxLines="5"

attribute for your xml file. Then the scrollbars attribute will work.

Upvotes: 2

Amsheer
Amsheer

Reputation: 7131

refer this link

 EditText dwEdit = (EditText) findViewById(R.id.DwEdit);       
 dwEdit.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View view, MotionEvent event) {
                // TODO Auto-generated method stub
                if (view.getId() ==R.id.DwEdit) {
                    view.getParent().requestDisallowInterceptTouchEvent(true);
                    switch (event.getAction()&MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_UP:
                        view.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                    }
                }
                return false;
            }
        });

Upvotes: 4

Coryffaeus
Coryffaeus

Reputation: 390

In your XML try setting the EditText height not in layout_height, but rather use android:lines attribute (btw, using sp is not usually a good practice when setting a size for anything except font size. Using dp/dip is more natural in this case).

Meanwhile set layout_height to wrap_content. Otherwise the XML you presented (with the changes I've mentioned) worked fine for me even without specifying movement method in the code.

And of course, the scroll bar will appear when the actual amount of lines of text in the EditText is larger than that, indicated in android:lines attribute.

Upvotes: 7

Related Questions