Skip to content Skip to sidebar Skip to footer

Create Edittext In Gridlayout Programmatically In Android

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,

Solution 1:

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

activity_main.xml

<LinearLayoutxmlns: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"><GridLayoutandroid:id="@+id/inputTasksLayout"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:columnCount="2"android:rowCount="10"></GridLayout><Buttonandroid:id="@+id/addnewTask"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="addView"android:text="Add" /></LinearLayout>

MainActivity.java

publicclassMainActivityextendsAppCompatActivity {
introwIndex=1;
intcolIndex=0;
private GridLayout gridLayout;

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

}

publicvoidaddView(View view) {
    EditTexteditText=newEditText(this);
    GridLayout.LayoutParamsparam=newGridLayout.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><itemname="task1"type="id" /><itemname="task2"type="id" /><itemname="task3"type="id" /></resources>

Solution 2:

Yes of course it is possible. It is one of two main ways to create Views. The good old code way...I prefer it over xml btw. but thats just me. I do nearly 90% of View creation and layouting in Code because I dont like the "dp" kind of way to say how wide or high a view ist for example....

You just create a new EditText via

new EditText();

Then you give it a height and a width. Check for the methods you can use here http://developer.android.com/reference/android/widget/TextView.html (EditText inherits the TextView)

And for your purpose check the Documentation of GridView out: http://developer.android.com/guide/topics/ui/layout/gridview.htmlhttp://developer.android.com/reference/android/widget/GridView.html

most importantly : http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

the last link shows you how to insert an item into a layout like grid view for example.

It is as easy as XML but just needs a little bit more code.....

EDIT: As you keep on persist on a code solution keep in mind that stack overflow is not for demanding a code solution in your questions. It is a plattform to get help not to receive specific solutions in code.

Check the opening text of my link:

"A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways:

Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically. The Android framework gives you the flexibility to use either or both of these methods for declaring and managing your application's UI. For example, you could declare your application's default layouts in XML, including the screen elements that will appear in them and their properties. You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time."

So if you read the text you can adapt the things you will learn not only for edittext but for all views and its subclasses its not that hard....

Again SO is not for demanding explicit code solutions

Post a Comment for "Create Edittext In Gridlayout Programmatically In Android"