I want to add programmatically a TextView in current view. I know how to make it when I create a new layout but I want to add this to current layout, the main problem is how to get reference to main_activity layout.
Thanks for answers.
I want to add programmatically a TextView in current view. I know how to make it when I create a new layout but I want to add this to current layout, the main problem is how to get reference to main_activity layout.
Thanks for answers.
Let's say that you have a LinearLayout
in your project like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/main"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
To add many TextView's
in your layout, you can do something like this:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View linearLayout = findViewById(R.id.main);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
}
}
private boolean tuVariable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.completar_perfil);
if(tuVariable){ //si tu variable es true
TextView textView = new TextView(this);
textView.setText("you message");
}