Methods are behaviors of a class. They are responsible for how a class interact with other classes or behave generally. Class is just a container that holds different methods. It is in methods that logic are written and all actions are executed.
Syntax of a java method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
It must have a return type. If it is not meant to return anything, then you declare void as its return type. The example above has not return, thereby it is declared void.
Example of a method with return type
public String getMyLocation() {
return myLocation;
}
return myLocation;
}
The first name or identifier must start with a lowercase. If there are concatenation of more than one name, the first name must be in lowercase and the first letter of the next word must be in upper case.
public String getMyLocation() {
return myLocation;
}
return myLocation;
}
private int classGrades(int maths, int english){
return maths + english;
}
return maths + english;
}
A java method is a collection of statements. Statements are written between two curly brackets {}.
protected void onCreate(Bundle savedInstanceState) {
//statements
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//statements
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Method Overloading
Method Overloading is when a class have two or more methods with the same name but different parameters. The only thing that is different in this methods is the parameters they take.This first method is not taking in any parameter
public String getMyLocation() {
return myLocation;
}
return myLocation;
}
The second method is taking in a parameter
public String getMyLocation(String myLocation) {
return myLocation;
}
return myLocation;
}
In the previous post, we worked on button clicks. I want to show you another way of how to implement button clicks (click listener) in our app.In your activity_main.xml file, add android:onClick="addBtn"
<!--The button that is
responsible for add numbers-->
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="addBtn"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="addBtn"/>
And in your MainActivity.java
Instead of this long process:
addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Anytime the button is clicked 1 is added to the screen numberUpdate = numberUpdate + 1; //Displays the numberUpdate to the screen //if you don't set the text, nothing will be displayed to //the screen defaultText.setText("" + numberUpdate); } });
Just use this
//addBtn is the value of android:onClick
value in the our xml
public void addBtn(View view) {
//Anytime the button is clicked 1 is added to the screen
numberUpdate = numberUpdate + 1;
//Displays the numberUpdate to the screen
//if you don't set the text, nothing will be displayed to
//the screen
defaultText.setText("" + numberUpdate);
}
public void addBtn(View view) {
//Anytime the button is clicked 1 is added to the screen
numberUpdate = numberUpdate + 1;
//Displays the numberUpdate to the screen
//if you don't set the text, nothing will be displayed to
//the screen
defaultText.setText("" + numberUpdate);
}