0

Is there a way to programatically add TextView and show it right next to the ImageView user clicked on?

I tried something like this, but no results, TextView is added to the bottom of my layout:

TextView score = new TextView(getBaseContext());
score.setText("Image clicked");
Rect rectf = new Rect();
im.getLocalVisibleRect(rectf);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 300, 270);
score.setLayoutParams(params);
layout.addView(score);
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
newman555p
  • 181
  • 1
  • 4
  • 13

1 Answers1

1

First thing, never use AbsoluteLayout, never! AbsoluteLayout is generally not recommended and you shouldn't use it. I recommend to you use RelativeLayout and then you only need to add rule.

Try to use following snippet of code:

relativeLayout = new RelativeLayout(this);
par = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
par.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
relativeLayout.addView(score, par);
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • No, the parent layout of image is LinearLayout. I'm gonna try this code now and let you now if it works. Thanks! – newman555p Jun 30 '12 at 17:47
  • so you are doing something bad. – Simon Dorociak Jun 30 '12 at 17:50
  • I added the same code as you posted, except that I used: relativeLayout = new RelativeLayout(MyClass.this); because I'm doing this inside of onClick() method. I tried looking at view hierarchy but I can't see this relativeLayout anywhere...Any ideas? – newman555p Jun 30 '12 at 18:00
  • try to check [this answer](http://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it-below-another-textview). – Simon Dorociak Jun 30 '12 at 18:05
  • Can't figure it out. When I put RelativeLayout in my main XML layout, textView is shown, but of course on the wrong place. When I create RelativeLayout with a constructor, I get nothing. It's like in this way, RelativeLayout is never attached my parent layout. – newman555p Jun 30 '12 at 18:37