Today I created a higher or lower game in Android Studio. Used Random class to generate a random number when the app launches.
Created a method that takes the user input when a button is clicked and converts the String to an int. Then I used If statement to take the int object and compare it to the random number that is generated. The app will tell the user higher or lower based on which statement is true. If the number is correct, it tells the user “That’s right! Try again!” and added the Random number code to generate a new number so the user can play again.
Went one step further and created a method called makeToast that takes a String object as its parameters and will display a message to the screen. So when the makeToast function is called, it will display the appropriate message.
This was super fun and it was great to see how to further simplify code by creating the makeToast function. I didn’t see it right away that it was possible, but with time I think I will get better at simplifying my code. And a photo of the app!
100 days of code: D8&9
Today I built currency converter application to take in Dollars and use toast to pop up the message in pounds. Even added the little squiggly pound sign to make it look more official. This was the end of the Android course section two. Next is the Java deep dive which I am really excited about. Some of the takeaways from the currency converter are using
In this example, the String.format
sets the decimal point to two places and accepts the variable it will be formatting
as the second parameter. If you are wondering what the “%.2f” means, like I
did, %f is the format specifier for float data type in the functions printf and
scanf. This format specifier will display up to six digits after the decimal
point, but using %.1f or %.2f will make the precision to the first or second
I added in the currency photo from Google and matched parent width.
I had to take the EditText
(which is a String) and create a new variable and parse it to a Double. I added
the conversion and assigned it to the variable poundAmount.
Then I used toast to display
the conversion to pounds at the bottom of the screen.
Here is the code snippet:
And the application output!
100 days of code D6 & 7.
Created an application that takes two photos uploaded to the drawable folder. Added a button called New Cat and when clicked, will display a photo of a different cat. First tested that my button worked by using the Log.i to display a message to the log to ensure that my button was clicked. Created the onClick function called “clickCat”. Worked very similar to the EditText, only used ImageView and typecasted the findView to ImageView. The second part was to use
to set the image to the second photo upon being clicked.
You can use Properties > Scale Type to change how the photo is displayed in the screen. FitXY stretches it to the edge of the container, fitStart keeps it in proportion and aligns to top of container, Center keeps it in proportion and centers the image. Center is probably one I will use the most.
And here are my results!
Beautiful cats aren’t they?
Day 5 of 100 days of code.
Learned about Toast today. I think its cute they call it toast because it pops up at the bottom of the screen like toast does out of the toaster.
Toast can only be Long or Short: meaning it will show up at the bottom of the screen for a short or long time. You can also use the variableName.getText().toString() to retrieve what the user entered and display it to the bottom of the screen as well as concatenate any message you want to go along with it. In this case, I added “Hi there, ” so it would display before the name retrieved.
I made a small toast program that has a TextView, a PlainText spot to enter your name, and a button that says “Click me!”.
In the MainActivity, I created an EditText variable and typecasted so the program can find it in the Resources. Here is the code to create this program and the result below!
Overall, I thought this was a neat little bit. I remember watching this video like a year ago and I didn’t think I was ever going to understand it. Well, LOOK AT ME NOW! 🙂
Days 3 and 4 of 100 days of code.
What I’ve Learned:
I had a problem getting my project set up and kept getting the error “Add Google Maven Repository and Sync Project”. The solution to this was to go into the Gradle Project: Build files, find “allprojects” and inside of “repositories” add google() and sync. This error was realized in the MainActivity, my class file for the “import android.support.v7.app.AppCompatActivity;” was read and the AppCompatActivity was red. So, if you ever have this problem.. this solves it. I was trying to run a version of Android that was too old since I am working on the Android N Developer course and had to update it to SDK version 28 and it required the Maven repository.
If you are struggling with getting layout to work in the activitymain.xml, check one of two things. First, go into your component tree and see if you have ConstraintLayout or RelativeLayout. RelativeLayout is a lot easier to work with since you can drag and drop the components onto the screen. Right click ConstraintLayout and “convert view”.
Second, if you ARE using RelativeLayout and they keep popping to the top left corner, go to the top left of your design screen and you will see a tiny little magnet. Click this to turn autoconnect on/off.
Something interesting I found out is 1 dp is roughly 1/60th of an inch. So 160dp is about 1″ on any Android Screen. Good to know for design!
If you are working with TextView and trying to retrieve the text from it by clicking a button, you will create a function that is called when you click the button. You will declare a variable of EditText and typecast EditText to the findViewById (since find VIEW is obviously looking for a view). “R” is for resources and then you will find the ID you entered for the component in the xml file. The button you created on the screen will also need an onClick function saved to it in the XML. You can use Log.i which takes a String tag and String msg parameter. I used the tag as “Info” and used the ID and getText().toString() function to return what it retrieved and convert it to string to display to the log. Code below to explain!
Find this helpful? Connect with me to get these kind of tips everyday!
Thank you for checking my post out.
If you’re reading this, chances are you are in the same boat as I am: learning to code and finding your way. Most likely, you’re interested in other things and maybe you share some of the same passions as me.
I am also interested in health and fitness, meal prepping, nutrition, helping women in technology, sharing knowledge, reading self-help books, self-care, playing piano, and advocating for others struggling with addictions and helping them overcome it.
The main focus of this blog is just to jot down all the little tips and tricks I learn along the way while coding and also post what I am working on to Twitter. I think that those things need to be shared with others (and having a log of those things will also help me remember how I did something!). How many times have you encountered an error or worked through a problem only to encounter it again and say, “I did this before and I don’t remember how to do it now! ARGH!”. Well, I have done that more times than I want to admit so this will be a collective journal that hopefully someone other than just myself will get use out of!
I would love to connect with other tech women and share experiences and learn from each other. I think that education is a social endeavor and the more we can communicate and learn from each other, the faster it will help all of us advance.
I am starting with the 100daysofcode challenge and will be updating this regularly with my progress. If you are doing the same challenge or want to learn more about it, connect with me and we can work through it together! I love accountability partners.