4

I'm using a FrameLayout to display (on demand) some text on the screen. I want the text to be in a certain place, so I thought setGravity() would do the job... but no, it seems to have no effect whatsoever on where the text goes (and other objects like ImageView don't even have this method).

So first, what exactly is TextView.setGravity() used for? (Edit: I understand this much better now, thanks! Still not clear on the following part of the question though.)

Second, it seems the only way to update a FrameLayout in this way is to create a new FrameLayout.LayoutParams object with the settings you want, and then use the setLayoutParams() method to apply it. (This seems to automatically update the view so is a requestLayout() call necessary?) And is there a simpler / more straightforward way to achieve this for a FrameLayout... say, without creating a new LayoutParams object as I'm doing now?

Thanks! For reference, below is a (working) code snippet showing how I'm setting up this FrameLayout and TextView.

    FrameLayout fl1 = new FrameLayout(this);
    FrameLayout.LayoutParams flp1 = new FrameLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    fl1.setId(9001);
    fl1.setLayoutParams(flp1);
      .....
    tv1 = new TextView(this);
    FrameLayout.LayoutParams tvp1 = new FrameLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
        (Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
    tv1.setId(9006);
    tv1.setLayoutParams(tvp1); // This works
    tv1.setBackgroundColor(Color.GRAY);
    tv1.setTextColor(Color.BLACK);
    tv1.setText("Dynamic layouts ftw!");
    tv1.setGravity(Gravity.CENTER); // This does NOT work
     .....
    fl1.addView(tv1);
MartyMacGyver
  • 9,483
  • 11
  • 47
  • 67

2 Answers2

3

tv1.setGravity(Gravity.CENTER);

belongs to the gravity of the TEXT inside the TextView.

As documentation states:

Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Thanks! I think that sums the gravity question up nicely (and I just verified it experimentally). Still not clear on the second part of my question though. – MartyMacGyver Sep 24 '11 at 09:53
3

1) view.setGravity means hows the view should positions if children. In the case of the textview it refers to the positioning of the text. When in linearlayouts or viewgroups it refers to its child views.

2) I checked your code. You are already using textview.setGravity method. In that case you dont need to specify gravity parameters in the FrameLayout.LayoutParams constructor.

Other thing I noticed is that you gave the textview the width and height as wrap content which will only take the size of the text. So there is no meaning in giving gravity as the textview has no extra area to position the text to the center. You need to give the width of the textview as fill_parent. That should make your gravity property work.

Btw this is a very good article about Gravities. It explains both about gravity and the layout_gravity attribute.

If you want your textview to wrap the content then you should add your textview to a linearlayout and setgravity to the linear layout.

This should give what your are trying to do

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
FrameLayout fr = new FrameLayout(this);
fr.setLayoutParams(lp);
fr.setBackgroundColor(Color.RED);

LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(lp2);
l.setGravity(Gravity.CENTER);
l.setBackgroundColor(Color.BLUE);

ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
TextView t = new TextView(this);
t.setLayoutParams(vp);
t.setText("Blessan Mathew");
t.setBackgroundColor(Color.CYAN);

l.addView(t);
fr.addView(l);
blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • You mean the line, "tv1.setGravity(Gravity.CENTER);"? It has no effect, even without the LayoutParams (or when different from them). And fill_parent is even worse - you get the entire screen as one huge textbox. But yes, I see that setGravity affects where the text ends up in that box. On to the second part of the question... – MartyMacGyver Sep 24 '11 at 09:53
  • Thing is, what I came up with does the job as I want it to, using just a FrameLayout and a TextView (this example requires yet another layout). At this point I just wonder: A) when I move the text box by updating the LayoutParams via setParams, do I need to call requestLayout() (seems to work without that) and B) is there a simpler way to update the LayoutParams than creating a whole new LayoutParams and calling setParams again on it (there doesn't appear to be)? – MartyMacGyver Sep 24 '11 at 10:53
  • you're going to need separate params – blessanm86 Sep 24 '11 at 10:59
  • OK, that's what I'm doing now too. Is requestLayout() required after calling setLayoutParams() to update them, or is it implied? – MartyMacGyver Sep 24 '11 at 11:23
  • And, by the way, is that (broken) "Gravities" article link above supposed to point to [this page](http://sandipchitale.blogspot.com/2010/05/linearlayout-gravity-and-layoutgravity.html)? – MartyMacGyver Sep 24 '11 at 11:28
  • you dont need to call requestlayout. It is automatically called in the seTlayoutParams method of the view class. – blessanm86 Sep 24 '11 at 11:32
  • Thanks! Upvote if you think this discussion might be useful to others. – MartyMacGyver Sep 24 '11 at 23:01