0

Programatically I have a RelativeLayout and TextView (called title) within it defined as follows:

RelativeLayout layout = new RelativeLayout(this);
final RelativeLayout.LayoutParams parameters_title = 
      new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                       RelativeLayout.LayoutParams.WRAP_CONTENT);  
title.setLayoutParams(parameters_title);
layout.addView(title,parameters_title);

I intend to add more textviews and buttons later. But for now, I want to "convert" (I am not sure how to put it into words) the RelativeLayout into a View in order to put it into WindowManager. I was trying to use LayoutInflater like this but it wouldn't work

LayoutInflater layoutInflater = 
       (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = layoutInflater.inflate(layout, null );

then the goal is to put myView into WindowManger through addView. How can I get a View from my RelativeLayout?

mmBs
  • 8,421
  • 6
  • 38
  • 46
AndroidLove
  • 123
  • 4
  • 11

1 Answers1

0

you can do this by getChildAt but you will have to keep the position of the view you want to get from layout. To avoid this you can set an id for your view and get the view with findViewById(). If you inflate a view from xml you will get the same views as xml. if you add some new views to the relativeLayout it will not effect the view you inflate from the xml.

when you are adding the view:

 myView.setId(id);
 layout.addView(myView);

when you are retrieving

 View myView = layout.findViewById(id);
Hadi Tok
  • 772
  • 8
  • 20