Megha Megha - 3 days ago 4
Android Question

Cursor is not visible in EditText when view is dynamically added

I have a Table Layout and dynamically view is added in the layout. Whenever layout has a edit text cursor is not visible on edit text but it cursor is visible on top of the edit text which is text view.

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

OnClick:

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>


Text View XML:

<?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"
/>


Edit Text 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 Edit text view but whenever I clicked on edit text, cursor is visible on Text View.

Answer

I got this solution worked for me.

textView.setText("text");
textView.post(new Runnable() {
         @Override
         public void run() {
             textView.setSelection(textView.getText().toString().length());
         }
});
Comments