Introduction to Boolean Variables: Android Studio Crash Course (Free Tutorial)

Ever heard of George Boole? He was a pioneer in the study of logic, and Boolean Variables are named after him. In this article, we will learn all about Boolean Variables, which are used to evaluate the logical condition. You can use the Boolean Function to determine if an expression or variable is true or false.

If you are a beginner and want to learn to code for FREE, check out our 30-minute introductory course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (the topmost folder) com.example.zebra.demo > MainActivity. You should see the following code on your screen:

public class MainActivity extends AppCompatActivity {

@Override
protected voice onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 

}
}

First let’s declare a Boolean Variable and name it isBatteryCharging. Also, let’s set the variable equal to true. Note that you can only assign the values true or false to Boolean Variables. To do so, add the following code beneath setContentView(R.layout.activity_main);.

Boolean isBatteryCharging = false;

The naming convention for Boolean Variables a question that can be answered with true or false, such as “Is volume turned on?” One equals sign means that the value on the right of the sign will be assigned to the variable on the left.

Let’s use a more practical example to practice using If/Else Statements. Type in the following code:

if(isBatteryCharging == true){

}

Note that adding == true is optional. When there are two equals signs beside each other, the two variables on both sides of the signs will be compared. The value stored in isBatteryCharging will be compared to the Boolean Constant true. Right now, it’s going to be evaluated as “false” because we set isBatteryCharging equal to false.

Therefore, we will add an else statement:

else{

}

Right now, your screen should look like the following:

public class MainActivity extends AppCompatActivity {

@Override
protected voice onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 

Boolean isBatteryCharging = false;

if(isBatteryCharging == true){

}


else{

}

}
}

This code means that if the battery is charging, a certain code will be executed. If the battery is not charging, a different code will be executed.

Next, we need to set the code that will be displayed if the battery is charging. There will be a message displayed on the screen. To code this message, we will use the Toast utility. Beneath if(isBatteryCharging){, add:

Toast.makeText();

We need to set some parameters within the parentheses of this code. Note that these parameters are separated by commas:

1. In order to display the message on the main screen on the application, add: this.

2. The message we want to display is: "Battery is charging!"

3. We have to set up the length of the message (how long it’ll be displayed on the screen). Use the predefined variable Toast.LENGTH_LONG.

After the parentheses, add the built-in function .show(); so that the message will appear on the screen immediately.

Now copy this code and paste it underneath else{. Change the message so that it says “Battery is NOT charging!” and “Plug in the charger!”

The code beneath your Else Statement should look like so:

Toast.makeText(this, "Battery is NOT charging!" + 
"Plug in the charger!",
Toast.LENGTH_LONG).show();

To test this example, run the emulator. You will see “Battery is NOT charging!Plug in the charger!” displayed on the screen. This is because we set our Boolean Variable equal to false. If you change isBatteryCharging to be equal to true, the message “Battery is charging!” will appear on the screen because the condition will be evaluated to true.

There you have it: Boolean Variables! To learn even more about coding, sign up for our FREE 30-minute introduction course: training.mammothinteractive.com/p/learn-to-code-in-30-minutes.

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com