Tuesday, 15 August 2017

Combining Strings Together Subtitles

When dealing with strings in Java, an important concept to understand is string concatenation. Now that's a really big word, but it just means we're joining character strings together end to end. If this is a string and this is a string, you can combine them by concatenating them to make an even longer string. To concatenate these strings together, we use the plus operator. This is the same addition symbol that we know from math. Just like you can add numbers together, you can concatenate strings together. Let's look at an example. Say I have three different strings, one string literal says I need, another string literal says 2 cups of coffee, and another string literal says on Monday. I can use the plus symbol to concatenate all these strings together. That forms a ginormous string that says I need2 cups of coffeeon Monday. Whenever I see something like this, I imagine the plus symbols are gone, and I imagine the quotes are gone, and I just imagine literally squishing all of these things together. And when I say squished, we're really squishing them together. There's even no extra space in between this string and this string. If you want to add a space here, you would have to explicitly add a space in this string literal at the end of it, or you add a space at the beginning of this string literal. Same with coffeeon Monday. I want a space here, so I'd have to either add it at the end of this string or the beginning of this string. I added a space here and a space here, so when I concatenate all of this together, I squish them together, and the sentence comes out correct like this. There's a space here and a space here. Adding spaces in the right place is a little bit tricky because you have the quotation marks everywhere and the plus symbols, and there's even spaces around the plus symbol. But these spaces around the plus symbol don't contribute to the overall display string. The space must be inside the double quotes. Here's an example of string concatenation in our app. I'm going to change the text so that it says "Amount due " + "$10". I'm concatenating this string literal with this string literal. When I run it on my device, and then I hit the order button, then I see Amount Due $10. You can also concatenate strings with integers like I have here. Before, the ten was in quotes so that was a string representation of the number ten. But here I just have 100 without quotes, so this is the integer value for 100. If I concatenate a string with an integer, then it immediately turns this whole thing into a string. If I hit the Order button, then I see $100 showing up on the screen. In a moment, I'll have you play around with string concatenation to try different values. You could get compile errors, so be careful of those. If I forget a closing quote, I could get an error. In a moment, I'll have you play around with string concatenation and try different values. According to Android code style guidelines, we should have a space before and after each operator. And this string concatenation operator counts as an operator. Now it's your turn to practice in your app. Experiment with combining different strings using the plus operator. You can also combine it with integer literal values as well. Once you feel comfortable with string concatenation, I want you to answer these questions.

Thursday, 3 August 2017

Nested View Group

When discussing how to position views in the last quiz, you may have mentioned using a relative layout. I know we did. But I want to introduce you to another way that you can build the layout for our app that would be more ideal, using nested viewgroups.

Nested viewgroups means putting viewgroups inside other viewgroups. That way you can build more interesting and complex layouts like these. Let's see how you would build up one of these layouts. Say, for example, you have a vertical linear layout with three views, an image view and two text views. What if you wanted to overlay some text on top of this image? Well, with a linear layout, you can't overlap views. But with a relative layout, you can, so we can replace this child with a relative layout instead. Then, we can put the image view inside this relative layout. This image view is now aligned to each edge of the parent relative layout. So, there you see an example of a relative layout that's contained within a larger linear layout. You can have many combinations of view groups within other view groups. For instance, you can have a relative layout as a root view for an app. That can contain a couple of views like an image view and two text views. You can also add a linear layout as a child of this broader relative layout. This linear layout itself can contain other children like two image views. You can nest as many view groups inside other view groups as you want. However, be careful because the more nesting that you do, the more expensive it will be for your app to lay out on the screen. The relative layout, in particular, is a very flexible layout but it can be very complex to calculate the position of each of these views relative to others. So it's recommended to not have too many layers of nesting within a relative layout. I pulled some examples from real life Google apps to show you some cases where it would be useful to use nested view groups. The Google Now app shows you cards of relevant information based on your current situation, like the current time or current location. If you like a certain team like the Red Sox, good choice, then it can show you the score for the latest game. To build out the layout for this card, you can imagine using a vertical linear layout because the information is displayed as rows. However, the first item in this vertical linear layout is not just a single text view, it's made up of multiple views. In fact, you would build this first item as a horizontal linear layout, made up of an image view and two text views. Same for the second item in this vertical linear layout. You would build it with a horizontal linear layout containing an image view and two text views.

 Another card in the Google Now app shows relevant stocks that you may be interested in. I wish the Google stock was still at that price. Anyways, you can also build out this layout as a vertical linear layout, because you can imagine the rows here. Except these middle entries aren't just a single text view, they're made up of multiple text views. For this line item relating to the Google stock price, you could use a horizontal linear layout and fill it with four text views. Same for the other stock prices as well. These horizontal linear layouts are children of this broader vertical linear layout. In the Google Play music app, you can, of course, listen to music.

 While this looks like a complicated layout to build, you can actually break it down into smaller view groups that you recognize. This pattern feels like a vertical linear layout with three different children. This first child can be built using a relative layout, because the views are relative to each other. The second child can also be built using a relative layout, because the views are overlapping each other. And this third child here looks like a horizontal linear layout made up of five different buttons. Again, we can use nested view groups to build out this screen. This is just one way to build out the layout. You can build it in many other ways as well. In the Google Maps app, earlier we saw that there are detailed pages for individual locations, like restaurants.

 You can't build out this screen with just a single linear layout or a single relative layout. You need to nest a bunch of view groups together. On a broad scale, we can see that the information is almost organized into rows. So, we can use a vertical linear layout as the parent view. One child could be this whole layout here. Another child could be this row of buttons. Another child would be this description and so on. But within each child is not just a single view. We have multiple views. This child in the broader linear layout is made up of three individual views. So you can use a horizontal linear layout with equal weights to each view so that they get spread out evenly. This child in the broader vertical linear layout can also be made up of a horizontal linear layout with an image view and a text view. If you keep scrolling this page, you see more information on the restaurant. Again, I can imagine the individual rows of information here. Using a vertical linear layout for all this information still works, and, again, some rows are more complex than just a single text view. This row shows a bunch of available times where you can make a reservation, and it's made up of three different buttons that you can arrange using a horizontal linear layout. This row of pictures can be built using a horizontal linear layout as well. And actually, within this horizontal linear layout, this first child is not just a single image view. It's an image view with a text view. So you could use a relative layout to build out this first child. So you could see how there can be view groups, within view groups, within view groups. There's actually more view groups beyond just relative layout, and linear layout, there's also frame layout and grid layout. We're not going to have time to cover those in this course, but I definitely recommend that you go out and learn about them on your own because sometimes those would be better than just using a relative or a linear layout.

Saturday, 29 July 2017

Just Java Final Code With No Errors With No U.I

JUST JAVA
JAVA CODE

 package com.example.android.justjava;

/** * IMPORTANT: Add your package below. Package name can be found in the project's AndroidManifest.xml file. * This is the package name our example uses: * <p>
 * package com.example.android.justjava; */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/** * This app displays an order form to order coffee. */public class MainActivity extends AppCompatActivity {
    int quantity = 0;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**     * This method is called when the plus button is clicked.     */    public void increment(View view) {

            quantity = quantity + 1;
            display(quantity);

/** * This method is called when the minus button is clicked. */    }
    public void decrement(View view) {

            quantity = quantity - 1;
            display(quantity);



    }
    public void submitOrder(View view) {

        display(quantity);
        displayPrice(quantity * 5);
    }

    /**     * This method displays the given price on the screen.     */    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }

    /**     * This method displays the given quantity value on the screen.     */    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }
}


XML CODE

<?xml version="2.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="16dp"        android:text="quantity"        android:textAllCaps="true"/>

    <Button        android:id="@+id/button3"        android:layout_width="48dp"        android:layout_height="48dp"        android:onClick="increment"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:text="+" />

    <TextView        android:id="@+id/quantity_text_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginLeft="16dp"        android:text="0"        android:textColor="#000000"        android:textSize="16sp"/>

    <Button        android:id="@+id/button4"        android:layout_width="48dp"        android:layout_height="48dp"        android:onClick="decrement"        android:layout_marginLeft="16dp"        android:text="-" />

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginStart="16dp"        android:layout_marginTop="16dp"        android:text="price"        android:textAllCaps="true"/>
    <TextView        android:id="@+id/price_text_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="16dp"        android:layout_marginTop="16dp"        android:text="$0"        android:textColor="#000000"        android:textSize="16sp"/>

    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="16dp"        android:layout_marginTop="16dp"        android:onClick="submitOrder"        android:text="order"        android:textAllCaps="true"/>

</LinearLayout>

My Trial With Coffee Ordering App

<?xml version="2.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"
    tools:context=".MainActivity">

    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingRight="5dp" >

        <ImageView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:src="@drawable/images"            android:scaleType="centerCrop"            andr            android:layout_gravity="top"            />
      <android.support.v7.widget.LinearLayoutCompat          android:layout_width="match_parent"          android:layout_height="match_parent"          android:orientation="vertical">
          <TextView              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_margin="16dp"              android:text="quantity"              android:textAllCaps="true"/>

          <Button              android:id="@+id/button3"              android:layout_width="48dp"              android:layout_height="48dp"              android:onClick="increment"              android:layout_marginBottom="16dp"              android:layout_marginLeft="16dp"              android:text="+" />

          <TextView              android:id="@+id/quantity_text_view"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_marginBottom="16dp"              android:layout_marginLeft="16dp"              android:text="0"              android:textColor="#000000"              android:textSize="16sp"/>

          <Button              android:id="@+id/button4"              android:layout_width="48dp"              android:layout_height="48dp"              android:onClick="decrement"              android:layout_marginLeft="16dp"              android:text="-" />

          <TextView              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_marginStart="16dp"              android:layout_marginTop="16dp"              android:text="price"              android:textAllCaps="true"/>
          <TextView              android:id="@+id/price_text_view"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_marginLeft="16dp"              android:layout_marginTop="16dp"              android:text="$0"              android:textColor="#000000"              android:textSize="16sp"/>

          <Button              android:id="@+id/button"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_marginLeft="16dp"              android:layout_marginTop="16dp"              android:onClick="submitOrder"              android:text="order"              android:textAllCaps="true"/>

      </android.support.v7.widget.LinearLayoutCompat>
        

    </RelativeLayout>
    
</LinearLayout>


JAVA CODE

 package com.example.android.justjava;

/** * IMPORTANT: Add your package below. Package name can be found in the project's AndroidManifest.xml file. * This is the package name our example uses: * <p>
 * package com.example.android.justjava; */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/** * This app displays an order form to order coffee. */public class MainActivity extends AppCompatActivity {

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

    /**     * This method is called when the plus button is clicked.     */    public void increment(View view) {
        int quantity = 2;
            quantity = quantity + 1;
            display(quantity);

/** * This method is called when the minus button is clicked. */    }
    public void decrement(View view) {
        int quantity = 2;
            quantity = quantity - 1;
            display(quantity);



    }
    public void submitOrder(View view) {
        int quantity = 5;
        display(quantity);
        displayPrice(quantity * 5);
    }

    /**     * This method displays the given price on the screen.     */    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }

    /**     * This method displays the given quantity value on the screen.     */    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }
}

Wednesday, 26 July 2017

debugger sentence formation

We&#39;re making really good progress so
far.
As we continue to build
more challenging things for
our app, we&#39;re going to have to
improve our debugging skills.
In this video,
we&#39;re going to learn about how to
use a tool called the debugger.
We&#39;ll access the debugger
using Android Studio.
And the debugger is meant to help us
identify and fix errors in our code.
The great thing about the debugger is
that we can pause the app at a certain
point in time, and then we can inspect
the whole state of the app more closely.
Normally, when the app is
running on our device,
all the code gets executed very rapidly.
Within milliseconds, it can respond
to button clicks, update the screen,
calculate the price, and etc.
But with the debugger, we can pause at
a specific line of code in our app,
then we can step through our code line
by line as quickly or slowly as we want.
If you want to learn more techniques on
how to debug your app in Android Studio,
just search for
debugging Android Studio.
This first link is good.
It&#39;s a developer.android.com page, and
it has a bunch of techniques
on how to debug your app.
Together, we&#39;re going to learn
about how to work with breakpoints.
After this course,
you can always come back to this
page to refresh your memory.
In this exercise I want to show you
that your Android device does run
each line of code one at a time
from top to bottom within a method.
We&#39;ll also verify that clicking on the
plus button does trigger the increment
method, and clicking on the minus button
does trigger the decrement method.
First add something
known as a breakpoint.
A breakpoint marks
a specific line of code
where the debugger should
pause when it reaches here.
The red circular dot indicates
that the breakpoint is
on this line on 25 of
our main activity file.
You can add breakpoints to as many
places as you want in your code.
But for now,
I&#39;m just going to add one on the first
line of the increment method.
Then I can hit this bug icon
to run the app in debug mode.
The app will only pause on these
breakpoints when the app is running
in debug mode.
If you run the app normally
with this green play button,
then it won&#39;t pause
at these breakpoints.
For a brief moment, on the device,
you may have seen a dialog that says,
waiting for debugger.
There&#39;s also a button to force close
the app, but don&#39;t click on that.
Just wait for the debugger to attach and
the message will go away soon.
Then the app starts like normal and
you can interact with it.
Now, in debug mode, this window will pop
up and show the status of the debugger.
If you don&#39;t see it, you can click
on this tab down here called debug.
If I hit this plus button here, I expect
the increment method will be triggered
and that the app will
pause at this breakpoint.
Let&#39;s see what happens.
Cool.
It stops there.
In the debug pane,
we see a list of variables.
This reflects the current state
of the app where it was paused.
We don&#39;t see our quantity variable yet,
because it hasn&#39;t finished
executing this line of code yet.
There&#39;s a bunch of options on
how to proceed with debugging.
But I&#39;m going to click on this
option that says step over.
This will step over the current line
of code to the next line of code.
Now the app is currently
paused at line 26.
We finished executing line 25,
so that&#39;s why we see the quantity
variable showing up down here.
This list of variables also shows
the current value of each variable.
We can also verify that the quantity
variable has a value of 3,
which we assigned up here.
We know that the app is
currently paused at this point,
because normally when
you hit the plus button,
it would automatically
update the quantity to be 3.
But since the quantity is still a 2,
we know that it hasn&#39;t finished
executing the increment method yet.
We can click step over
to go to the next line.
At this point, the method is done.
And we&#39;re not really interested in how
it actually updates the screen, so
let&#39;s just hit resume program.
All we care about is that it
actually does update the screen.
If you&#39;re interested, you could have
clicked on these other options to look
at more details of how it
actually updates the screen.
At this point,
the app is running normally,
but the debugger is still attached.
So if I hit the plus button again,
it will stop at this breakpoint.
If I want to detach the debugger so that
it doesn&#39;t always stop on this line when
I hit the plus button, I can just
click on this red stop button.
Now whenever I hit the plus button,
it responds immediately and
updates the value to 3.
Now I want you to try
it on your computer.
Go ahead and add a breakpoint to
the increment and decrement methods.
Then run the app in debug mode,
and then on the device,
try to trigger each
of these breakpoints.
Then step through each line of code,
making sure that the quantity
variable shows up with the right
value in the variables list.
You can click on this play button
to resume execution of the app so
that it runs as normal.
Or you can hit the stop button
to detach the debugger.


Friday, 14 July 2017

ORDER COFFEE INITIAL HALF CODE

<?xml version="2.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">

    <TextView        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_margin="16dp"        
android:text="quantity"        
android:textAllCaps="true"/>

    <Button        
android:id="@+id/button3"        
android:layout_width="48dp"        
android:layout_height="48dp"        
android:onClick="increment"        
android:layout_marginBottom="16dp"        
android:layout_marginLeft="16dp"        
android:text="+" />

    <TextView        
android:id="@+id/quantity_text_view"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_marginBottom="16dp"        
android:layout_marginLeft="16dp"        
android:text="0"        
android:textColor="#000000"        
android:textSize="16sp"/>

    <Button        
android:id="@+id/button4"        
android:layout_width="48dp"        
android:layout_height="48dp"        
android:onClick="decrement"        
android:layout_marginLeft="16dp"        
android:text="-" />

    <TextView        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_marginStart="16dp"        
android:layout_marginTop="16dp"        
android:text="price"        
android:textAllCaps="true"/>
    <TextView        
android:id="@+id/price_text_view"        
android:layout_width="wrap_content"       
android:layout_height="wrap_content"        
android:layout_marginLeft="16dp"        
android:layout_marginTop="16dp"        
android:text="$0"        
android:textColor="#000000"        
android:textSize="16sp"/>

    <Button        
android:id="@+id/button"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_marginLeft="16dp"        
android:layout_marginTop="16dp"        
android:onClick="submitOrder"        
android:text="order"        
android:textAllCaps="true"/>
</LinearLayout>



JAVA CODE FOR COFFEE ORDER

package com.example.android.justjava;

/** * IMPORTANT: Add your package below. Package name can be found in the project's AndroidManifest.xml file. * This is the package name our example uses: * <p>
 * package com.example.android.justjava; */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/** * This app displays an order form to order coffee. */public class MainActivity extends AppCompatActivity {

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

    /**     * This method is called when the order button is clicked.     */    public void increment(View view) {
        int quantity = 3;
        display(quantity);
        displayPrice(quantity * 5);

    }
    public void decrement(View view) {
        int quantity = 1;
        display(quantity);
        displayPrice(quantity * 5);


    }
    public void submitOrder(View view) {
        int quantity = 2;
        display(quantity);
        displayPrice(quantity * 5);
    }

    /**     * This method displays the given price on the screen.     */    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }

    /**     * This method displays the given quantity value on the screen.     */    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }
}

SCREEN SHOT

Thursday, 13 July 2017

Code For The Happy Birthday App Final Project Lesson 3


<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">


    <ImageView        android:src="@drawable/happybirthday"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        />
    <TextView        android:text="Happy B'day Bharath"        android:fontFamily="sans-serif-light"        android:layout_width="wrap_content"        android:textColor="#ffffff"
        android:layout_margin="20dp"        android:layout_height="wrap_content"        android:textSize="36sp"/>

    <TextView        android:text="From, Sambhav!"        android:fontFamily="sans-serif-light"        android:layout_width="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:layout_margin="20dp"        android:textSize="36sp"        android:textColor="#ffffff"        android:layout_height="wrap_content" />

</RelativeLayout>