Skip to content

Instantly share code, notes, and snippets.

@novembergave
Last active December 29, 2016 21:58
Show Gist options
  • Save novembergave/befb382d6392059b5038c7628fad8c50 to your computer and use it in GitHub Desktop.
Save novembergave/befb382d6392059b5038c7628fad8c50 to your computer and use it in GitHub Desktop.
Japan Trip Autumn 2016 App

Hello! This is my Tour Guide App project for Udacity's Android Basics: Multiple Screens course. I have decided to use this project to feature my favourite places in the cities I have visted in my most recent trip to Japan.

As a lot of the project contains repeated material, I am only posting the code for my main landing pages and the modified ViewPager.

A corresponding blogpost with screenshots will be available on my blog

package com.example.android.japanautumntrip2016;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by novembergave on 21/12/2016.
*/
public class CategoryAdaptor extends FragmentPagerAdapter {
/** Context of the app */
private Context mContext;
/**
* Create a new {@link CategoryAdaptor} object.
*/
public CategoryAdaptor(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
/**
* Return the {@link Fragment} that should be displayed for the given page number.
*/
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new IntroFragment();
} else if (position == 1) {
return new OsakaFragment();
} else if (position == 2) {
return new MiyajimaHiroFragment();
} else if (position ==3) {
return new HimejiKobeFragment();
} else if (position ==4) {
return new KyotoUjiFragment();
} else {
return new KinosakiFragment();
}
}
/**
* Return the total number of pages.
*/
@Override
public int getCount() {
return 6;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return mContext.getString(R.string.category_intro);
} else if (position == 1) {
return mContext.getString(R.string.category_osaka);
} else if (position == 2) {
return mContext.getString(R.string.category_miyajimahiro);
} else if (position == 3) {
return mContext.getString(R.string.category_himejikobe);
} else if (position == 4) {
return mContext.getString(R.string.category_kyotouji);
} else {
return mContext.getString(R.string.category_kinosaki);
}
}
}
package com.example.android.japanautumntrip2016;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class IntroFragment extends Fragment {
public IntroFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_list, container, false);
// Create a list of words
final ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word(R.string.introduction, R.string.blog_url));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
WordAdaptor adapter = new WordAdaptor(getActivity(), words, R.color.icons);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) rootView.findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
// Send viewer to blog by clicking the listview item. There is only one item so there's
// no need to write additional code.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("http://www.novembergave.wordpress.com"));
startActivity(myWebLink);
}
});
return rootView;
}
}
package com.example.android.japanautumntrip2016;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
CategoryAdaptor adapter = new CategoryAdaptor(this, getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
// Find the tab layout that shows the tabs
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
// Connect the tab layout with the view pager. This will
// 1. Update the tab layout when the view pager is swiped
// 2. Update the view pager when a tab is selected
// 3. Set the tab layout's tab names with the view pager's adapter's titles
// by calling onPageTitle()
tabLayout.setupWithViewPager(viewPager);
}
}
package com.example.android.japanautumntrip2016;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class OsakaFragment extends Fragment {
public OsakaFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_list, container, false);
// Create a list of words
final ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word(R.string.location_osaka_kaiyukan, R.string.osaka_kaiyukan_desc,
R.drawable.osaka_0101kaiyukan));
words.add(new Word(R.string.osaka_kaiyukan_desc, R.string.osaka_kaiyukan_whaleshark,
R.drawable.osaka_0102kaiyukanwhaleshark));
words.add(new Word(R.string.location_osaka_kaiyukan, R.string.osaka_kaiyukan_feeding,
R.drawable.osaka_0103kaiyukanhungrywhaleshark));
words.add(new Word(R.string.location_shittenoji, R.string.shittenoji_desc,
R.drawable.osaka_0104shitennojitemple));
words.add(new Word(R.string.location_coco, R.string.location_coco,
R.drawable.osaka_0105cocoichibanyacrispychickenmustardcurry));
words.add(new Word(R.string.location_denden, R.string.denden_desc,
R.drawable.osaka_0106dendentown));
words.add(new Word(R.string.location_dotonbori, R.string.dotonbori_desc,
R.drawable.osaka_0107dotonbori));
words.add(new Word(R.string.location_osakastation, R.string.osakastation_desc,
R.drawable.osaka_0108osakasunset));
words.add(new Word(R.string.location_kamukura, R.string.kamukura_desc,
R.drawable.osaka_0109ramenegg));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
WordAdaptor adapter = new WordAdaptor(getActivity(), words, R.color.category_osaka_kino);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) rootView.findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
return rootView;
}
}
package com.example.android.japanautumntrip2016;
/**
* Created by novembergave on 21/12/2016.
*/
public class Word {
/** String resource ID for name of place */
private int mLocationId;
/** String resource ID for description of place */
private int mDescriptionId;
/** Image resource ID for the word */
private int mImageResourceId = NO_IMAGE_PROVIDED;
/** Constant value that represents no image was provided for this word */
private static final int NO_IMAGE_PROVIDED = -1;
/**
* Create a new Word object.
*/
public Word(int locationId, int descriptionId) {
mLocationId = locationId;
mDescriptionId = descriptionId;
}
/**
* Create a new Word object.
*/
public Word(int locationId, int descriptionId, int imageResourceId) {
mLocationId = locationId;
mDescriptionId = descriptionId;
mImageResourceId = imageResourceId;
}
/**
* Get the string resource ID for the name of the place.
*/
public int getLocationId() {
return mLocationId;
}
/**
* Get the string resource ID for the description of place.
*/
public int getDescriptionId() {
return mDescriptionId;
}
/**
* Return the image resource ID of the word.
*/
public int getImageResourceId() {
return mImageResourceId;
}
/**
* Returns whether or not there is an image for this word.
*/
public boolean hasImage() {
return mImageResourceId != NO_IMAGE_PROVIDED;
}
}
package com.example.android.japanautumntrip2016;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
/**
* Created by novembergave on 21/12/2016.
*/
public class WordAdaptor extends ArrayAdapter<Word> {
/** Resource ID for the background color for this page */
private int mColorResourceId;
public WordAdaptor(Context context, ArrayList<Word> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID location text view.
TextView locationTextView = (TextView) listItemView.findViewById(R.id.location_text_view);
// Get the name from the currentWord object and set this text on
// the location TextView.
locationTextView.setText(currentWord.getLocationId());
// Find the TextView in the list_item.xml layout with the ID description text view.
TextView descriptionTextView = (TextView) listItemView.findViewById(R.id.description_text_view);
// Get the description from the currentWord object and set this text on
// the description TextView.
descriptionTextView.setText(currentWord.getDescriptionId());
// Find the ImageView in the list_item.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
// Check if an image is provided for this word or not
if (currentWord.hasImage()) {
// If an image is available, display the provided image based on the resource ID
imageView.setImageResource(currentWord.getImageResourceId());
// Make sure the view is visible
imageView.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
imageView.setVisibility(View.GONE);
}
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
return listItemView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment