2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.supportv4.app;
19 import com.example.android.supportv4.R;
21 import android.support.v4.app.FragmentActivity;
22 import android.support.v4.app.Fragment;
23 import android.support.v4.app.FragmentTransaction;
25 import android.app.Activity;
26 import android.content.res.TypedArray;
27 import android.os.Bundle;
28 import android.util.AttributeSet;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.TextView;
35 * Demonstrates a fragment that can be configured through both Bundle arguments
36 * and layout attributes.
38 public class FragmentArgumentsSupport extends FragmentActivity {
39 //BEGIN_INCLUDE(create)
40 @Override protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.fragment_arguments_support);
44 if (savedInstanceState == null) {
45 // First-time init; create fragment to embed in activity.
46 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
47 Fragment newFragment = MyFragment.newInstance("From Arguments");
48 ft.add(R.id.created, newFragment);
54 //BEGIN_INCLUDE(fragment)
55 public static class MyFragment extends Fragment {
59 * Create a new instance of MyFragment that will be initialized
60 * with the given arguments.
62 static MyFragment newInstance(CharSequence label) {
63 MyFragment f = new MyFragment();
64 Bundle b = new Bundle();
65 b.putCharSequence("label", label);
71 * Parse attributes during inflation from a view hierarchy into the
72 * arguments we handle.
74 @Override public void onInflate(Activity activity, AttributeSet attrs,
75 Bundle savedInstanceState) {
76 super.onInflate(activity, attrs, savedInstanceState);
78 TypedArray a = activity.obtainStyledAttributes(attrs,
79 R.styleable.FragmentArguments);
80 mLabel = a.getText(R.styleable.FragmentArguments_android_label);
85 * During creation, if arguments have been supplied to the fragment
86 * then parse those out.
88 @Override public void onCreate(Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
91 Bundle args = getArguments();
93 CharSequence label = args.getCharSequence("label");
101 * Create the view for this fragment, using the arguments given to it.
103 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
104 Bundle savedInstanceState) {
105 View v = inflater.inflate(R.layout.hello_world, container, false);
106 View tv = v.findViewById(R.id.text);
107 ((TextView)tv).setText(mLabel != null ? mLabel : "(no label)");
108 tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
112 //END_INCLUDE(fragment)