• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simplifying code to understand the code  RSS feed

 
Nathan Grealish
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

Just as a quick back-story, I've been a graphic designer for 15 years and a web developer for 6 years now. I've taught myself a ton of web languages, but I'm finally making the jump to Java (for Android specifically). I can write most of a program using javascript/jQuery, but I know that Java and javascript are not the same! So, I am getting stuck in my own head.

I have this code (below) that I've written in android studio. It works, but I feel like there has to be a better/easier way.



Here's what it looks like:


Now, in javascript, I know I could write this same thing in 4 lines (with a few extra lines of CSS




So, I'm trying to get this mental "key" where I can start associating things. Is there a better way of writing these fake checkboxes? Can I style a checkbox to look like those buttons and let the default behavior work? Where do I need to go with this?

Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Wow, that is indeed some horrible copy-and-paste code, and you could indeed do that in a much better way. For example, instead of making 16 variables for the 16 buttons you could put them all in an array, and then loop over the array, and then you wouldn't have to have 16 copies of the code that follows - you'd just have that once, inside the loop.
 
Nathan Grealish
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Idea. I will start looking into Java Arrays. I know there has to be a better way, but I don't even know what to search for when it comes to Java!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!