I want to dynamically create a textview and set attributes.
LinearLayout timeBar = (LinearLayout)findViewById(R.id.linear_layout_cells);
TextView tv = new TextView(this);
timeBar.addView(tv);
I need to be able to set the height to match parent, the width to zero, the weight will be some % of 100 and I also need to set the background colour. Is this even possible?
Any help greatly appreciated
Yes, just make an instance of LinearLayout.LayoutParams. It has a constructor for width, height, and weight. Then, call
tv.setLayoutParams().In this case, you use
LinearLayout.LayoutParamsbecause yourTextViewis going inside aLinearLayout. Here’s a more formal example: