SamArmen SamArmen - 4 months ago 12
Java Question

Set TextView programaticaly

Here is an xml code of my

TextView
:

<TextView
android:layout_weight="8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="15sp"/>


I want to add this kind of a
TextView
to my horizontal linear layout programaticaly. I am trying this but it says that this code will not work for API lower than Level 11.

name_text.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT));
name_text.setText(name);
name_text.setTextSize(15);


So what should I do to make it available for all APIs?

Here is a screenshot of the error

Answer

You're using the wrong LayoutParams. Use LinearLayout.LayoutParams instead of ActionBar.LayoutParams.

Comments