0

I need to replace textview which is already define in xml layout file with dynamic textview created programmatically in my Activity class file.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Raj
  • 103
  • 9
  • [mcve] perhaps? – Shark Jul 11 '18 at 12:15
  • Why do you want to replace a `textView`? Please elaborate. – tahsinRupam Jul 11 '18 at 12:15
  • why do you want to replace the TextView? Why not replace the text or any of its properties instead? – Udit Jul 11 '18 at 12:15
  • @Udit https://stackoverflow.com/questions/51190630/how-to-create-customized-text-input-layout-like-attached-design/51191166?noredirect=1#comment89366398_51191166 i need to make a design like this,I try to combine image and text in a single drawable but i can't able to do that,so i try to replace textview with my custom textview. – Raj Jul 11 '18 at 12:17

2 Answers2

0

You don't need replace it, just get the particular properties from textView (from Java) and set them on text_view (from XML).

For example:

text_view.setText(textView.getText().toString())
text_view.setColor(textView.getColor())
...
text_view.setAnything(textView.getAnything())

Maybe it helpfull to you

Abner Escócio
  • 2,697
  • 2
  • 17
  • 36
0

I didn't get why you would do this, but anyhow will layout the steps:

  1. get reference to the TextView containing ViewGroup
  2. call removeView with TextView Reference.
  3. call addView with new TextView.

    TextView old = (TextView) findViewById(R.id.old_text_view) LinearLayout layout = (LinearLayout) findViewById(R.id.container) TextView new = new TextView()

    layout.removeView(old); layout.addView(new, position);

Udit
  • 1,037
  • 6
  • 11