Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to create editText everytime I clicked the button (create). and Set the id, column and row.

Gridlayout id: inputTasksLayout button id: addnewTask

When i clicked the button, it will create an editText in column 1, row 1 and assigned an id (task1).

When i clicked the button again, it will create another editText in column 1, row 2 and assigned an id (task2).

When i clicked the button again, it will create another editText in column 1, row 2 and assigned an id (task3). And so on..

I want to know if its possible. If yes, can you give some sample codes? I tried the other sources but they are all in LinearLayout. I want to put it in GridLayout.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
406 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes can EditText View Dynamically on the click of Button Here below is the sample code.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<GridLayout
    android:id="@+id/inputTasksLayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:columnCount="2"
    android:rowCount="10">


</GridLayout>

<Button
    android:id="@+id/addnewTask"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="addView"
    android:text="Add" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
int rowIndex = 1;
int colIndex = 0;
private GridLayout gridLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inputTasksLayout);
    gridLayout = (GridLayout) findViewById(R.id.gridView);

}

public void addView(View view) {
    EditText editText = new EditText(this);
    GridLayout.LayoutParams param = new GridLayout.LayoutParams();
    param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    param.width = GridLayout.LayoutParams.MATCH_PARENT;
    param.rowSpec = GridLayout.spec(rowIndex);
    param.columnSpec = GridLayout.spec(colIndex);
    editText.setLayoutParams(param);
    if (rowIndex == 1) {
        editText.setId(R.id.task1);
    }
    if (rowIndex == 2) {
        editText.setId(R.id.task2);
    }

    gridLayout.addView(editText);
    rowIndex++;
 }
}

ids.xml in values folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="task1" type="id" />
<item name="task2" type="id" />
<item name="task3" type="id" />
</resources>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...