1

Related questions are here and here. But they do not answer my question.

I'm trying to add the buttons dynamically at runtime, so there is minimal design stage xml. All the buttons are created at runtime. All works fine till four buttons are added to a single line. It goes awry when a fifth button is added to the GridLayout.

I'm trying to affect the constraints to avoid the below error I get, when I add the fifth button:-

D/android.widget.GridLayout: horizontal constraints: x1-x0>=231, x2-x1>=231, x3-x2>=231, x4-x3>=231, x5-x4>=231, x5-x0<=1080 are inconsistent; permanently removing: x5-x0<=1080. 

There are five buttons in my GridLayout trying to squeeze themselves but they do not fit in 1080dp, as 231dp*5=1155.
1. Where is the 231dp coming from?
Output from the logs show that the Button.getMinimumWidth() was 231dp (Scroll to the right to see the last parameter):-

D/CommandCenter: Btn <0> details:X<0.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<0> || MinW<231>
D/CommandCenter: Btn <1> details:X<270.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<270> || MinW<231>
D/CommandCenter: Btn <2> details:X<540.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<540> || MinW<231>
I/zygote: Waiting for a blocking GC Debugger
D/CommandCenter: Btn <3> details:X<810.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<810> || MinW<231>

enter image description here 2. Change the minimum width from 231dp to 216dp as below:

//btnContainer --> GridLayout Object
//btn          --> Button Object
//BTN_COLS     --> Number of Buttons in one row
btn.setMinimumWidth(btnContainer.getWidth()/BTN_COLS);

3. Check if the minimum width was set properly(Scroll to the right, the param MinW in log line):

D/CommandCenter: Btn <0> details:X<0.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<0> || MinW<216>
D/CommandCenter: Btn <1> details:X<270.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<270> || MinW<216>
D/CommandCenter: Btn <2> details:X<540.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<540> || MinW<216>
D/CommandCenter: Btn <3> details:X<810.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<810> || MinW<216>

Unfortunately the minimum width of the button was not used to resolve(solve) the GirdLayout Constraints and still shows the same error as the width being used was still 231dp instead of 216dp

D/android.widget.GridLayout: horizontal constraints: x1-x0>=231, x2-x1>=231, x3-x2>=231, x4-x3>=231, x5-x4>=231, x5-x0<=1080 are inconsistent; permanently removing: x5-x0<=1080. 

.
4. Here is the state of affairs after everything goes haywire:

D/CommandCenter: Btn <0> details:X<0.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<0> || MinW<216>
D/CommandCenter: Btn <1> details:X<2391.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<2391> || MinW<216>
D/CommandCenter: Btn <2> details:X<4782.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<4782> || MinW<216>
D/CommandCenter: Btn <3> details:X<7173.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<7173> || MinW<216>
D/CommandCenter: Btn <4> details:X<9564.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<9564> || MinW<216>
kaza
  • 2,317
  • 1
  • 16
  • 25

0 Answers0