The cursor does not appear in EditText when a view is dynamically added

I have a TableLayout , and a dynamic view is added to the layout. Whenever a layout has an EditText cursor, it is not displayed on the EditText , but its cursor is displayed on top of the EditText , which is equal to the TextView .

I added an onClick event, an XML file for EditText and TextView and an XML file for the main layout.

My TextView :

 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 add a TextView , and in the next line I add an EditText view, but whenever I clicked EditText , the cursor is displayed on the TextView .

+4
source share
6 answers

I got this solution for me.

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

You set the visibility of the cursor in the textview , so it appears in the 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 setting visibility to edittext

  edittext .setCursorVisible(true); 
+4
source

Try adding <requestFocus / "> in your editor. Maybe this works.

+1
source

Remove this line:

 textView.setCursorVisible(true); 

Then it works.

+1
source

Try calling setTextIsSelectable inside onTouchListener

  editText.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { editText.setTextIsSelectable(true); return false; } }); 
+1
source

This error is one of the stupidest in Android, I think ...

Here's how I did it:

  public void addEditText(LinearLayout linearLayout, final View view) { final EditText editText = new EditText(getActivity()); linearLayout.addView(textView); linearLayout.addView(editText); Utils.runDelayed(new Callable() { @Override public Object call() throws Exception { if (editText.getText() == null || editText.getText().toString().isEmpty()) { editText.setText(""); editText.setSelection(editText.getText().toString().length()); editText.setCursorVisible(true); editText.setTextIsSelectable(true); editText.setInputType(InputType.TYPE_CLASS_TEXT); } return null; } }, 10); editText.setTextAppearance(R.style.LargeEditText); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { } }); } 

Assistant:

 /** * Run delayed. * * @param callable the callable * @param delay the delay */ public static void runDelayed(final Callable callable, long delay) { final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { try { callable.call(); } catch (Exception e) { e.printStackTrace(); } } }, delay); } 
0
source

Source: https://habr.com/ru/post/1500864/


All Articles