Home » Android » android – How to create multiline edittext with scroll view within activity?

android – How to create multiline edittext with scroll view within activity?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want edittext which will have 3 lines and if the edittext content exceeds 3 lines limit then vertical scrollbar should be visible to user.

How to&Answers:

may work 100%

<EditText
    android:inputType="textMultiLine"
    android:lines="int number" <!-- Total Lines prior display -->
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:scrollbars="vertical"/>

Answer:

Use inputType="textMultiLine" and set android:lines="3" like:

<EditText
android:inputType="textMultiLine"
android:lines="3" <!-- Total Lines prior display -->
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbars="vertical"
/>

Answer:

In you edit Text just add

    android:inputType="textMultiLine" 
    android:maxLines="3">

Answer:

I think this question is more about adding Multiline editText inside scrollview.

Inside scrollview, If your EditText has more text you wont be able to scroll inside, rather whole page will scroll.

You can achieve this with help of TouchListener.

edtDescription = (EditText)findViewById(R.id.edt_description);

    edtDescription.setVerticalScrollBarEnabled(true);
    edtDescription.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
    edtDescription.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    edtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());

    edtDescription.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            view.getParent().requestDisallowInterceptTouchEvent(true);
            if ((motionEvent.getAction() & MotionEvent.ACTION_UP) != 0 && (motionEvent.getActionMasked() & MotionEvent.ACTION_UP) != 0)
            {
                view.getParent().requestDisallowInterceptTouchEvent(false);
            }
            return false;
        }
    });

Answer:

Have tried with ? :

<EditText
            android:id="@+id/edt_note"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:maxLines="3"
            >

Answer:

Apply this code snippets…you will get your requirement right. And one thing to note, you have to give hard coded Height of ScrollView…I give here as 70dip.

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="70dip" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="false" />
</ScrollView>

Answer:

if textview lines exceeds two line scrollview will scroll

if (mTvContacts.getLayout().getLineCount() >= 2) {
    if (scrollContactsHeight == 0)
        scrollContactsHeight = scrollContacts.getHeight();

        scrollContacts.getLayoutParams().height = scrollContactsHeight * 2;
} else {
    if (scrollContactsHeight == 0)
        scrollContacts.getLayoutParams().height = scrollContacts.getHeight();
    else
        scrollContacts.getLayoutParams().height = scrollContacts.getHeight() / 2;
}


 <ScrollView  
        android:id="@+id/scrollContacts"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"    
        android:layout_marginLeft="5dp"           
        android:layout_marginRight="5dp"        
        android:layout_toRightOf="@+id/tvTo"  
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/imgAddContacts">

        <TextView
            android:id="@+id/tvContacts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"    
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="wfd"
            android:textColor="#000" />

</ScrollView>