Skip to content
Coding Fanatic
  • Android Development
  • News and Reviews
  • Road2Pro
  • Tutorials
  • Services
  • About

Switching to OnClickListeners, 47/100 Days of code

  • September 30, 2019September 30, 2019
  • by Richard Clarke

In my latest update to the Travel app, I refactored my code with onClickListeners. This makes it easier to maintain later down the line.

I updated my Travel app to use onClickListeners to start Activities when needed. You can find the video on this at the bottom of the page and a link to the source code here.

To do this, I removed the android:onClick XML attribute from the TextViews in the activity_main.xml file. These attributes are used to invoke a method when the TextViews are clicked.

Next, I set ids for each TextView using the android:id XML attribute. Afterwards, I instantiated TextView objects in MainActivity.java using the findViewById method to reference the TextViews in the activity_main.xml file based on their ids.

Finally, I used the setOnClickListener() method to configure each TextView to create an Intent and start their respective Activities when clicked.

I removed the android:onClick XML attributes from the TextViews in order to keep all code related to user interactions in the MainActivity.java file. Assigning ids to the TextViews used for options allowed me to reference them using objects in MainActivity.java. Creating these objects lets the class configure how the TextViews behave by overriding methods from the View class.

Keeping code related to user interactions in one place makes it easier to maintain my code. I know where to look in the event something doesn’t work when users click the options. Now that the logic is only stored in the MainActivity.java file, I have optimized debugging proactively. It’s easier to find your keys when you always keep them in the same place.

Next, I will configure the List_Addresses.java file to display the list of addresses being used for this app. For more on this, watch the YouTube video below. For the source code click here.

Join the mailing list to see updates like this every week!
Coding Fanatic

Related

Setting a Parent Activity, 46/100 Days of Code
Travel App ListView Update, 48/100 Days of Code
Richard Clarke
Richard is currently a developer using Java and Android Studio to build custom applications for Android. Richard tutored in mathematics at Montgomery College, and worked in Information Technology before entering the Software Development industry as a Quality Engineer.
100daysofcode Android AndroidDev Java

Related articles

Your New Favorite Stopwatch App!…
Two for Two BABY! 7…
…But the App is for…
Play Store Blues – 5…
Top 4 Tips for Android…
No Dice – 3 of…

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • GitHub
  • YouTube
  • X
  • Instagram
  • LinkedIn
My Tweets

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • X
  • GitHub
  • LinkedIn
  • YouTube

Archives

Theme by Colorlib Powered by WordPress