Search code examples
javaandroidandroid-edittextscrollviewcode-editor

Design Edit Text Code Editor with line number not scrollable


I am trying to design a code editor with the help of Android Edit Text. I am using a text view parallel to it which is showing line number. It is working fine but the problem is after going down more then 30 lines it text view remains same but edit text becomes scroll able I can make the text view scroll able but it will not going to scroll with respect to edit text. The main problem is of scrolling. If anyone can help me to correct this or some other way to use similar kind of simple activity to code with line numbers, So that further I will be able to save that text in a file. Below is the Activity and xml code I have tried.

MainActivity.java

package com.rk.codeareadesignworkingedittextline;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText ed=(EditText)findViewById(R.id.editText);
        final TextView tv=(TextView)findViewById(R.id.textView);

        ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                int lines=ed.getLineCount();
                String lineText="";
                for (int i=1;i<=lines;i++){
                    lineText=lineText+i+"\n";
                    tv.setText(lineText);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}  

Below is the xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="51dp"
            android:layout_height="587dp"
            android:background="#eeeeee"
            android:gravity="center_horizontal"
            android:padding="12dp"
            android:text="1"
            android:textSize="18dp" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="350dp"
            android:layout_height="595dp"
            android:ems="10"
            android:gravity="left|top"
            android:inputType="textMultiLine"
            android:text=""
            android:textSize="18dp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>


Solution

  • Put your EditText in a ScrollView. And let the EditText and TextView expand as much as it can with wrap_content.

    <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="51dp"
            android:layout_height="wrap_content"
            android:background="#eeeeee"
            android:gravity="center_horizontal"
            android:padding="12dp"
            android:text="1"
            android:textSize="18dp" />
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="left|top"
            android:inputType="textMultiLine"
            android:text=""
            android:textSize="18dp" />
    
      </LinearLayout>
    
    </ScrollView>