Textview inside RelativeLayout in custom View not applying gravity correctly?
Asked Answered
F

3

7

So I have a setup where I'm creating my own View and I'm adding some TextViews into it. However, the gravity setting is broken for it. (It centers horizontally, but not vertically) I'm doing it this way because there's other stuff I'm also drawing within my view besides just the TextViews, but those work fine. There's only a problem with the TextView gravity. Here's partial code of what I have.

public class myView extends View {

    protected RelativeLayout baseLayout;
    protected TextView textView1;
    protected TextView textView2;

    public myView (Context context) {
        super(context);
        setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));

        baseLayout = new RelativeLayout(context);
        baseLayout.setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));

        textView1 = new TextView(context);
        // initialize textView1 string, id, textsize, and color here
        textView2 = new TextView(context);
        // initialize textView2 string, id, textsize, and color here

        baseLayout.addView(textView1);
        baseLayout.addView(textView2);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Resources res = getResources();
        // calculate out size and position of both textViews here
        textView1.layout(left1, top1, left1 + width1, top1 + height1);
        textView1.setGravity(Gravity.CENTER);
            textView1.setBackgroundColor(green); // just to make sure it's drawn in the right spot
        textView2.layout(left2, top2, left2 + width2, top2 + height2);
        textView2.setGravity(Gravity.CENTER);
            textView2.setBackgroundColor(blue); // same as above

        baseLayout.draw(canvas);
    }
}

This draws the TextViews in the exact spots and sizes that I want them (I know because of the background color), but the gravity sets them to be only centered horizontally.. not vertically. (yes, the TextViews are larger than the actual text strings)

I could PROBABLY implement the solution found here (TextView gravity), but that doesn't seem like a very efficient or reliable way to get around this. Is there something I'm doing wrong that's causing gravity to stop working correctly? Any input/help is appreciated.

Frankiefrankincense answered 3/6, 2011 at 4:25 Comment(3)
Two things: 1. I didn't realize that a View could contain a ViewGroup ? 2. Why use a RelativeLayout when the child views' positions are being set absolutely ?Callipygian
Never mind 1, I didn't realize ViewGroup extended View !!Callipygian
It's been a while since I posted this, but if I recall correctly - an AbsoluteLayout required different constructor arguments and it was just easier to use a RealtiveLayout.. But that might not be accurate. :/ Either way - you may want some view positions to be relative and some to be absolute - who knows what you might have planned. At least you'd have the option with a relative layout. :)Frankiefrankincense
F
9

Ok.. So I figured this out. I just had to run the measure() method on each TextView.. So my new code looks like:

    textView1.measure(MeasureSpec.makeMeasureSpec(width1, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height1, MeasureSpec.EXACTLY));
    textView1.layout(left1, top1, left1 + width1, top1 + height1);
    textView1.setGravity(Gravity.CENTER);

    textView2.measure(MeasureSpec.makeMeasureSpec(width2, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2, MeasureSpec.EXACTLY));
    textView2.layout(left2, top2, left2 + width2, top2 + height2);
    textView2.setGravity(Gravity.CENTER);

Now it centers both horizontally and vertically like it should. If you're having the same issues, try this out.

Frankiefrankincense answered 3/6, 2011 at 23:44 Comment(1)
another way is to WRAP_CONTENT on the TextView and change the parent align (Gravity.CENTER) or alignParent..Aloisia
A
2

You can set multiple Gravity parameters using setGravity like this.

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );

Hava a look on How do I center text horizontally and vertical in a TextView in Android?

Adlai answered 3/6, 2011 at 5:20 Comment(1)
Actually that's what I had originally before I noticed that just "CENTER" was an option (which is the bitwise or combination of vertical and horizontal).. That won't fix this issue. I did, however, figure it out. (posted below)Frankiefrankincense
L
0

I just ran into this problem, too. My issue was that when I set gravity on TextView by calling TextView.setGravity(), it silently affects the layout of itself(Textview) in parent view.

Here's what I did for the workaround fix:

TextView schedule = getBubbleTextView (item, hasZeroSpanEvent);
// There's a bug in TextView, we need to use wrapper to fix it   
LinearLayout wrapper = new LinearLayout (getApplicationContext ());
wrapper.addView (schedule); 
hourlyBubbleParent.addView (wrapper, llp);

You should grab the idea that you should use wrapper to workaround this.

Liles answered 27/5, 2013 at 11:3 Comment(1)
I'm happy that some of my questions are still being found useful nearly 2 years after I asked them.. ^.^ If I have this problem again, I will consider this approach - I'm wondering if it's the same Android OS version, though, or if the issue still exists even with the newer API versions. (I think I was coding at API 7 at the time... might have been 9)Frankiefrankincense

© 2022 - 2024 — McMap. All rights reserved.