Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 21    Views: 296

Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
10/28/13 11:07 AM (11 years ago)

Button Grid Menu on iOS and Android

If you use one Buzztouch project for both iOS and Android, the button menu grid doesn't work very well. If you set the buttons to be evenly spaced on iOS, then they are off center on Android. Here is a solution in code. Set your settings as you would like them in iOS, then replace out the "grid layout" section in the .java file to hard-wire the buttons so they are evenly spaced. The code below evenly spaces the buttons in Android in a two-pattern grid. (See below for code to use a three-pattern grid.) It is relatively easy to tweak this code so that it can be used in many grid sizes. Keep in mind that this will work across all Android sizes, including tablets. So the buttons could be very large in Android tablets. I will try to get this into a plugin so it is easier to use. //////////////////////////////////////////////////////////////////////////// //grid layout if(buttonLayoutStyle.equalsIgnoreCase("grid") || buttonLayoutStyle.length() < 1){ //hide horizontal scroller, not used... containerViewHorizontal.setVisibility(View.GONE); //tableLayout gets top padding same as button padding... tableLayout.setPadding(0, buttonPadding, 0, buttonPadding); tableLayout.removeAllViews(); usingTable = true; //figure out rows / cols... //int buttonsPerRow = (int) Math.floor(deviceWidth / (buttonSize + buttonPadding)); int buttonsPerRow = 2;//sjm rows = numButtons; cols = buttonsPerRow; //outer loop rows.. int tmpRowCounter = 0; //must have some buttons... while (cnt < numButtons) { buttonSize = ((deviceWidth*100)/235);//sjm buttonPadding = (deviceWidth/13); //table row holds parts... TableRow tr = null; if(tmpRowCounter < buttonsPerRow){ tr = new TableRow(this.getActivity()); tr.setLayoutParams(trLp); tr.setPadding(0, 0, 0, 0); } //re-set for each row... tmpRowCounter++; if(tmpRowCounter >= buttonsPerRow) tmpRowCounter = 0; //inner loop cols.. for(int c = 0; c < cols; c++){ //increment counter here if NOT a vertical layout... if(cnt < numButtons){ //BT_item... BT_item tmpItem = childItems.get(cnt); //relative layout holds button parts...this is the button "size" and the background color... RelativeLayout buttonBox = getButton(tmpItem, cnt, buttonSize, buttonSize + addHeight, buttonPadding, addHeight); //add to list of "button boxes" to keep track of it... buttonBoxes.add(buttonBox); //add buttonBox to the table row... tr.addView(buttonBox, buttonSize + buttonPadding, buttonSize + buttonPadding + addHeight); //increment counter... cnt++; }//cnt < numButtons... } //for num cols... //add the row... tableLayout.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); //if addHeight was used we need a spacer between each row.. if(addHeight > 0){ TableRow spacerRow = new TableRow(this.getActivity()); spacerRow.setPadding(0, 0, 0, 0); RelativeLayout spacerBox = new RelativeLayout(this.getActivity()); spacerBox.setMinimumHeight(15); spacerRow.addView(spacerBox, 15, 15); tableLayout.addView(spacerRow, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); }//if addHeight > 0 } //for num buttons... } //end grid layout.. //////////////////////////////////////////////////////////////////////////// Settings for three buttons per row int buttonsPerRow = 3;//sjm rows = numButtons; cols = buttonsPerRow; //outer loop rows.. int tmpRowCounter = 0; //must have some buttons... while (cnt < numButtons) { buttonSize = ((deviceWidth*100)/350);//sjm buttonPadding = (deviceWidth/21);
 
SheriDee
Code is Art
Profile
Posts: 1094
Reg: Sep 23, 2011
location unknow...
22,840
like
10/28/13 11:13 AM (11 years ago)
Yea! I got to try this! Thanks love! You are the best! xoxox
 
GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
like
10/28/13 11:32 AM (11 years ago)
Nice!
 
Lankster
Lost but trying
Profile
Posts: 7
Reg: Jul 27, 2013
Sacramento
70
like
10/28/13 12:01 PM (11 years ago)
Thought I was would ask? is that in java?
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
10/28/13 12:23 PM (11 years ago)
Yes. The trick is to make your settings look just as you want in the iOS app via the control panel. Then you use this code in your android project (.java file) to evenly space the buttons on the android project.
 
mutzy
Aspiring developer
Profile
Posts: 841
Reg: Nov 03, 2010
Medford, MA
9,860
like
10/28/13 12:34 PM (11 years ago)
I can't wait to try this!!!
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/28/13 02:02 PM (11 years ago)
ace, just what im looking for , thanks sean
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/28/13 02:04 PM (11 years ago)
can you tell me how to move the buttons around the screen pretty please ie move the whole grid down and across sean :)
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
10/28/13 02:43 PM (11 years ago)
I am not sure what you mean, @funkymonkey. This code centers the buttons horizontally on the screen. You could try playing with the numbers a bit to get to whatever you are trying to achieve. If you want space at the top of the screen before the buttons start then try playing with the xml files, and build in some space before the buttons start.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
10/28/13 02:47 PM (11 years ago)
Thanks Susan!! Cheers! -- Smug
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/28/13 02:59 PM (11 years ago)
Thanks Susan , will try that, I have tried to do it, even with snugs help, but I'm bit of newbie, and eclipse makes me wanna cry lol Thanks Sean
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
10/28/13 05:22 PM (11 years ago)
LOL! Eclipse gets better after you get used to it. We all have a lot to learn at Buzztouch, between what we need to know about iOS and Android, and then our Control Panels too. That is why we have to all work together.
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/29/13 05:10 AM (11 years ago)
i have learned tons through the buzz team, i would never have managed to figure most of this out, your right about eclipse, daunting at first, but after 6 sleepless nights lol feel as if im getting somewhere now, thanks, little question, when i import the code above i get error on this row tr = new TableRow(this.getActivity()); saying that its undefined, any idea what im doing wrong :) sean
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
10/29/13 11:35 AM (11 years ago)
I am not sure. I tried pasting the code into a fresh app to see if I could see the same error, and I don't get the error. Make sure you cover up the //grid layout exactly when you cut and paste. Cut and paste just the code between the ///////////////// lines.
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/29/13 12:05 PM (11 years ago)
cheers susan, will give that shot thanks
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/30/13 06:10 AM (11 years ago)
hi susan mangaged to sort it by , putting the individual bits that you changed in, rather than cut and paste the whole lot, prob i have now, is i had 3 rows of 3 buttons, the above lets me centre them which is great, but the other 2 rows of buttons vanish from the screen :0 sean
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
10/30/13 09:52 AM (11 years ago)
You may have missed a piece of code....what about the code under //reset for each row? Is that there?
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/30/13 10:17 AM (11 years ago)
yes your right i did susan thanks, as normal, me not concentrating hard enough lol, thanks, still cant move the buttons down the page, any idea how i would or even how to put something above the buttons to maybe shove them done sean
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
10/30/13 10:43 AM (11 years ago)
I would go into the associated xml file, try the graphical version, and see if you can add some white space. It isn't as easy as IB, but something to try.
 
FunkyMonkey
Aspiring developer
Profile
Posts: 1177
Reg: Aug 07, 2013
blackpool
14,570
like
10/30/13 10:52 AM (11 years ago)
Ok thanks Susan
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
02/11/14 04:20 AM (11 years ago)
I love this Susan - thanks for the share. It works really well. Any thoughts on centering the buttons horizontally on the device? I have played around with the xml file (using android:gravity= "CENTER_HORIZONTAL") with no joy. I know the .java file effectively overrides the .xml file, so does a centering command need to be added to the .java file?
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
02/11/14 06:41 AM (11 years ago)
Regarding centering horizontally - this is hopefully a cheats solution for a 2 x 2 grid using a margin. //add these two lines public int margin = 0; margin = ((deviceWidth*100)/1111); //and edit the following lines in Susan's java //tableLayout gets top padding same as left margin ... tableLayout.setPadding(margin, margin, margin, 0); tableLayout.removeAllViews(); usingTable = true; //must have some buttons... while (cnt < numButtons) { buttonSize = ((deviceWidth*100)/264); // buttonPadding = (deviceWidth*100/5000); // Its not a perfect center, but its gd enough for me. See screenshot https://www.dropbox.com/s/ps8rz2svxipf7o4/Screenshot_2014-02-11-13-29-56.png Sean - if you still haven't found a way of increasing your top margin then you can perhaps add: public int topMargin = 0; topMargin = *your calculation or measurement here*; //and edit this section as follows //tableLayout gets top padding same as topMargin + button padding ... tableLayout.setPadding(margin, topMargin, margin, 0); tableLayout.removeAllViews(); usingTable = true;
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.