Dur, Spelling is Hard

Hey guys, Another weekend of lots of gaming and near enough sleep, but I got back at coding Sunday afternoon. Started out with lesson 15 over at MyBringBack. In this lesson we finished up the current activity we’ve working on. We made the Radio Groups for Gravity and Typeface work. We fleshed out the onCheckedChanged method we created as a stub last week. I’m not really sure how to walk through this, but I’ll post a picture of my code.

Code that makes our Radio Buttons work

Let me take a stab at this… Let’s look at the first case (rbLeft). In case our Left Radio Button is checked, set the Gravity to Gravity.LEFT. That will make the text align to the left side of the screen. Well, that wasn’t too bad. But it get’s more interesting when we get to the Typeface ones, so let’s look at rbNormal and see if we can make sense of it. In case our Normal Radio Button is checked set Typeface to Normal. That is what it does, but does all this mean? “Typeface.defaultFromStyle(Typeface.NORMAL), Typeface.NORMAL” Why do we need all that instead of just something like Typeface.NORMAL? I’m not sure. Hopefully it all becomes clear at some point in the future. In the mean time let’s take a look at our activity in action.

Our text with Typeface set to Normal and Gravity to Center

Typeface set to Bold and Gravity set to Right

Lesson 16 had us working on the menu. You might have noticed that this blog post is really late. Well that is because the lesson burned me out a bit. It all seemed simple enough. And in reality it was. But my inability to spell hurt me. Worse than that my inability to notice my spelling mistake for the two hours + I spent staring at my code to try to solve my issue. I ended up sending my who project to a friend who knows more about coding than me and he found my error (Thanks Glen!!!). By that time I was pretty discouraged and spent the week gaming instead of coding. Ready to get back at it now.

Code Monkey Works with some Java

Hello Everyone!

Been busy the last couple days with some boring stuff like lawn care (I was trying to grow a jungle, but my wife didn’t like that plan). I only managed to get one lesson in. It was Lesson 14 and was all about using the getText() and setText() methods. There really isn’t a whole lot to talk about here. We set up a few more ids on our radio groups from lesson 13 and then we started setting up the methods to make them work. We set up textOut to output text, or at least we started to. I don’t think we finished anything yet. We set up textIn to get the text we will output. We also set up our radio groups so we can get them to do stuff later on.

Our TutorialOne.java code after lesson 14

My First App

I started working on the layout of my sound effects app. I’ve decided I will do as much of it as I can on my Nexus 7 using AIDE.  AIDE is a Java IDE that can build and compile apps right on your android device. I highly suggest checking it out if you make apps at all. I see myself mostly using it just to tweak or make small changes later on, but I thought I’d see if I can build a basic app with it. As I said I’ve only just started building my layout. If I can get my wireless keyboard running it’ll be much faster sailing. Stupid batteries! So far I have got one layout file started with 4 buttons across the top that I used a Horizontal Linear Layout and weight to appear as tabs at the top of the screen. I plan to add 3 columns of 4 or 5 buttons each. I tried to add a Text View under the tab buttons, but I couldn’t get it to appear properly. I am pretty sure I need to give it it’s own Linear Layout to get it to work. I’ll try to do that tonight.

Sorry this post is so short. Hopefully

Monkey Learns More About Layouts

After a weekend of Wedding Anniversary celebrations and late night gaming sessions, I was happy to get back to coding!

I had a little bit of trouble getting back into mybringback’s lessons. What should have been the next lesson was talking about things that we hadn’t done yet. Where did that radio button come from? How did he make those two text views appear side by side like that? After I clicked through the lesson pages a few times, I determined that lesson 12 was in fact missing. I tracked it down on his YouTube playlist. What’s really funny is that after I completed the tutorial I clicked back through the webpages and found it there. Weird.

Lesson 12 was all about setting up a more advanced XML layout. Not too much was new, which is nice. The main new thing we learned was setting a layout for our activity and setting a component’s weight. We took out new activity (TutorialOne.java and it’s XML file: tutorial1.xml) and added an Edit Text field, a button, a Radio Button and three View Text fields. I guess the Radio Button was new too, but we didn’t really do anything with it except for dragging it into the design section from the palette. When we added them they were all a jumble, so we worked on tweaking the layout so it made sense.

A somewhat jumbled layout

Now that doesn’t look terrible, but it isn’t pretty either. And is not what Travis wanted it to look like. So we did some tweaking. I should note that he had us add a vertical orientation tag to our linear layout. Android Studio seemed to have already added this to mine. We reviewed gravity by centering a few things. Then we set a few of the components to fill the parent or wrap content depending on what effect we desired. This was review as well. Travis wanted the two text view directly following the edit text to appear on the same line as each other instead of occupying two lines as you see above. To do this we set up a new linear layout within our existing one, by simply copying the opening tag and pasting it between our edit text and the first text view components. Then we added a closing tag after the second one. Since we want this views to be side by side on the same horizontal line we changed our new orientation from vertical to horizontal. When we had a look at what this changes did we noticed we now only one text view and big empty space beneath it. We had to add weight to make it work.

Code from our Weighted Linear Layout

Weight was new to our lessons. If you look at our code above you can see that it was fairly simple to set up. All we did was tell the linear layout how much weight it had by using android:weightSum=”100″ as seen on line 19, and then we told each of the text views that they could use half of that weight. android:layout_weight=”50″ can be seen within both TextView’s on lines 27 and 36. This told both TextView’s that they could use 50 of the available 100 weight, or 50% or One Half. So they should each appear using half of the line. Travis also explained that we could get the exact same result if we set the weightSum to 2 and the layout_weight’s to 1. In my mind I took this further and decided if we want to split the line into 3 it might be best to use a number that is a multiple of 3. 100 does not divide into 3 parts using whole numbers… I wonder if weight can use decimals. If it can there really isn’t a problem. Or if the remaining weight is split and used a margin on either side it would work well. It also occurs to me that we could use weight in a vertically orientated layout to spread things out down the screen. Anyway, you can also see that we centered the gravity of both TextView’s. Let’s see how that looks.

Our Layout after Lesson Twelve

We continued to set up our new layout in Lesson 13. New things learned here included RadioGroup. RadioGroup makes Radio Buttons work as they are meant to. When you select any one button within a RadioGroup it will automatically deselect all others. Neat!

We started the lesson by adding 5 new RadioButtons by simply copying and pasting the existing ones. Next, we set up a new LinearLayout around all six RadioButtons with a weight of 2 because we wanted two columns. Then we used our new RadioGroup  and made one with a vertical orientation, fill_parent width, wrap_content height and a layout_weight of 1. We set this up around three of the buttons, and then did the same for the remaining three. This gave us 2 columns of 3 buttons each and within each column only one button can be selected at any one time. We finished the lesson by setting up the buttons and text fields with the proper words as well as changing the ids to be what we want them to be. Travis gave good suggestions for ids. He uses an abbreviation of the type of component and a quick label describing it. A TextView that says Style would have the id of tvStyle and a Radio Button that is labelled Bold would be rbBold. This seems to work well and I think I will adopt it. How do you handle ids? Any suggestions that work better for you?

 

A look at Our App after Lesson 13

It’s looking pretty good if I do say so myself. One thing I’ve noticed is that Travis’ app now fills the whole screen of his AVD, while mine doesn’t. I need to figure out how to make the radio buttons bigger. Any quick advice?

My First App

Welcome to a new segment of this blog. I’ll talk about an app I’m starting to work on here and discuss the progress I am making. So far I’m just deciding what to make. I know it is going to be a sound board, and I so wish I could base it off something popular like my beloved Star Wars, but I want to keep it legal and legit and whatnot. Then I thought that having it be a bunch of Monkey Sounds would be cool for the Wannabe Code Monkey, but it turns out free to use monkey sounds are hard to find. If I had a zoo nearby I would go try to record some, but I don’t. Currently I’m thinking I might just try to use a variety of  generic sounds. After all I’m sure the only people that will check out the app will be readers of this blog and family and friends.

Thanks for reading!

Code Monkey Does Not Wish to Be Sued

Hello Monkey followers!

Before I get into the lesson’s I’ve done since the last post I wanted to just talk about this blog a little. First up is my update schedule. I plan to post something every Tuesday and Thursday. Hopefully with each post I’ll cover 2 or three tutorials for as long as I work on them, and when I’m done then I’ll post about what I’m learning as I develop apps. You might also see the occasional weekend post if I get wrapped up in something development related and want to share, but mostly my weekend computer time will be spent gaming.  I’d also like to talk about what this blog will focus on and what it’s become. I never intended it to be a step by step tutorial, and it won’t become that. But at this point I feel it is a good supplement for anyone working through mybringback’s tutorials. It is my hope that even those not working through them will find them informative, or at least a little amusing. Let me know what you’d like to see!

I started working nights this week so my days are a little messed up and thinking is more difficult, but I think I’m still learning. Lesson nine from mybringback was all about audio. It went pretty smoothly. Adding a media player was remarkably simple.

MediaPlayer logoMusic;

That’s it. Well, that’s it for adding the media player. To start it wasn’t too much harder. I’ll put the code snippet that got it running below.

This code starts the Music running

I did have a few problems with this that I’ll go over quickly in case it helps anyone else. 1) Where it says MainActivity up there, that is the class you are working in. Travis’ had main there in the tutorial so I just wrote what he did and it didn’t work because I don’t have a main class. I used Android Studio’s tool tip on the error to sort things out. 2) Next my music file (The Mermaid) was throwing an issue. The tool tip said it was an invalid name. So I removed the space in it and changed the first capital (theMermaid). AS still didn’t like that so I changed it again to themermaid and it worked no problem.

Before we ran the program we added an onPause method to our activity, and told our MediaPlayer to release the music when the activity paused. This caused the music to stop when the 5 second sleep timer ran out and the app moved on to our menu screen.

mybringback’s Lesson Ten makes our buttons work! Yay! OK, let’s get into this. Our buttons were made earlier, so all had to do with them was make sure that they had id’s. “@+id/tutorial1” That was easy. We also had to create a new xml layout file. Travis left his completely blank and to start with I did as well, but when I ran into trouble I added to it by putting in my ugly background an a TextView. I’ll get to the trouble I had later, as well as my eventual solution to it. We set up the button in our menu class.

The Code from our Button

We learn the basics of the OnClickListener from this. We set up the button first by naming it and telling it what id it is. Then we tell it to listen for clicks. Finally when it clicked (onClick) we start a new activity with the intent that follows. This was a new way of doing intents and I feel it cleans things up as opposed to having a line of code above it. I’ll try to use this way in the future when possible. If we tried to run our app at this point, it would not work. We haven’t declared the activity in the AndroidManifest.xml file. To do this we copied the last activity we made and pasted it between the <application> tags there, then we edited the android:name to and intent-filter android:name to match the new activity we set up. We left the category as DEFAULT so that the application would know not to use it as the launcher activity. That was pretty much it. Except my app crashed every time I tried to run it.

Uh-Oh

I’m not proud of how long it took me to find the problem. Travis had name various things in this lesson “TUTORIALONE”, “TutorialOne”, “tutorialone”, and “tutorial1”. This confused me to no end. I’ll need to get better at watching what labels are put on what. I spent over 2 hours looking for the issue. I looked at comments from the tutorial page and others that had a similar issue solved it by added a background to the layout file, so I did that. No dice! I finally sorted it out by re watching the video while following along in my code to find what should match up to what. I had made a mistake in the AndroidManifest file with naming the intent filter properly. After that it ran properly.

Lesson 11 got me excited. In the introduction on the mybringback web site he said that with the knowledge from this lesson we would be able to make a soundboard app and publish it, maybe start making money. That sounded great to me! First, let’s look at the lesson.

In this lesson we added sound to our buttons. He suggested grabbing sounds from soundjay.com so that’s what I did. I went with button 1… This lesson was mostly a review with really only one new thing going on that we didn’t use in our splash screen sound example. What we did was set up our second button to work the exact same as our first (sort of boring going to the same activity again) and then added a sound to them. We still used the media player instead of the media pool.

Adding Sound to a Button

As you can see on line 22 we set up the MediaPlayer. We didn’t initially make it final, but then line 32 threw an error. I’m not sure exactly why, but Travis said to add final to the MediaPlayer that made it work. That is pretty much all there was to it. Not too difficult at all and a nice review.

So after reading the introduction to the lesson I was excited. I thought I could make a Star Wars soundboard app and sell it in the Play Store. Get my fee (I think it’s $25) out of the way and maybe start gaining money to offset it before any of my other apps our ready to be made. Seemed like a fun idea! Then I thought about it longer. Isn’t the audio I would want to work with under Copyright? Doesn’t that make sound boards using them (or any other copyrighted audio) illegal? I answered yes to those two questions. So it begs a third question: Why do I see so many sound board apps on the market? Surely they didn’t all receive permission from the copyright holders to use them! Is there something I’m missing here? Can I go ahead and publish a sound board app without being sued into oblivion by Disney/LucasFilm?

Who Said Monkeys Can Spell?

Hey guys!

Lesson 6 from the mybringback Basic Android Tutorial Series was the one I’ve been waiting for. We are starting to learn Java and how it relates to Android development. I have no experience with Java programming so it’s all new to me. All the basic layout stuff is really interesting, but I remember it all from last year. I don’t remember any of the actual coding. I think I’ll just put up some code and attempt to explain what it means.

Code from main activity

Okay, so line one simply sets up the package with the name I gave it when I created it. Maybe someday I’ll set up a custom URL so I can use that. Lines three, four and five bring in libraries (?) from Android so they can be used in our app. I remember from before that you need to import different things based on parts of the OS or device you want to use such as Camera, internet, mic, ect. Line seven creates a new class which is Public, meaning it can be referenced by other classes, and it extends Activity (imported on line 4) which gives the MainActivity class all the characteristics that the Activity class has. At this point we could call MainActivity the sub-class and Activity the Super-class.

Line nine is where it gets confusing for me. Mybringback says “The “@Override” means we are overriding the method from the super class (Activity Class).” I’ll just leave it at that and continue on. In line 10 we call the onCreate method from the Activity super-class. The tutorial didn’t really specify but I think this method is used to make an activity (makes sense, doesn’t it?). This would pretty much make a page as far as I can tell. We also want to make some changes to the onCreate method and that is why we bundle it with TonyIsAwesome so that it isn’t exactly the same as onCreate because that would be boring! Line 11 is where we actually create the method. Note how we referenced the super-class. In line twelve we point to what layout file we want to display.

Lines 16-20 didn’t appear in mybringback’s code. But I think I know what it is anyway so I’ll hazard a guess. Line 17 calls onCreateOptionsMenu which I assume is why there is a menu button in the top right corner of my app. I guess this is either a built in feature of Android Studio or it is something that the SDK started including around Honeycomb or Ice Cream Sandwich. It’s a boolean, so that means it can either be true or false. Line 20 indicates it is true. I’m not sure exactly what happens in line 19, or at least I don’t know how or why it happens. I know it makes a menu button and calls up a settings option in my app. Clicking on settings doesn’t do anything. Yet. I’ll add to it when I learn how to.

This tutorial also had us add a new folder under src/main/res called raw. He had us put a sound in the raw folder. I searched my PC for wave files and found one called JKIIstart.wav. I didn’t listen to it. I assume it’s a sound file from Jedi Knight II. It’s also short and I thought that would probably be good. This .wav file seemed to have caused some issues. AS wouldn’t associate it with a file type. I chose to just remove it until I found out what we were doing with it. I also tweaked the background I had made earlier to be less transparent and set it up in the splash layout.

I’d love to hear from in the comments. Am I missing anything? Can you fill in the parts I’m unsure one?

The Next Lesson talked about Android Activity Life Cycles. We didn’t do a whole lot in here other than some set up to get a working splash screen. I’m going to put up an Android Life Cycle chart for reference, but other than that I won’t say too much about this lesson, as when it finished we didn’t have anything new working. In fact we might have broke a few things.

Activity Lifecycle chart

Lesson Eight! Android Splash Screen with a Thread. This lesson caused me all kinds of problems, but not for any good reason. We learned how to set up a Java thread with a timer and edit the AndroidManifest file a bit. I learned that a thread requires a Try, Catch, and Finally section. In the Android Manifest we told it that we had another activity and specified which should launch first. We told it to start with our Splash Screen by labeling it with a LAUNCHER category. I believe we will set all other activities to DEFAULT so they will only be started when another activity calls on them. This what we did with our new Menu Activity.

After 5 seconds on the Splash Screen the app now automatically forwards onto the menu.

The timer thread I talked about earlier was set to 5000 milliseconds so that it will stay there for 5 seconds, then run an intent to call the menu activity and finish the splash screen one. When all was said and done I tried to run my app in the emulator. It got errors. I spent 2 hours tweaking and pouring over my code trying to find them. It turns out if you spell “Activity” like “Activiy” it doesn’t know what to do. I was both mad and relieved when I found the error. It just goes to show how little mistakes can really throw you off in programming.

Travis (the mybringback tutorial guy) mentioned that Splash Screens can be a good branding tool within your apps. I’d like to say that I’m not sure I agree with him. In our example it just added 5 seconds of time that the user can’t interact with our app. To me that would just frustrate them if there is something they want to do. Maybe I’m missing something. To the real developers out there… What do you think? Do you use them?

Code Monkey Gets Gooey

See what I did there? No? Well, you might get it later.

Lesson 4 on mybringback was all about setting up a layout via a XML file. Good video and I learned some things. Including the fact that linear and relative layouts are different. I learned this the hard way as I tried to follow along in Android Studio while mybringback used eclipse. In eclipse the default layout file created a linear layout whereas AS created a relative one. Of course I didn’t notice it for a while and nothing was working properly. Once I changed over to a linear layout my layout suddenly looked like his. I’m having some trouble with the layout working (or not working) on Extra Large screens like the Nexus 10. It appears it has a seperate xml file, but I couldn’t get it to work properly. I think it also led into another problem I’ll get to in a minute.

After my mybringback showed me how to center a textview, I decided to figure out how to do the same with my button. I though it would be to add a gravity tag, but it turns out it needs to be layout_gravity for a button instead of just gravity like for the text. Next I made a really quick and ugly background image to add to my layout. I had all kinds of rendering issues with it. Mybringback showed to put the image in the res/drawable folder, but I seemed to have 4 different res/drawable folders(drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhpdi) so I stuck my background into the drawable-hdpi folder and then tried to get it to work using android:background=”@drawalbe-hdpi/background1″ It didn’t work. Eventually I tried it exactly as mybringback had it, which was android:background=”@drawable/background1″. This taught me that you don’t need to point to the specific screen density. I imagine this means that I should build the background to all screen sizes in the future and simply name them the same put in the appropriate folder. That would probably make the image look a lot better than it does on some of the screen sizes in the layout preview. A lot of them looked very stretched and distorted. I’ll keep that in mind for the future.

It was at about this point that I ran into a major issue. I went to run my app in my Nexus 7 Emulator and got errors. I’m still not sure what they meant or why they happened, but I ended up with a “Schema” problem. Android Studio said that the URI was not registered. A little looking around on stackoverflow told me this was a somewhat common issue (bug?) in the Intellij IDE on which AS is based. The only thing I could find to do was create a new project and copy over my files from the first one. This fixed my problem but was annoying. I hope it doesn’t happen again. I also hope it wasn’t caused by something I did. One error message makes me think it had something to do with the xlarge layout file I had. When I got that sorted I moved onto the next lesson.

My App after lesson 4

Lesson 5 from mybringback was more of the same, only without the XML file this time. By the way, did you get the title of this blog yet? “Code Monkey Gets Gooey” Gooey… GUI… Graphical User Interface. Yes, it’s a lame pun, but blogs are tough to title, OK? Graphically editing the GUI was pretty straight forward. The biggest thing I learned was that in AS you must right click on or in the res/layout folder in order to create a new xml layout file. For some reason I kept clicking other places and couldn’t figure out why I wasn’t getting the right option. At the end of the video he gave the homework of creating a new logo for the app so I finally installed GIMP and made a quick and ugly logo.

The Basic Series Logo

My Quick App Logo

I’m going to take the rest of the night off and spend time with my baby girl. If she has a nap I might work on a nicer background image.

The Code Monkey Slowly Remembers

Hey guys, Wannabe Code Monkey here!

I had family visiting over the Long weekend (Victoria day here in Canada) so I didn’t get too much done on the coding front. I did get Android Studio installed and working without too much trouble. A bit of problems after I loaded it when I tried to open it nothing at all happened. Turns out I hadn’t installed Java JDK on my new Windows 8 build yet. Then it still wouldn’t work until I had set up a home variable for it. I figured this out by finding a post on stack overflow.com.

I also watched a few YouTube tutorials (mybringback and thenewboston) in my downtime before the family all woke up. These were mostly redundant when using AS (Android Studio) as things are already built in, but I chose to watch them anyway as a refresher. I got as far as setting up a new project. Things started coming back to me. While looking at activity_main.xml in res/layout I saw the TextView and went to change what it said. AS was kind enough to tell me that what I highlighted (and was about to delete) was just pointing to a string elsewhere. I remembered I could find the strings in res/values/strings.xml. I also remembered thinking Strings were really cool so you could change things in multiple places at once just by altering the string file. If I recall correctly this can also really help with translation to other languages. After finding the strings file I changed the defualt hello string to something different and then added a second line of text simply by copying the TextView in activity_main.xml. This caused an issue since the original TextView had an android:id that I copied over. AS didn’t like having two of those the same as far as I could tell. I simply deleted the line from the second TextView and that worked. Next I changed the text from the hello string to just some text, with no string attached. Again that worked, although AS protested and told me I should really use a string. That’s nice of it. Also nice is that it opened a dialog box to quickly turn my text into a string. Fun! Oh dear, I find THAT fun. I think I need to play some video games to realign my head! Once that was taken care of I had a look at the design tab for activity_main.xml and noticed a new problem. The two strings of text were showing up over top of each other on the first line of the tab. Since I couldn’t figure out how to fix this when staring blankly at the text tab, I headed back to the Design tab and simply dragged and dropped the second text string down to the next line. When I went back to the Text tab it had added

android:layout_alignLeft=”@+id/textView”

to the second TextView’s field. @+id\textView is the id of the first TextView. Interesting.

Android Studio multiple layout view showing my test app

The next reasonable course of action I could think of was to get this running in an emulator. I created an AVD. I opted for the pre built definition of a Nexus 7. It wouldn’t run. I thought about searching around online to find out why, but decided to poke around first. When I went to edit I noticed a message at the bottom stating “On Windows, emulating RAM greater than 768M may fail depending… Try progressively smaller values of RAM is the emulator fails to launch…” Part of the message was cut off, but I got the main point and reduced the RAM to 700 and it started no problem. Now I’m mostly just messing around before diving into the next videos.

In other slightly related news: I had held off picking up a Nexus 7 since all the rumors pointed to a new version being released at I\O. Since it didn’t come I picked one up. This will replace my Acer Iconia A500 that isn’t working at the moment. I still hope to get it running again, but I wanted something smaller anyway. There are a few features I will miss from the A500, but so far I love the Nexus 7!

My new Nexus 7

I think I’ll go play around in AS a bit more before calling it a night. As always comments and advice are welcome!