From 7507f4e375a8e377f09950f74f8e75bd7a591ea9 Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 17:33:39 -0800 Subject: [PATCH 1/5] TFragments.01-Exercise-CreateBodyPartFragment --- .../android/android_me/data/AndroidImageAssets.java | 2 +- .../android/android_me/ui/AndroidMeActivity.java | 12 ++++++++++++ app/src/main/res/layout/activity_android_me.xml | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/example/android/android_me/data/AndroidImageAssets.java b/app/src/main/java/com/example/android/android_me/data/AndroidImageAssets.java index ea82261ee..f339000bb 100755 --- a/app/src/main/java/com/example/android/android_me/data/AndroidImageAssets.java +++ b/app/src/main/java/com/example/android/android_me/data/AndroidImageAssets.java @@ -77,7 +77,7 @@ public class AndroidImageAssets { addAll(bodies); addAll(legs); }}; - + // Getter methods that return lists of all head images, body images, and leg images diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index dcb2a163e..7e5d243b0 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -24,9 +24,21 @@ // This activity will display a custom Android image composed of three body parts: head, body, and legs public class AndroidMeActivity extends AppCompatActivity { + // TODO (1) Create a layout file that displays one body part image named fragment_body_part.xml + // This layout should contain a single ImageView + + // TODO (2) Create a new class called BodyPartFragment to display an image of an Android-Me body part + // In this class, you'll need to implement an empty constructor and the onCreateView method + // TODO (3) Show the first image in the list of head images + // Soon, you'll update this image display code to show any image you want + + + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_me); + + // TODO (5) Create a new BodyPartFragment instance and display it using the FragmentManager } } diff --git a/app/src/main/res/layout/activity_android_me.xml b/app/src/main/res/layout/activity_android_me.xml index ee47f7e24..71bd30adb 100644 --- a/app/src/main/res/layout/activity_android_me.xml +++ b/app/src/main/res/layout/activity_android_me.xml @@ -29,6 +29,10 @@ android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> + + + + From 9636797838d0bf35e0346945965514d834e7ed09 Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 17:47:39 -0800 Subject: [PATCH 2/5] TFragments.01-Solution-CreateBodyPartFragment --- .../android_me/ui/AndroidMeActivity.java | 20 +++---- .../android_me/ui/BodyPartFragment.java | 56 +++++++++++++++++++ .../main/res/layout/activity_android_me.xml | 8 ++- .../main/res/layout/fragment_body_part.xml | 20 +++++++ 4 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java create mode 100644 app/src/main/res/layout/fragment_body_part.xml diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index 7e5d243b0..db6f37c88 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -16,6 +16,7 @@ package com.example.android.android_me.ui; +import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -24,21 +25,20 @@ // This activity will display a custom Android image composed of three body parts: head, body, and legs public class AndroidMeActivity extends AppCompatActivity { - // TODO (1) Create a layout file that displays one body part image named fragment_body_part.xml - // This layout should contain a single ImageView - - // TODO (2) Create a new class called BodyPartFragment to display an image of an Android-Me body part - // In this class, you'll need to implement an empty constructor and the onCreateView method - // TODO (3) Show the first image in the list of head images - // Soon, you'll update this image display code to show any image you want - - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_me); - // TODO (5) Create a new BodyPartFragment instance and display it using the FragmentManager + // Create a new head BodyPartFragment + BodyPartFragment headFragment = new BodyPartFragment(); + + // Add the fragment to its container using a FragmentManager and a Transaction + FragmentManager fragmentManager = getSupportFragmentManager(); + + fragmentManager.beginTransaction() + .add(R.id.head_container, headFragment) + .commit(); } } diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java new file mode 100644 index 000000000..bc065113c --- /dev/null +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -0,0 +1,56 @@ +/* +* Copyright (C) 2017 The Android Open Source Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package com.example.android.android_me.ui; + +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.ImageView; + +import com.example.android.android_me.R; +import com.example.android.android_me.data.AndroidImageAssets; + +public class BodyPartFragment extends Fragment { + + /** + * Mandatory empty constructor for the fragment manager to instantiate the fragment + */ + public BodyPartFragment() { + } + + /** + * Inflates the fragment layout file and sets the correct resource for the image to display + */ + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + + // Inflate the Android-Me fragment layout + View rootView = inflater.inflate(R.layout.fragment_body_part, container, false); + + // Get a reference to the ImageView in the fragment layout + ImageView imageView = (ImageView) rootView.findViewById(R.id.body_part_image_view); + + // Set the image to the first in our list of head images + imageView.setImageResource(AndroidImageAssets.getHeads().get(0)); + + // Return the rootView + return rootView; + } + +} diff --git a/app/src/main/res/layout/activity_android_me.xml b/app/src/main/res/layout/activity_android_me.xml index 71bd30adb..b7af0db2c 100644 --- a/app/src/main/res/layout/activity_android_me.xml +++ b/app/src/main/res/layout/activity_android_me.xml @@ -29,8 +29,12 @@ android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> - - + + + diff --git a/app/src/main/res/layout/fragment_body_part.xml b/app/src/main/res/layout/fragment_body_part.xml new file mode 100644 index 000000000..1455ea00b --- /dev/null +++ b/app/src/main/res/layout/fragment_body_part.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file From 29da6c23a1414659a228aebb5db7948ffaf9cb06 Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 19:56:18 -0800 Subject: [PATCH 3/5] TFragments.02-Exercise-DisplayThreeFragments --- .../example/android/android_me/ui/AndroidMeActivity.java | 5 +++++ .../example/android/android_me/ui/BodyPartFragment.java | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index db6f37c88..5d98c17bc 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -34,11 +34,16 @@ protected void onCreate(Bundle savedInstanceState) { // Create a new head BodyPartFragment BodyPartFragment headFragment = new BodyPartFragment(); + // TODO (4) Set the list of image id's for the head fragment and set the position to the second image in the list + // Add the fragment to its container using a FragmentManager and a Transaction FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .add(R.id.head_container, headFragment) .commit(); + + // TODO (5) Create and display the body and leg BodyPartFragments + } } diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java index bc065113c..cdb944a3a 100644 --- a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -28,6 +28,11 @@ public class BodyPartFragment extends Fragment { + // TODO (1) Create a setter method and class variable to set and store of a list of image resources + + // TODO (2) Create another setter method and variable to track and set the index of the list item to display + // ex. index = 0 is the first image id in the given list , index 1 is the second, and so on + /** * Mandatory empty constructor for the fragment manager to instantiate the fragment */ @@ -49,6 +54,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa // Set the image to the first in our list of head images imageView.setImageResource(AndroidImageAssets.getHeads().get(0)); + // TODO (3) If a list of image ids exists, set the image resource to the correct item in that list + // Otherwise, create a Log statement that indicates that the list was not found + // Return the rootView return rootView; } From 6d7b7bec54a5230c2b301c924a5652551db6854b Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 21:20:12 -0800 Subject: [PATCH 4/5] TFragments.02-Solution-DisplayThreeFragments --- .../android_me/ui/AndroidMeActivity.java | 20 +++++++++-- .../android_me/ui/BodyPartFragment.java | 34 ++++++++++++++----- .../main/res/layout/activity_android_me.xml | 11 ++++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index 5d98c17bc..6ba47a389 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -21,6 +21,7 @@ import android.os.Bundle; import com.example.android.android_me.R; +import com.example.android.android_me.data.AndroidImageAssets; // This activity will display a custom Android image composed of three body parts: head, body, and legs public class AndroidMeActivity extends AppCompatActivity { @@ -34,7 +35,9 @@ protected void onCreate(Bundle savedInstanceState) { // Create a new head BodyPartFragment BodyPartFragment headFragment = new BodyPartFragment(); - // TODO (4) Set the list of image id's for the head fragment and set the position to the second image in the list + // Set the list of image id's for the head fragment and set the position to the second image in the list + headFragment.setImageIds(AndroidImageAssets.getHeads()); + headFragment.setListIndex(1); // Add the fragment to its container using a FragmentManager and a Transaction FragmentManager fragmentManager = getSupportFragmentManager(); @@ -43,7 +46,20 @@ protected void onCreate(Bundle savedInstanceState) { .add(R.id.head_container, headFragment) .commit(); - // TODO (5) Create and display the body and leg BodyPartFragments + // Create and display the body and leg BodyPartFragments + + BodyPartFragment bodyFragment = new BodyPartFragment(); + bodyFragment.setImageIds(AndroidImageAssets.getBodies()); + fragmentManager.beginTransaction() + .add(R.id.body_container, bodyFragment) + .commit(); + + BodyPartFragment legFragment = new BodyPartFragment(); + legFragment.setImageIds(AndroidImageAssets.getLegs()); + fragmentManager.beginTransaction() + .add(R.id.leg_container, legFragment) + .commit(); + } } diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java index cdb944a3a..0526e957f 100644 --- a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -18,20 +18,24 @@ import android.os.Bundle; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.example.android.android_me.R; -import com.example.android.android_me.data.AndroidImageAssets; + +import java.util.List; public class BodyPartFragment extends Fragment { - // TODO (1) Create a setter method and class variable to set and store of a list of image resources + // Tag for logging + private static final String TAG = "BodyPartFragment"; - // TODO (2) Create another setter method and variable to track and set the index of the list item to display - // ex. index = 0 is the first image id in the given list , index 1 is the second, and so on + // Variables to store a list of image resources and the index of the image that this fragment displays + private List mImageIds; + private int mListIndex; /** * Mandatory empty constructor for the fragment manager to instantiate the fragment @@ -51,14 +55,28 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa // Get a reference to the ImageView in the fragment layout ImageView imageView = (ImageView) rootView.findViewById(R.id.body_part_image_view); - // Set the image to the first in our list of head images - imageView.setImageResource(AndroidImageAssets.getHeads().get(0)); - - // TODO (3) If a list of image ids exists, set the image resource to the correct item in that list + // If a list of image ids exists, set the image resource to the correct item in that list // Otherwise, create a Log statement that indicates that the list was not found + if(mImageIds != null){ + // Set the image resource to the list item at the stored index + imageView.setImageResource(mImageIds.get(mListIndex)); + } else { + Log.v(TAG, "This fragment has a null list of image id's"); + } // Return the rootView return rootView; } + // Setter methods for keeping track of the list images this fragment can display and which image + // in the list is currently being displayed + + public void setImageIds(List imageIds) { + mImageIds = imageIds; + } + + public void setListIndex(int index) { + mListIndex = index; + } + } diff --git a/app/src/main/res/layout/activity_android_me.xml b/app/src/main/res/layout/activity_android_me.xml index b7af0db2c..c65bc721b 100644 --- a/app/src/main/res/layout/activity_android_me.xml +++ b/app/src/main/res/layout/activity_android_me.xml @@ -30,12 +30,23 @@ android:paddingTop="@dimen/activity_vertical_margin"> + + + + + From c245127a728896cd81e57c97e241ae6225a1d76f Mon Sep 17 00:00:00 2001 From: cezannec Date: Sun, 26 Feb 2017 21:44:45 -0800 Subject: [PATCH 5/5] TFragments.03-Exercise-ClicksAndSavingState --- .../example/android/android_me/ui/AndroidMeActivity.java | 2 ++ .../example/android/android_me/ui/BodyPartFragment.java | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java index 6ba47a389..39345cafb 100644 --- a/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java +++ b/app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java @@ -32,6 +32,8 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_me); + // TODO (5) Only create new fragments when there is no previously saved state + // Create a new head BodyPartFragment BodyPartFragment headFragment = new BodyPartFragment(); diff --git a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java index 0526e957f..370ba872d 100644 --- a/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java +++ b/app/src/main/java/com/example/android/android_me/ui/BodyPartFragment.java @@ -30,6 +30,8 @@ public class BodyPartFragment extends Fragment { + // TODO (3) Create final Strings to store state information about the list of images and list index + // Tag for logging private static final String TAG = "BodyPartFragment"; @@ -60,6 +62,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa if(mImageIds != null){ // Set the image resource to the list item at the stored index imageView.setImageResource(mImageIds.get(mListIndex)); + + // TODO (1) Set a click listener on the image view and on a click increment the list index and set the image resource + // TODO (2) If you reach the end of a list of images, set the list index back to 0 (the first item in the list) + } else { Log.v(TAG, "This fragment has a null list of image id's"); } @@ -79,4 +85,5 @@ public void setListIndex(int index) { mListIndex = index; } + // TODO (4) Override onSaveInstanceState and save the current state of this fragment }