-2

I want to add a line of texts inside linear layout(Vertical)

and I want to add them in function in java

How to do this?

Laila Mattar
  • 361
  • 4
  • 19

3 Answers3

0

To add dynamically in Java write like this : LinearLayout myLayout = new LinearLayout(MainActivity.this) ;

Jamal
  • 111
  • 8
0

Do it like this :

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

//add layout params
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

//add TextView inside the vertical LinearLayout
TextView textView = new TextView(this);
textView.setTextSize(16); //set the text size here
textView.setTextColor(Color.BLACK); // set the text color here
textView.setText("Texts"); // set the text here
textView.setTypeface(Typeface.BOLD); // set the text style here
linearLayout.addView(textView, p); // add the TextView to the LinearLayout

You can add any views inside the LinearLayout as much as you need.

itsmebil
  • 155
  • 1
  • 9
0

Use addView :

LinearLayout layoutCard  = new LinearLayout(getApplicationContext());
TextView text = new TextView(getApplicationContext());
text.setText("hello");
layoutCard.addView(text);