2

The XML file(matrix_cell.xml) being inflated.

<EditText 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rect_edit_text"
    android:textCursorDrawable="@drawable/cursor_black"
    android:textColor="@color/colorAccent"
    android:gravity="center_horizontal"
    android:inputType="number"
    android:layout_margin="1dp"
    android:layout_weight="1" />

The code inflating the view:

LayoutInflater inflater = LayoutInflater.from(this);
TableRow tableRow = new TableRow(this);
inflater.inflate(R.layout.matrix_cell, tableRow, true);

The EditText View created is not selectable, the input focus remains on the previous input being focus when the EditText View is clicked.

2 Answers2

1

Add an id to your EditText.

It can be done like

<EditText
    android:id="@+id/edittext"
    android:inputType="text"
    ...
    ...
/>

It should fix your problem. More over, do you use any layout and make your EditText as its child? It is mandatory to do that. You have to remember that EditText is a view and not a Layout.

After the inflator inflates the view, use the view.findViewById(R.id.edittext) method to get your EditText for work.

UPDATE

As said by Alexander Kulyakhtin in this answer,

for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) {
    View nextChild = ((ViewGroup)v).getChildAt(i);
}

You can get the child. And as said by me, use nextChild.findViewById(R.id.edittext) to get the EditText and work with it.

Community
  • 1
  • 1
Sibidharan
  • 2,717
  • 2
  • 26
  • 54
  • Thanks. Adding id to the EditText solved the problem. But I have used the EditText as cells of a TableLayout. Won't the id refer to all the editTexts in the layout then? Wouldn't I still have to use getChildAt() to get a specific editText? – Aabesh Karmacharya Apr 07 '16 at 13:54
  • 1
    You are using inflator! So it wont affect! Make sure table cells are a linear or relative layout and edittext is a child of them. Just call the id as like I said after inflating. If this solved your problem, please mark the answer as correct. Thanks. – Sibidharan Apr 07 '16 at 13:57
1

Add id in your xml as @Sibidharan said.

<EditText
    android:id="@+id/editText"
    ...
    ...
/>

In your activity, make sure you have initialized it.

EditText editText= (EditText)findViewById(R.id.editText)
John Joe
  • 12,412
  • 16
  • 70
  • 135