Android app that leverages the OpenLibrary API to search books and display cover images. This app is to be used as the base app for adding suggested extensions.
The app does the following:
- Fetch the books from the OpenLibrary Search API in JSON format
- Deserialize the JSON data for each of the books into
Book
objects - Build an array of
Book
objects and notify the adapter to display the new data. - Define a view holder so the adapter can render each book model.
To achieve this, there are four different components in this app:
BookClient
- Responsible for executing the API requests and retrieving the JSONBook
- Model object responsible for encapsulating the attributes for each individual bookBookAdapter
- Responsible for mapping eachBook
to a particular view layoutBookListActivity
- Responsible for fetching and deserializing the data and configuring the adapter
This app is intended to be the base project on top of which new features can be added. To use it, clone the project and import it using the following steps:
- Use SearchView to search for books with a title
- Show ProgressBar before each network request
- Add a detail view to display more information about the selected book from the list
- Use a share intent to recommend a book to friends
This app leverages two third-party libraries:
- Android AsyncHTTPClient - For asynchronous network requests
- Picasso - For remote image loading
- What is context? When it is used? Why?
- A
Context
is a liaison between an application and the environment it's running in. It informs the application about its available resources, and grants access to those resources, such as images, other applications, databases, etc. - We often use Contexts to pass information to a new item, like a
View
on anActivity
.
- A
- How do I setup a click handler for items in a
RecyclerView
?- In the
ViewHolder
for the items in aRecyclerView
, implement anonClick
method after implementing theView.OnClickListener
interface.
- In the
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(View itemView) {
// Do any necessary setup
...
itemView.setOnClickListener(this);
}
// Handles the row being being clicked
@Override
public void onClick(View view) {
int position = getAdapterPosition(); // gets item position
...
}
}
- How do I change the title text in the
ActionBar
? How do I add menu items?- To change the title text...
- We first retrieve the
ActionBar
viagetSupportActionBar()
, or by using a Toolbar of our own. - Then, we set the title of the
ActionBar
withactionBar.setTitle(str)
.
- We first retrieve the
- To add menu items...
- We configure a menu layout resource file with the items of our choosing. A few examples:
- To change the title text...
Place inside of...
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".BookSearchActivity">
...
</menu>
Settings
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
Search
<item android:id="@+id/action_search"
android:orderInCategory="5"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
Progress Bar
<item
android:id="@+id/miActionProgress"
android:title="Loading..."
android:visible="false"
android:orderInCategory="100"
app:showAsAction="always"
app:actionLayout="@layout/action_view_progress" />
Share
<item
android:id="@+id/miShare"
app:showAsAction="ifRoom"
android:title="Share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
- In `onCreateOptionsMenu` in the `Activity`, inflate the menu item(s):
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_layout_name, menu);
MenuItem item = menu.findItem(R.id.item_id);
...
return super.onCreateOptionsMenu(menu);
}
- How can I make menu items clickable?
- Hook into
onOptionsItemSelected
in theActivity
, and filter the menu item clicked by its id.
- Hook into
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.item_id) {
return true;
} else if ...
return super.onOptionsItemSelected(item);
}
- What is an intent? What are the two types and how do they differ?
- An
Intent
is the fabric between twoActivity
s, with oneActivity
from which to go and oneActivity
to which to go. - We're not sure about the difference between implicit and explicit intents.
- An
- What types of tasks can I do with an implicit intent?
- How do I communicate data between intents? to a child intent and back to the parent?
- How do I enable an arbitrary object to be passable via an intent?
- What’s the difference between Parcelable and Serializable?