just
just

Reputation: 2020

TextView add to GridLayout dynamically

I have a problem, that i dont know how can i add a textview to a gridlayout. I have an xml:

<TextView
        android:id="@+id/textView1"
        android:layout_column="2"
        android:layout_columnSpan="3"
        android:layout_gravity="center_horizontal|top"
        android:layout_row="3"
        android:layout_rowSpan="2"
        android:text="TextView" />

And would like to do this xml code dynamically. How can i do this? I know how to create textview, but i dont know how to add to the gridlayout...

Upvotes: 1

Views: 13310

Answers (1)

JamieB
JamieB

Reputation: 923

GridLayout gv = (GridLayout) findViewById(R.id.gridlayout);
TextView tv = new TextView(context);
tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
tv.setText("TextView");
LayoutParams params = new LayoutParams(Set your column and row information as params);
tv.setLayoutParams(params);
gv.addView(tv);

You can't paste the code in and it will need to be tested and changed to fit (it won't just work). But it should give you the general idea.

Upvotes: 5

Related Questions