In this short snippet we'll learn how to programmatically add views to the LinearLayout
in Android.
// create LinearLayout LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); // or get LinearLayout LinearLayout ll = (LinearLayout) findViewById(R.id.wrapper); // add ImageView ImageView iv = new ImageView(this); //setting image resource iv.setImageResource(R.drawable.image); //setting image position iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); //adding view to layout ll.addView(iv); // add TextView TextView tv = new TextView(this); tv.setText("Hello world"); tv.setId(5); tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); tv.setTextSize(16); tv.setPadding(5, 3, 0, 3); ll.addView(tv);