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