Debopam Mitra
Debopam Mitra

Reputation: 1870

How can I set the text of the TextView within a LinearLayout?

I have created a custom LinearLayout through program which have a custom TextView within it. I have not added the TextView using inflater. Now I want to use this LinearLayout multiple times in XMl layout. But the problem is that how can I set the text of these TextView within the LinearLayout?

Any ideas??

Upvotes: 1

Views: 188

Answers (3)

Vipul J
Vipul J

Reputation: 6691

Use a reference of that TextView to to set the text. Then add this TextView to LinearLayout.

ex -

TextView text = new TextView(yourActivity.this);
text.setText("Sample Text");
ll.addView(text);

// ll is your LinearLayout.

Upvotes: 1

Gautam
Gautam

Reputation: 7958

When adding the customTextView set the tag using setTag method. And then to retrieve it use linearLayout.findViewWithTag method to retrieve the customTextView and as usual use setText to set the text

You an also alternatively set the id using setId and retrieve it using findViewById method

Upvotes: 1

Ilya Demidov
Ilya Demidov

Reputation: 3305

If I right understand it will help you. Good luck!

LinearLayout layout = findViewById(id_linear_layout);
((TextView) layout.findViewById(id_textview)).setText("Text");

Upvotes: 1

Related Questions