How to use addView to add view to layout?
I can't remember, but maybe there is any "refresh" method to load again the layout.
Try layout.invalidate();
You forgot to specify the LayoutParameters
for the newly added view.
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
TextView text=new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setText("test");
layout.addView(text);
EDIT
The GridView
with an id of @+id/gridview
is defined with a layout height of fill_parent
, leaving you with no space to add a new view. Changing its height to wrap_content
may solve your problem.
Adding my comment to this post to help others easily verify the solution.