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

Ready to master Java? Today’s topic is static variables. Static variables extend across the entire run of a program. In this article, you’ll learn how to create static variables in Android Studio with Java.

If you need an introduction to coding and want to learn for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

A static variable is associated with a class rather than a specific object. So let’s create a class! To follow along with this example in Android Studio, go into Project view. Then go to app > java. Right-click on the topmost com.example.zebra.demo. Select New > Java Class.

The tab “Create New Class” will pop up. Let’s name our class “Vehicle”. Follow this format:

public class Vehicle {

}

As you can see, we have created a new class called Vehicle. Next we shall set up some fields. On a line within the curly brackets of the public class, let’s declare a string brand.

private String brand;

Note that we made this field private, meaning that we can access the variable only inside this Vehicle class.

Furthermore, let’s declare a variable for the conversion rate from kilometers to miles, which is 0.621371.

public static final double KILOMETERS_TO_MILES = 0.621371

First off, the name of our variable is KILOMETERS_TO_MILES, and its value is 0.621371. We used several keywords to specify the type of variable:

  • public is used because the static variable has to be used throughout our project.
  • static is used because our conversion rate applies to all the vehicles. The rate is not specific to a certain vehicle, such as a Honda or Toyota.
  • final is used because the conversion rate is a constant that won’t change throughout the project.
  • double is used because our conversion rate has decimal places. A variable of the data type double can contain decimals.

Now we have to create a constructor for our class. Constructors are run whenever objects are created. On a new line, type in:

public Vehicle(){

}

Within the parentheses, set up the parameter String brand. As well, within the curly brackets, say:

this.brand = brand;

This means that our input field brand is equal to the value of the parameter brand.

To make sure we can retrieve brand, create a getter by coding:

public String getBrand(){
return this.brand
}

This is called the getter method because it “gets” the private field. We can’t access the private field String brand otherwise.

Now that we have created the constructor, go to app > java > (topmost) com.example.zebra.demo > MainActivity. Beneath setContentView(R.layout.activity_main);, create a new Vehicle called myVehicle. Give it the value Toyota:

Vehicle myVehicle = new Vehicle("Toyota");

We just created a variable! We also want to display a message on the screen. For this to happen, on a new line, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete the following code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

To specify the message we want to display, edit that code so it looks like this:

Toast.makeText(MainActivity.this, "Brand of my car: " + myVehicle.getBrand() + " Conversion rate between KM and MILES: " + Vehicle.KILOMETERS_TO_MILES, Toast.LENGTH_SHORT).show();

Note the difference between myVehicle.getBrand() and Vehicle. myVehicle is used because getBrand applies to a specific vehicle. When attaching the variable for the conversion rate, Vehicle is used because the conversion rate applies to all vehicles.

Run the emulator and zoom in. You should see the message “Brand of my car: Toyota Conversion rate between KM and MILES: 0.621371”.

If you want to learn even more coding for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Introduction to Strings Java: Android Studio Crash Course (Free Tutorial)

Are you stuck trying to display a message in your program? This is the article for you. Today we’ll be showing you how to use string variables in Java in Android Studio.

If you need an introduction to coding and want to learn for FREE, check out our 30-minute beginners 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 > (topmost) com.example.zebra.demo > MainActivity.

On a line underneath setContentView(R.layout.activity_main);, let’s declare our first string variables. Use the keyword String to do so. Then let’s declare three strings and name them firstName, lastNamename, and name.

String firstName, lastName, name;

Give firstName the value Mammoth and lastName the value Interactive. The following format is used to do so:

String firstName = "Mammoth", lastName = "Interactive", name;

For the value of name, concatenate (combine) the first two strings. Also, because we want to have a space between the words “Mammoth” and “Interactive”, add a space in quotation marks between the two strings in the concatenation. Your line of code should look like so:

String firstName = "Mammoth", lastName = "Interactive", name = firstName + " " + lastName;

To have our string actually appear on the screen, we will need to use the Toast utility. Below your strings, type in the following code:

Toast.makeText().show();

Within the parentheses of this code, we can set some parameters. In the parentheses after Toast.makeText, type in the following:

this, "The string " + name + " has " + name.length() + " characters.", Toast.LENGTH_LONG

Thus the message that shows up will tell us of how many characters our strings is made up. Toast.LENGTH_LONG means the message be displayed for a couple of seconds.

To test the code, run the emulator, and zoom in. You will see the message “The string Mammoth Interactive has 18 characters.”

That is how you use strings in Android Studio! You will now be able to provide messages for the users of your applications. For more FREE tutorials, check out our 30-minute intro course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Introduction to Making a Simple Banner Ad: Photoshop Crash Course (Free Tutorial)

Looking to make an attractive ad? In this tutorial, you’ll learn how to make a simple ad in Photoshop that can be used as banner or something of the like for your marketing needs.

On a side note, if you’d like to learn to code for FREE, we have a 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To follow along in Photoshop, go to File > New to create a new document. You can choose any dimensions you’d like, such as 960 x 640 px. For the purpose of our example, also set the background to Transparent. Press OK to begin.

Firstly, let’s choose the color of our font. Click on the front-most of the two colored squares in the left sidebar. The color of this square is the color of your font.

The “Color Picker (Foreground Color)” tab will appear. In this tab, you can manipulate values to get your desired color. We will use an off-white color by setting the hex code to #fafafa. Press OK to close the Color Picker tab.

Push G or Shift + G. The background will change to the off-white color!

Place a logo, or draw a rectangle as a temporary logo, on the screen:

  • To place an image, go to File > Place, select your file, and hit Enter.
  • To draw a rectangle, in the left sidebar, click on the rectangle above the magnifying glass. The header at the top of your screen will transform to give you options for manipulating your shape. Click and drag to draw a box.

Move your logo/rectangle to the left and center. As you move it, pink lines will appear to help you center it.
advid
Now let’s add some text into our ad. Click on the “T” in the left sidebar. To select a font color, click on the reversible arrow next to the two colored squares in the left sidebar. Then click on the front square to open the “Color Picker (Foreground Color)” tab again. Let’s use an off-black with the hexcode #393939.

Beside your logo, type “MASTER” using your desired font. The font I’m using is Oswald Bold. If you go to Edit > Transform > Scale, you can change the size of your text box by dragging its corner. To scale proportionally, push the Shift/Alt key. Hit Enter to apply the changes.

Below “MASTER”, let’s draw a rectangle of the same color. On top of the shape, add a new text layer saying “APPS” in our off-white color. Because “APPS” is the same color as our background, you can scale it so that it bleeds into the background. Your screen should look something like this:
advid3
Finally, in our off-black color, add the text “MAKE MONEY” underneath “APPS”. Line up the new text so that it touches the bottom of the off-black rectangle.

Likewise, move “MASTER” down so that it touches and lines up with the rectangle. Note: to select an object, select its layer in the Layers tab in the right-hand sidebar. Press Shift to select multiple layers.
advid4
There we have it! You are now able to create a stylish, bold ad in Photoshop. If this tutorial helped you make your ad, we’d love to see it! Share your images with us on Twitter or in the comments below. For more tutorials, check out this FREE 30-minute beginners course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

 

Introduction to Nested If Statements: Android Studio Crash Course (Free Tutorial)

Is your code cluttered? You know what they say: tidy code, tidy mind! With this article, you’ll learn how to simplify your code. Specifically, we’re going to compare two ways of checking the value of an integer: using several if statements vs using one if statement.

If you need an introduction to coding and want to learn for FREE, check out our 30-minute beginners 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 > (topmost) com.example.zebra.demo > MainActivity.

Firstly we have to set up some variables. On a line underneath setContentView(R.layout.activity_main);, declare an integer number, and set it equal to 98.

int number = 98;

Below that, declare the string message, and set it equal to "This number is ". This is part of the message that we want to display on the screen.

String message = "This number is ";

Next create an if statement:

if(){

}

Within this if statement, we’ll be checking if our integer is greater than 95. In the parentheses, write number > 95.

As well, we need to code the other part of the message that will be displayed on the screen if our number is greater than 95. Within the curly brackets of the if statement, let’s add the following:

message += " greater than 95";

Still within the if statement, let’s build another if statement. We’ll use this nested if statement to check if our integer is less than 100.

if(number < 100){

}

If our integer is less than 100, let’s have the message “This number is greater than 95 and lower than 100.” appear. For this to happen, add the following line within the curly brackets of your second if statement:

message += " less than 100.";

To have our message actually appear on the screen, we will need to use the Toast utility. Below your if statements, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete this code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

Within the parentheses of this code are the parameters. Change the quotation marks to message.

If you run the emulator and zoom in, you will see the text “This number is greater than 95 and less than 100.” appear on the screen, exactly as expected.

But there’s a faster way of doing this operation without using the nested if statement. Cut number < 100 from your nested if statement, and place it into the parentheses of the first if statement. Use the AND operator (&&) to separate the two parameters in the parentheses:

if(number > 95 && number < 100){
message += " greater than 95";
}

Also add and is less than 100. after greater than 95. Now you can delete the nested if statement. As such, with only one if statement, we can check if our integer is between 95 and 100!

Run the emulator again to see the same message appear on the screen. Although most of the time you will need if statements, you were able to write this program in fewer lines of code that are easier to understand.

For more FREE tutorials, check out our 30-minute intro course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Introduction to Making a Photo Black and White: Photoshop Crash Course (Free Tutorial)

You may think there’s only one way a photo can look in black and white. But no! You can customize your images to look the way you want them to in grayscale using Photoshop.

On a side note, if you want to learn to code for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To begin, open up an image you’d like to edit. There are several ways to change it into black and white:

1. Go to Image > Mode > Grayscale. On the pop-up that appears, click “Discard”. This will convert the image to black and white by discarding the color. To undo that action, push Control/Command + Z.

2. Now click on the Channels tab in the right-hand sidebar. By clicking on the Red, Green, and Blue bars individually, you will get different variations of your image in black and white!

Note: to revert your image to how it looked originally, go to the Layers tab in the right-hand sidebar, and delete the layers that were added as you modified your image.

3. Next let’s look at the Adjustments tab in that sidebar. Alternatively, go to Window > Adjustments. This tab has even more options for changing the way your image looks. For instance, Hue/Saturation refers to the amount of color in your image. If you move the saturation bar all the way to the left, your image will become black and white.

4. In the Adjustments tab, you can also select the Black and White icon to convert your image. Notably, if you check the Tint box in the Properties tab that opens, your image will have a sepia tone.

As you can tell, there are numerous ways to modify your image to black and white in Photoshop. If this tutorial helped you edit a photo, we’d love to see it! Share your images with us on Twitter or in the comments below.

For more tutorials, check out our FREE 30-minute beginners course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes