0

I need to access inner textview via layout inflater

sudo hirarchy:

<relative layout id="rlRoot">
 <relative layout>
  <linear layout>
   <linear layout>
    <relative layout id="rlTv">
      <textView id="tvOne"> 

I want to dynamically set value for the id tvOne through toast.

Logic I used as below

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.test, (ViewGroup) findViewById(R.id.rlRoot));
TextView txtId = (TextView) layout.findViewById(R.id.tvOne);
txtId.setText("Id: " + getId());

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.FILL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

But this gives "View not attached to window manager". I also changed rlRoot to rlTv. But same error. How can I access this nested text view?

M P Mathugama
  • 1,418
  • 1
  • 20
  • 31
  • Yes, it is not a problem with context I guess. Actually above that textView I also have a button which I set to invisible in code after I call setText function on that textView. After I set inflater.inflate(R.layout.test, null) seems logic is working fine now. – M P Mathugama Jun 06 '13 at 08:11

1 Answers1

1

If your logic is in your onCreate() then change

View layout = inflater.inflate(R.layout.test, (ViewGroup) findViewById(R.id.rlRoot));

This line to

View layout = inflater.inflate(R.layout.test, null);

Like this. I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Actually it is not in onCreate but a separate function. But as I mentioned in ρяσѕρєя K's comment by setting it to null, it worked. I think if we specify a viewGroup, we need to travel exact parent as I originally asked in this question. But if it is null, it by default decides the root of the inflated xml as in specification. Anyway thanks for your anwser. – M P Mathugama Jun 06 '13 at 08:16