Note that there are some explanatory texts on larger screens.

plurals
  1. POAlign a Button Text after Repositioning the button onLayout in Android
    text
    copied!<p>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 order<img src="https://i.stack.imgur.com/8xapF.png" alt="enter image description here"></p> <p>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. </p> <p>Here is my OnLayout code:</p> <pre><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); } </code></pre> <p>Here is my OnMeasure Function:</p> <pre><code>@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); this.setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), App.isTablet(context) ? 96 : 128); } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload