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.