This post is still a work-in-progress.

Table Of Contents

  • Google Maps
  • Common Intent
  • Create Menu on Action Bar
  • Working with strings.xml
  • Create Scroll List
  • Create a URI
  • UI
  • Data Binding

UI

  • FrameLayout: Used to display only one item
  • RelativeLayout: Enables you to decide the relation between the different views. i.e, The textbox should be left to the ImageView…
  • ConstraintLayout: uses constraints to place views – efficient instead of using recursive layouts. notice the layout_constraintLeft_...

 

Data Binding

Enable Data Binding in your application

in the module build.gradle file, under the Android tag, enable the binding as follows:

android {
    ...
    dataBinding.enabled = true;
}

Use <layout> as the root element of the layout activity

in our example, it is main_activity.xml. Don’t forget to move the xmlns attributes to the root element:

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

Create a data binding instance called mBinding of type ActivityMainBinding at​​​  MainActivity.java (this could be any java file)

ActivityMainBinding mBinding;

Set the Content View using DataBindingUtil to the activity_main layout

DataBindUtil.setContentView replaces our normal call of setContent view.
DataBindingUtil also created our ActivityMainBinding that we will eventually use to
display all of our data.

 mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

Load a BoardingPassInfo object with fake data using FakeDataUtils

BoardingPassInfo fakeBoardingInfo = FakeDataUtils.generateFakeBoardingPassInfo();

Call displayBoardingPassInfo and pass the fake BoardingInfo instance

displayBoardingPassInfo(fakeBoardingInfo);
    private void displayBoardingPassInfo(BoardingPassInfo info) {

        // COMPLETED (6) Use mBinding to set the Text in all the textViews using the data in info
        mBinding.textViewPassengerName.setText(info.passengerName);
        mBinding.textViewOriginAirport.setText(info.originCode);
        mBinding.textViewFlightCode.setText(info.flightCode);
        mBinding.textViewDestinationAirport.setText(info.destCode);

Activity LifeCycle

lifecycle

Google Maps

Ref here

Common Intents

Common intent is used to share data with other apps via a url.

private void showMap(Uri geoLocation) {
    /*
     * Again, we create an Intent with the action, ACTION_VIEW because we want to VIEW the
     * contents of this Uri.
     */
    Intent intent = new Intent(Intent.ACTION_VIEW);

    /*
     * Using setData to set the Uri of this Intent has the exact same affect as passing it in
     * the Intent's constructor. This is simply an alternate way of doing this.
     */
    intent.setData(geoLocation);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

Ref here

 

Create Menu on Action Bar

1. Right click the resource directory(res) -> New -> Android Resource Directory

Capture

2. In the resource drop list, select menu

Capture2

3. Right-click the generated menu folder under res, and then New->Menu Resource File

4. Fill Menu with items

<item
    android:id="@+id/action_search"
    android:orderInCategory="1"
    app:showAsAction="ifRoom"
    android:title="@string/search"/>

5. Override menu methods: In the activity java file, override the next methods:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// COMPLETED (9) Within onCreateOptionsMenu, use getMenuInflater().inflate to inflate the menu
getMenuInflater().inflate(R.menu.main, menu);
// COMPLETED (10) Return true to display your menu
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.action_search) {
Context context = MainActivity.this;
String textToShow = "Search clicked";
Toast.makeText(context, textToShow, Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}

6. Toast: On line 7, use the toast to get the next feature:

toast

Working with strings.xml

you can get a String saved in res -> values -> strings.xml by calling the getString method.

String myString = getString(R.string.today);

 

Create Scroll List

add the next to the activity layout xml:

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_weather_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="20sp" />
</ScrollView>

view raw
ScrollView.xml
hosted with ❤ by GitHub

then fill the list as follows:

/*
* Iterate through the array and append the Strings to the TextView. The reason why we add
* the "\n\n\n" after the String is to give visual separation between each String in the
* TextView. Later, we'll learn about a better way to display lists of data.
*/
for (String toyName : toyNames) {
mToysListTextView.append(toyName + "\n\n\n");
}

view raw
importscrolls.java
hosted with ❤ by GitHub

Create a URI

To create a well-formed Uri without having to worry about the particular uri components, use the Uri Builder

Uri builtUri = Uri.parse(BASE_URL).buildUpon()
// add various params
.appendQueryParameter(PARAM_QUERY, githubSearchQuery)
.appendQueryParameter(PARAM_SORT, sortBy)
// build the uri
.build();
// Convert the Uri to a URL:
URL url = null;
try {
url = new URL(builtUri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
return url;

view raw
uribuilder.java
hosted with ❤ by GitHub