Looking at various examples, below is what seems to be the correct way of adding a view to a layout. However, nothing shows up for me. I’m assuming it has something to do with the layout options, but I’m new so I don’t know what is missing.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_layout);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.songs_layout, null);
Button myButton = new Button(this);
myButton.setText("Change View");
myButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.addView(myButton);
}
Your
layoutvariable is not the same as the layout you used insetContentView. Try callingsetContentView(layout);afterlayout.addView(myButton);and removing the previous call to it (setContentView(R.layout.songs_layout);).To be more clear, your onCreate method should look like