Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

TFragments.01-Solution-CreateBodyPartFragment #10

Open
wants to merge 1 commit into
base: TFragments.01-Exercise-CreateBodyPartFragment
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
8 changes: 6 additions & 2 deletions app/src/main/res/layout/activity_android_me.xml
Original file line number Diff line number Diff line change
@@ -29,8 +29,12 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<!-- TODO (4) Create a container to hold the head BodyPartFragment of the custom Android -->
<!--The container should be 180dp in height -->

<!-- This container holds the head BodyPartFragment of the custom Android-Me image -->
<FrameLayout android:id="@+id/head_container"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"/>



20 changes: 20 additions & 0 deletions app/src/main/res/layout/fragment_body_part.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--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.-->

<!-- This fragment layout displays just one image of an Android-Me body part -->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/body_part_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ImageView>