-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?

2

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

Use addView :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.