I'm working with TextViews progammatically and I need to dynamically add new views and set their left/top position in the RelativeLayout parent. What I'm doing is something like this:

RelativeLayout global=(RelativeLayout)findViewById(R.id.global);
TextView view=(TextView)findViewById(R.id.root);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView childView=new TextView(view.getContext());
lp.addRule(RelativeLayout.ABOVE, view.getId());
childView.setLayoutParams(lp);
childView.setId(childView.getId()+1);
childView.setText("TRY STRING");
childView.setTextSize(view.getTextSize());
childView.setWidth(view.getWidth());
childView.setLeft(view.getLeft()+interval);
global.addView(childView);

Basically, I have a textview in a certain position (which is called view) and I'm trying to create a new textview above the existing one and on the same left position. Although the getLeft() method correctly returns the left position of view, childView happens to have the left field set to zero, and appears at the left end of the screen when i add it to the RelativeLayout. It looks like it's completely ignoring the setLeft method! Can anyone explain why? Thank you very much in advance!

Related posts

Recent Viewed