Before creating a real app, you may want to test the development environment on a sample app to see how everything works. As a common practice, a "Hello World" app will be created.
TIP: Remember these values (e.g., MainActivity, activity_main, and menu_main) as you will see how they are connected in Java code and the XML files later.
A button will be added and given an interactive behavior.
public class MainActivity extends AppCompatActivity { private Button myButton;
CODE EXPLANATION: This will create a variable to reference the button in the activity_main.xml file.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButton = findViewById(R.id.myButton);
CODE EXPLANATION: This line of code actually ASSOCIATE the button within the activity_main.xml file by finding the view (Button) by its ID (Remember, it was given an ID of myButton in the activity_main.xml file earlier). The findViewById is similiar to findElementByID in JavaScript. It is important to note that the code is placed AFTER the setContentView() method so that the view is set BEFORE it is referred to in code; otherwise, you would get an error.
myButton = (Button) findViewById(R.id.myButton);
CODE EXPLANATION: This will cast the value to a Button instead of a normal View. You could just as easy had typed this in initially when the code was written. Because every visible component is a View, you have to be more specific as to the TYPE of view it is—in this case, a Button view. This process is called CASTING where you "cast" an "object" to a different or more specific type.
myButton = (Button) findViewById(R.id.myButton); View.OnClickListener myListener = new View.OnClickListener() { @Override public void onClick(View v) {
} }
CODE EXPLANATION: The first line of code create an event listener object with a method called onClick that get executed when the button is clicked. However, there is no code in it to execute. This will be resolved in the next step.
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You clicked on the button", Toast.LENGTH_SHORT).show(); }
};
CODE EXPLANATION: The Toast() object allows you to show a message for a brief period of time on the user screen. The makeText() method of the Toast object allows you to set a text message ("You clicked on the button." for a given context (MainActivity.this). The show() method of the Toast object is responsible for "showing" the message once the Toast is made. While either way is correct, you may see is sometimes placed on a separate line like this:
Toast.makeText(MainActivity.this, "You clicked on the button", Toast.LENGTH_SHORT)
Toast.show();
TIME SAVING TIP: When you typed Toast and press the Tab key, you were using what is called a Live Template. There are a host of Live Teplates that comes with Android Studio that can save you time by automatically expanding a pharse like Toast to a complete code block after you click the Tab button. You can also create your own Live Templates. Learning some of the vital few Live Templates can save you a ton of time AND avoid you from making "stupid" mistakes when coding. See Live Templates for more details.
View.OnClickListener myListener = new View.OnClickListener() { @Override
public void onClick(View v) { Toast.makeText(MainActivity.this, "You cliced on the button", Toast.LENGTH_SHORT).show(); }
};
myButton.setOnClickListener(myListener);
CODE EXPLANATION: Now that the button is referenced and the event listener object is created you have to TIE the two together by ASSIGNING the setOnClickListener() method to the button and pass it the event listener object (myListener).