Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView span full width programmatically

I've created a TextView within a LinearLayout programmatically:

LinearLayout filmTitleContainer = new LinearLayout(ctx);
TextView filmName = new TextView(ctx);
filmName.setBackgroundColor(Color.rgb(Color.YELLOW));
filmName.setText(this.screeningTime+" "+this.name);

The text view is shrunk:

enter image description here

How do I set the text to the occupy the full width of the screen? When I use the XML layout file, I'd set android:layout_width="match_parent". What's the equivalent Java code?


like image 300
Adam Matan Avatar asked Nov 11 '12 08:11

Adam Matan


1 Answers

Use LayoutParams for both the view and its container:

LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
filmTitleContainer.setLayoutParams(params);
filmName.setLayoutParams(params);

enter image description here

like image 123
Adam Matan Avatar answered Nov 07 '22 12:11

Adam Matan