Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 12    Views: 981

krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
08/13/13 05:13 AM (12 years ago)

Making attractive buttons (Android)

I would like to use the buttons as seen here: http://www.dibbus.com/2011/02/gradient-buttons-for-android/ as part of an interactive quiz app I'm creating. As suggested by the author, I have created .xml files for each colour button (e.g. blue_button.xml) and saved them in the Res/Drawable folder. Now on to the bit I'm having difficulty with. In the Buzztouch interactive quiz java source code file there are lines such as - btnAnswer_1.setBackgroundColor(BT_color.getColorFromHexString(quizButtonColorAnswers)); which is retrieving a color from the input provided in the BT Control Panel. How do I alter the source code file to point to the 'blue_button.xml' file instead of the BT Control Panel? The following line was recommended in the thread accompanying the button article: btnAnswer_1.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_button)); but this line gives me an error as follows: - Line breakpoint:BT_screen_quiz [line: 263] - onCreate(Bundle) - context cannot be resolved Any ideas? If this can be resolved, i'll happily write a how-to article for others to follow.
 
Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
like
08/13/13 05:38 AM (12 years ago)
Funny, I used that exact same tutorial a while ago but it was on another website. I use this: btnAnswer_1.setBackgroundResource(R.drawable.button_normal); (button_normal is the name of the xml file in the Res > Drawable folder)
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
08/13/13 06:36 AM (12 years ago)
Raveyd - We are half way there. Your line resulted in no errors - so thank you very much. However, the blue button isn't appearing as there is still this line in the java source code: quizButtonColorAnswers = BT_strings.getStyleValueForScreen(this.screenData, "quizButtonColorAnswers", "#3F3F3F"); The emulator is picking up the default value - #3F3F3F. If I remove this whole line from the code then the app doesn't work. If I alter the line to: quizButtonColorAnswers = BT_strings.getStyleValueForScreen(this.screenData, "@drawable/blue_button", "#3F3F3F"); then it picks up the default colour. Did you alter / delete any other code in the source file?
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
08/13/13 06:40 AM (12 years ago)
@krompa, Yeah it looks like @raveyd has given you the correct name and format of the drawable xml. I not understanding why you cannot do the buttons in the layout.xml though and reference in the java file. Either way, it would work! You can also comment out the color scheme for the original strings in java. LA
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
08/13/13 07:06 AM (12 years ago)
Hi LA. I am a real amateur and only started developing apps very recently. The main reason for having separate .xml files for the coloured buttons is that the button changes colour (blue when question is being asked, green for correct answer and red for incorrect) and the logic in my mind finds it easier to see where this occurs in the java code. Unfortunately, I can't work this out for the layout.xml file. I have commented out the original string in java and the app will not load on the emulator as a direct result. If I undo the //comment, then it works again (using the default #3F3F3F).
 
Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
like
08/13/13 07:07 AM (12 years ago)
Sorry for the delay, just firing up eclipse. Hi LA, how are you? @krompa, there's a second change needed further down the java file, do a search for "//set button backgrounds..." without the quotes. In my file it's around line 1135, but that'll probably be way off on yours. It's a repeat of the code above, but for the next question buttons. Also, make sure you have the right settings saved in the bt control panel, you can switch on/off the answer button colours (red/green).
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
08/13/13 07:09 AM (12 years ago)
I'm doing well @raveyd, and you? @krompa you are in great hands with @raveyd! LA
 
Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
like
08/13/13 07:15 AM (12 years ago)
@LA, Glad to hear it mate :) I'm fine, just wish there were about 30 hours in the day!
 
Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
like
08/13/13 07:27 AM (12 years ago)
Just refreshing my memory, I've slept since doing this... You can over-ride the red/green colours for wrong/right answers, do a search for: //change button backgrounds, red for wrong, green for right and try this code: if(correctAnswer.equals(btnAnswer_1.getText().toString())){ btnAnswer_1.setBackgroundResource(R.drawable.button_right); }else{ btnAnswer_1.setBackgroundResource(R.drawable.button_wrong); }
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
08/13/13 07:48 AM (12 years ago)
Raveyd ... seriously ... thank you. Many hours spent looking into this and finally a positive outcome thanks to your input. With your suggested update at line 1135, the lines causing a problem can now be commented out as suggested by LA. In addition to the basic question colours, I have also updated the buttons for correct and incorrect answers (at approx line 720 in the source file). So all buttons looking very smart.
 
Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
like
08/13/13 08:03 AM (12 years ago)
Great news @krompa! It's always worth doing these mods and tweaks, they can really make an app stand out from the crowd. Good work! btw, You might find it useful to comment any changes you make in the code, it makes it lots easier to refer to for your next app! Start the comment with your name, "//krompa" then you can just do searches for your name.
 
GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
like
08/13/13 09:09 AM (12 years ago)
@krompa....would love to see a tutorial on this! Seems like a nice addition to Android GUI development. Thanks! Mark
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
08/13/13 11:40 AM (12 years ago)
I'll knock something together. I think it will help bring a bit of variety into the world of quiz apps.
 

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.