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

Commit 9636797

Browse files
cezannecAsser
cezannec
authored and
Asser
committed
TFragments.01-Solution-CreateBodyPartFragment
1 parent 7507f4e commit 9636797

File tree

4 files changed

+92
-12
lines changed

4 files changed

+92
-12
lines changed

app/src/main/java/com/example/android/android_me/ui/AndroidMeActivity.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.android.android_me.ui;
1818

19+
import android.support.v4.app.FragmentManager;
1920
import android.support.v7.app.AppCompatActivity;
2021
import android.os.Bundle;
2122

@@ -24,21 +25,20 @@
2425
// This activity will display a custom Android image composed of three body parts: head, body, and legs
2526
public class AndroidMeActivity extends AppCompatActivity {
2627

27-
// TODO (1) Create a layout file that displays one body part image named fragment_body_part.xml
28-
// This layout should contain a single ImageView
29-
30-
// TODO (2) Create a new class called BodyPartFragment to display an image of an Android-Me body part
31-
// In this class, you'll need to implement an empty constructor and the onCreateView method
32-
// TODO (3) Show the first image in the list of head images
33-
// Soon, you'll update this image display code to show any image you want
34-
35-
3628

3729
@Override
3830
protected void onCreate(Bundle savedInstanceState) {
3931
super.onCreate(savedInstanceState);
4032
setContentView(R.layout.activity_android_me);
4133

42-
// TODO (5) Create a new BodyPartFragment instance and display it using the FragmentManager
34+
// Create a new head BodyPartFragment
35+
BodyPartFragment headFragment = new BodyPartFragment();
36+
37+
// Add the fragment to its container using a FragmentManager and a Transaction
38+
FragmentManager fragmentManager = getSupportFragmentManager();
39+
40+
fragmentManager.beginTransaction()
41+
.add(R.id.head_container, headFragment)
42+
.commit();
4343
}
4444
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2017 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.android_me.ui;
18+
19+
import android.os.Bundle;
20+
import android.support.v4.app.Fragment;
21+
import android.view.LayoutInflater;
22+
import android.view.View;
23+
import android.view.ViewGroup;
24+
import android.widget.ImageView;
25+
26+
import com.example.android.android_me.R;
27+
import com.example.android.android_me.data.AndroidImageAssets;
28+
29+
public class BodyPartFragment extends Fragment {
30+
31+
/**
32+
* Mandatory empty constructor for the fragment manager to instantiate the fragment
33+
*/
34+
public BodyPartFragment() {
35+
}
36+
37+
/**
38+
* Inflates the fragment layout file and sets the correct resource for the image to display
39+
*/
40+
@Override
41+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
42+
43+
// Inflate the Android-Me fragment layout
44+
View rootView = inflater.inflate(R.layout.fragment_body_part, container, false);
45+
46+
// Get a reference to the ImageView in the fragment layout
47+
ImageView imageView = (ImageView) rootView.findViewById(R.id.body_part_image_view);
48+
49+
// Set the image to the first in our list of head images
50+
imageView.setImageResource(AndroidImageAssets.getHeads().get(0));
51+
52+
// Return the rootView
53+
return rootView;
54+
}
55+
56+
}

app/src/main/res/layout/activity_android_me.xml

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
android:paddingRight="@dimen/activity_horizontal_margin"
3030
android:paddingTop="@dimen/activity_vertical_margin">
3131

32-
<!-- TODO (4) Create a container to hold the head BodyPartFragment of the custom Android -->
33-
<!--The container should be 180dp in height -->
32+
33+
<!-- This container holds the head BodyPartFragment of the custom Android-Me image -->
34+
<FrameLayout android:id="@+id/head_container"
35+
android:layout_width="match_parent"
36+
android:layout_height="180dp"
37+
android:scaleType="centerInside"/>
3438

3539

3640

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--Copyright (C) 2017 The Android Open Source Project
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.-->
12+
13+
<!-- This fragment layout displays just one image of an Android-Me body part -->
14+
<ImageView
15+
xmlns:android="http://schemas.android.com/apk/res/android"
16+
android:id="@+id/body_part_image_view"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent">
19+
20+
</ImageView>

0 commit comments

Comments
 (0)