Question

How to Align Button text when we reposition the Button from onLayout? I Have a graph which has many horizontal axis each axis has three buttons arranged in the following orderenter image description here

Now i have to reposition so that all the buttons on the graph align properly and get placed where ever i need. Every thing works fine except for the blue button's text alignment. If i say gravity as center it assumes the height as combined height of blue and red buttons and puts text half behind the red button. If is say nothing its on the black area where we just have blue little arrow on the left end of the button but i want the text to be on the blue area with gravity bottom and cent_horizontal. Interesting thing is my gravity bottom and center horizontal works fine for Tablets.

Here is my OnLayout code:

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);


    //this.strikeButton.layout(left,top,right,bottom);
    int height = this.getMeasuredHeight();
    int width = this.getMeasuredWidth();
    int buttonWidth = this.upArrowButton.getMeasuredWidth();
    int buttonHeight = (2*height)/5;
    int right = width - buttonWidth;

    int left = 0;
    int top = height / 2;
    top -= buttonHeight / 2;

    int bottom = top + buttonHeight;
    this.strikePriceButton.layout(left, top, right, bottom);
    this.upArrowButton.layout(left, 0, right, height / 2);
    this.downArrowButton.layout(left, height / 2, right, height);
    this.upArrowButton.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
    this.downArrowButton.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);
    this.strikePriceButton.setGravity(Gravity.CENTER);
    }

Here is my OnMeasure Function:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    this.setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), App.isTablet(context) ? 96 : 128);
}

I tried using same height for tablet and Phone it does not look good on one of them so i had to use different heights.

Was it helpful?

Solution

Just had to use dp rather than using the height and width values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top