In this video, let's create a simple android application, which displays predefined quotes,
This is what we are going to build.
Open android studio.
Let the name of the application be 'Quote'.
Choose default settings for a quick set up.
Once the build is completed, let's open XML file and create a simple user interface.
Change the layout to 'RelativeLayout'.
It's one of the simplest layout.
We will remove the textview and add a button.
Let the button text be 'Next Quote'.
In relative layout, we can place the element in the center using 'layout_centerInParent'
Next, we will create a textview, where the quote will be displayed.
If we use the 'layout_centerInParent' attribute again, it will overlap the button.
So, we will use the 'layout_above' attribute to place it above the button.
The ID of the button should be specified as its value.
For that, let's add a new ID to the button.
Use this ID as the value for 'layout_above' attribute.
For a better alignment, we can set the 'layout_centerHorizontal' to true.
Also, we can give an ID for the textview, for later use.
Since the text looks small, we can use the 'textAppearance' attribute to make it little
For this application, we need few quotes to be stored in the application.
In android, strings are stored in the file 'strings.xml', which is under resource > value.
A string is defined inside this file like this.
Give a name to be used as a reference.
Let it be 'quote1'.
And between opening and closing of the 'string' tag, write the text.
We will write a quote and at the end, the name of person who said that.
In a similar way, let's create few more quotes in 'strings.xml'.
Now let's write logic for this application.
Open the java file and let's define a new function 'displayQuote'.
Since we are going to display quotes randomly, we need a random number generator.
For that, declare a 'Random' class variable named 'random' at the top of the class, using
the 'new' keyword.
To store the generated random number, use a integer variable inside the class.
Now generate the random number using 'nextInt' function of 'random'.
We have 10 quotes to displayed randomly.
so we will generate random number between 1 and 10.
To achieve that, pass parameter to the function like this, and add the result with minimum
This generated a random number everytime, between 1 and 10.
Just use the switch statement in the function to select the random quote from 'strings.xml',
using the random number passed as parameter.
In order to store the random quote, let's declare a empty string before the switch block.
Now, with respect to the number between 1 and 10, we will assign the quote to the string
Use the 'getString' function.
The parameter for this function is the resource ID of the string.
We can specify it as R.string.quote name.
R stands for 'Resource'.
Similarly, assign the respective quotes to all possible numbers.
The quote should be displayed in the TextView in user interface.
For setting the quote to TextView, we should have reference to the TextView.
We use the id of the textview defined earlier, for referencing it.
Go to java file and declare a TextView at the top if the class.
Inside the 'onCreate' function, use the 'findViewById' function to link the textview with java code.
Pass the id of textview as parameter to this function.
Now, after the switch statement, we will set the selected quote to the textview, using
the function 'setText', like this.
The quote should be displayed when the button is clicked.
For that, we should handle the button click.
Do the same procedures we did for textview.
For detailed explanation about button click, and ways of handling it, watch the previous
video in my channel playlist.
We will handle the button click with a click listener.
Use the 'setOnClickListener' function.
In the generated overrided function, we will call the 'displayQuote' function.
So everytime the button is clicked, the quote is displayed.
Run the application and see how it works.
As you can see the the quote and the persons name is displayed together, and also there
is no enough spacing around the text.
Let's make it beautiful.
Got to 'strings.xml' file and add the new line character '\n' between quote and persons
Add a '-' in front of the name too.
For a better spacing, add padding to the textview.
let it be 20dp.
Run the application again and see how it looks.
One more thing we can do is, display the quote first time without the user clicking the button.
For that, go to java file, and inside the 'onCreate' function, call the 'displayQuote'
It will display a random quote once when the application is opened, without the button
Thanks for watching, see you in the next video.