BasicNetworking / src / com.example.android.basicnetworking /

SimpleTextFragment.java

1
/*
2
 * Copyright 2013 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.basicnetworking;
18
 
19
import android.os.Bundle;
20
 
21
import android.support.v4.app.Fragment;
22
import android.util.Log;
23
import android.view.Gravity;
24
import android.view.LayoutInflater;
25
import android.view.View;
26
import android.view.ViewGroup;
27
import android.widget.TextView;
28
 
29
/**
30
 * Simple fragment containing only a TextView. Used by TextPagerAdapter to create
31
 * tutorial-style pages for apps.
32
 */
33
public class SimpleTextFragment extends Fragment {
34
 
35
    // Contains the text that will be displayed by this Fragment
36
    String mText;
37
 
38
    // Contains a resource ID for the text that will be displayed by this fragment.
39
    int mTextId = -1;
40
 
41
    // Keys which will be used to store/retrieve text passed in via setArguments.
42
    public static final String TEXT_KEY = "text";
43
    public static final String TEXT_ID_KEY = "text_id";
44
 
45
    // For situations where the app wants to modify text at Runtime, exposing the TextView.
46
    private TextView mTextView;
47
 
48
    public SimpleTextFragment() {
49
    }
50
 
51
    @Override
52
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
53
            Bundle savedInstanceState) {
54
        // Before initializing the textView, check if any arguments were provided via setArguments.
55
        processArguments();
56
 
57
        // Create a new TextView and set its text to whatever was provided.
58
        mTextView = new TextView(getActivity());
59
        mTextView.setGravity(Gravity.CENTER);
60
 
61
        if (mText != null) {
62
            mTextView.setText(mText);
63
            Log.i("SimpleTextFragment", mText);
64
        }
65
        return mTextView;
66
    }
67
 
68
    public TextView getTextView() {
69
        return mTextView;
70
    }
71
 
72
    /**
73
     * Changes the text for this TextView, according to the resource ID provided.
74
     * @param stringId A resource ID representing the text content for this Fragment's TextView.
75
     */
76
    public void setText(int stringId) {
77
        getTextView().setText(getActivity().getString(stringId));
78
    }
79
 
80
    /**
81
     * Processes the arguments passed into this Fragment via setArguments method.
82
     * Currently the method only looks for text or a textID, nothing else.
83
     */
84
    public void processArguments() {
85
        // For most objects we'd handle the multiple possibilities for initialization variables
86
        // as multiple constructors.  For Fragments, however, it's customary to use
87
        // setArguments / getArguments.
88
        if (getArguments() != null) {
89
            Bundle args = getArguments();
90
            if (args.containsKey(TEXT_KEY)) {
91
                mText = args.getString(TEXT_KEY);
92
                Log.d("Constructor", "Added Text.");
93
            } else if (args.containsKey(TEXT_ID_KEY)) {
94
                mTextId = args.getInt(TEXT_ID_KEY);
95
                mText = getString(mTextId);
96
            }
97
        }
98
    }
99
}