OSDN Git Service

Add new API demo for -swNNNdp resource qualifier.
[android-x86/development.git] / samples / ApiDemos / src / com / example / android / apis / support / app / FragmentArgumentsSupport.java
1 /*
2  * Copyright (C) 2011 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.apis.support.app;
18
19 import com.example.android.apis.R;
20
21 import android.support.v4.app.FragmentActivity;
22 import android.support.v4.app.Fragment;
23 import android.support.v4.app.FragmentTransaction;
24
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;
33
34 /**
35  * Demonstrates a fragment that can be configured through both Bundle arguments
36  * and layout attributes.
37  */
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);
43
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);
49             ft.commit();
50         }
51     }
52 //END_INCLUDE(create)
53
54 //BEGIN_INCLUDE(fragment)
55     public static class MyFragment extends Fragment {
56         CharSequence mLabel;
57
58         /**
59          * Create a new instance of MyFragment that will be initialized
60          * with the given arguments.
61          */
62         static MyFragment newInstance(CharSequence label) {
63             MyFragment f = new MyFragment();
64             Bundle b = new Bundle();
65             b.putCharSequence("label", label);
66             f.setArguments(b);
67             return f;
68         }
69
70         /**
71          * Parse attributes during inflation from a view hierarchy into the
72          * arguments we handle.
73          */
74         @Override public void onInflate(Activity activity, AttributeSet attrs,
75                 Bundle savedInstanceState) {
76             super.onInflate(activity, attrs, savedInstanceState);
77
78             TypedArray a = activity.obtainStyledAttributes(attrs,
79                     R.styleable.FragmentArguments);
80             mLabel = a.getText(R.styleable.FragmentArguments_android_label);
81             a.recycle();
82         }
83
84         /**
85          * During creation, if arguments have been supplied to the fragment
86          * then parse those out.
87          */
88         @Override public void onCreate(Bundle savedInstanceState) {
89             super.onCreate(savedInstanceState);
90
91             Bundle args = getArguments();
92             if (args != null) {
93                 CharSequence label = args.getCharSequence("label");
94                 if (label != null) {
95                     mLabel = label;
96                 }
97             }
98         }
99
100         /**
101          * Create the view for this fragment, using the arguments given to it.
102          */
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));
109             return v;
110         }
111     }
112 //END_INCLUDE(fragment)
113 }