Yes, it is possible. You can use the addView() method in any Viewgroup as Linearlayout, Relativelayout, etc. Follow an example:
package com.example.androidview;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout =
(LinearLayout)findViewById(R.id.mainlayout);
//newButton added to the existing layout
Button newButton = new Button(this);
newButton.setText("Hello");
mainLayout.addView(newButton);
//anotherLayout and anotherButton added
//using addContentView()
LinearLayout anotherLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button anotherButton = new Button(this);
anotherButton.setText("I'm another button");
anotherLayout.addView(anotherButton);
addContentView(anotherLayout, linearLayoutParams);
}
}
More details here:
http://android-coding.blogspot.com.br/2013/10/add-view-programmatically-using-addview.html
Marcio, could you add a piece of code here in Stackoverflow? Because it will break, and over time we would not have the information
– David
I edited my answer
– Márcio Oliveira
Thanks for the help. I’ll test and come back to comment.
– Henqsan