Cursor is not visible in EditText when view is dynamically added

Megha picture Megha · Sep 6, 2013 · Viewed 17.4k times · Source

I have a TableLayout and dynamically view is added in the layout. Whenever layout has a EditText cursor is not visible on EditText but it cursor is visible on top of the EditText which is TextView.

I added onClick event, XML file for both Edittext and Textview and XML file for the main layout.

My textview listener:

textView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        textView.setSelection(textView.getText().toString().length());
        textView.requestFocus();
        textView.requestFocusFromTouch();
        textView.setCursorVisible(true);
        return false;
    }
});

XML file:

 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:scrollbars="vertical" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:focusable="true"
                android:focusableInTouchMode="false"
                android:text="@string/title_form_details"
                android:textSize="25dp" />

            <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/fieldsContainer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbars="vertical"
                android:shrinkColumns="1"
                android:stretchColumns="1" >
            </TableLayout>
        </LinearLayout>
    </ScrollView>

TextView XML file:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|left"
    android:paddingBottom="0dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
     />

EditText XML file:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cursorVisible="true"
   android:textColor="#000000"
android:textCursorDrawable="@null"
         />

Dynamically I am adding TextView and in next row I am adding Edittext view but whenever I clicked on EditText, cursor is visible on TextView.

Answer

Kalai.G picture Kalai.G · Sep 6, 2013

You are setting the visibility of the cursor to textview, So it is visible in TextView

Remove this line from your code

textView.setCursorVisible(true);


textView.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    textView.setSelection(textView.getText().toString().length());
                    textView.requestFocus();
                    textView.requestFocusFromTouch();
                  //textView.setCursorVisible(true);
                    return false;
                }
            });

and try to set the visiblity to edittext

    edittext .setCursorVisible(true);