Creating LinearLayout programmatically

suggest change

Hierarchy

- LinearLayout(horizontal)
   - ImageView
   - LinearLayout(vertical)
     - TextView
     - TextView

Code

LinearLayout rootView = new LinearLayout(context);
rootView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
rootView.setOrientation(LinearLayout.HORIZONTAL);

// Image on the left of the horizontal root.
ImageView imageView = new ImageView(context);
// Vertical layout for the two text lines.
LinearLayout linearLayout2 = new LinearLayout(context);
linearLayout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout2.setOrientation(LinearLayout.VERTICAL);

TextView tv1 = new TextView(context);
TextView tv2 = new TextView(context);
linearLayout2.addView(tv1);
linearLayout2.addView(tv2);

// Add the image and the vertical layout to the horizontal root.
rootView.addView(imageView);
rootView.addView(linearLayout2);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents