Wednesday, July 23, 2014

The First Day...

First day: To be fair, this isn't exactly my first day at this, simply just the first day writing this blog. So I did do a little bit of playing around in Eclipse with sample applications before this. While trying to create an application idea I had around Car Statistics, I realized my need for a reminder type system for myself to take a break. Sometimes I would be googling and browsing through Stack Overflow for the longest time without giving my eyes a break and even missing some of my favourite shows on television because I have lost track of time. So, here goes, my first app, albeit a simple one, one that I intend to finish and publish. 


I began by brainstorming things that I must have in the application, the bare bones of it, as something that works and is basic is always better than something complicated that does not work. So, here's the list I came up with:



Where do I begin... I began by creating a new Android Application Project with a MainActivity with Fragment. I quickly noticed that there were inner classes in the automatically generated code. What are they? 

Inner Classes:
  • Implement helper classes -- instead of having another class outside, you can simply place them inside for more readable code
  • I found this very informative: When to use Nested Classes
To create the clock and buttons (I decided to delay the notify in x minutes feature until I got the start and stop to work), I switched to the graphical layout (res -> layout -> ____.xml and switch to graphical view tab on the bottom) and pulled the items onto the "screen." Changing to the ___.xml view, I fiddled around with some numbers until I got my "clock" large enough.

onCreateView:

My next challenge: implementing the button activities to recognize the clicking of off and on. As my MainActivity had a fragment, I initialized the buttons there. Some quick googling later, I was off and had the first bit of code:
 @Override  
           public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                     Bundle savedInstanceState) {  
                
                final View rootView = inflater.inflate(R.layout.fragment_main,  
                          container, false);  
                final Chronometer chron = ((Chronometer) rootView.findViewById(R.id.chronometer));  
                final ToggleButton newButton = (ToggleButton) rootView  
                          .findViewById(R.id.toggleButton1);  
             
                newButton.setOnClickListener(new OnClickListener() {  
                     @Override  
                     public void onClick(View v) {  
                          // do something
                     }  
                });  
                return rootView;  
           }  

What exactly is this doing?
  • When the View (what you see) is created, the inflater.inflate(....) method literally inflates the layout made in the XML file, including the buttons and chronometer. 
  • The word final stops the variable to be assigned to something else. This post answered my questions: What does "final" do?
  • Each time we initialize the buttons, we must have some way to find the button. We do this by referencing their ID which can be seen in the XML file, with tag android:id="@+id/____". Changing the ID in the XML file will also cause you to change it here in the code. The '+' sign in front of the ID is only necessary if you are using it for the first time.
  • Setting the onClickListener is similar to the Observer method where when it is observed, it pushes it to the 'listeners'. We can choose what we want to do with the onClick method, in this case, we want to start the chronometer.

No comments:

Post a Comment