Home   Courses   Blog   About

What are If statements (conditions)

What is an If statement and how to choose a random color?


When programming we have to do alot of decisions and for that we use if statements. In programming an if statement is a way of telling a program when to run your code. If your condition is true it will run your code, if it is not true it will jump over it.

Let me show you an example:
If (1 == 1) {
   ... some code ...
}


In this example the code in brackets will run, because 1 equals 1. In java we use == to compare numbers, booleans....

If (1 == 2) {
   ... some code ...
}


This code will not run because 1 does NOT equal 2.

In real life if statements will get more complex. And they will look more like this:
If (number == 1) {
   ... code1 ...
} else if (number == 2) {
   ... code2 ...
} else {
   ... code3 ...
}


This means if the number is equal to 1 “code 1” will be executed and if the number is equal to 2, “code 2” will be executed, but what happens if the number is something else like 5, 1000, 4, 67,... in that case it will execute “code 3”

And that is it. With this statement we do most of our decisions in programming, therefore it is important that you understand it.

Now let’s make an application that will show you how if statements work in practice.

The first step is to create a new project. To do that you can follow the steps in our blog:

  1. Open layout editor and add a TextView. Read more about Layout Editor here.
    • In an empty activity there is usualy a “Hello world” TextView already on the activity, if not add it yourself.
    • Once you have it change the text size to at least 36sp, we want to have a large text.
    • Change text of the TextView to 0
    • Position your TextView in the center of the screen
    • The name should be ID=textView
  2. Add a button on screen as well
    • Position your button under the TextView
    • Change the text to “Draw a number”
    • Name it, ID=buttonDraw
  3. Also name your Layout, select the layout by clicking the background and name it ID=layout, you will have to find it since we will be changing the color of it.
  4. Now your screen should look something like this


Main Activity, CrazyColors



It's time for code!



6. In MainActivity.java declare variables for all 3 controls that we have named. Put this code just after the declaration of the MainActivity class. (if you have RelativeLayout, the last line will start with RelativeLayout)

Declare variables

7. In order to work with our controls we have to find it, and we will do that after setContentView(R.layout.activity_main); in onCreate method

Find controls

8. Add an OnClickListener for our button with code: Inside the onClick event we will add a code that changes the color of the screen (layout) and shows a random number.

OnClickListener

9. How do we get a random number?
• Java has a class called Random that will help us get random numbers. We will get the numbers from 1 to 10 with this code. And we will store it in the variable named randomNumber

Random number

10. Now that we have a number we will show it on the screen using our TextView
• String.valueOf(...) is a way of changing a number to a string type

Set text to textView
• setText will change the text of our TextView
• You can already run your app and see what happens. If you have done it right it should already be a fun example how to draw a number. So run it, press a button a few times and you should see random numbers between 1 and 10.


11. Since I have called this example CrazyColors we are missing the Crazy and Colors part. For that we will use an if statement.
• If a random number will be 1 we will change the layout background color to one color and if it is 2 we will change it to different color.
• We will set a color with hex string, thats why we need to parse a string to color with the code Color.parseColor("#...")
• We will change the background of the layout with setBackgroundColor
• And the code for this should look something like this

If else statement

• Go ahead and try it, but you will have to click a few times to get a random number that equals 1 or 2, otherwise there will be no color for your layout.
• For practice go online and pick colors and choose for all 10 possibilities. Google “color picker” . You could also change a color of the text for our textView, research and do this for fun.

Color picker

12. The code for the listener is now like this

Final code for click listener