accessing textview within a fragment in activity

18,925

Solution 1

You should call getView() on the Fragment and then use findViewById() on the view returned.

Of course, it won't return anything until after the view has been created, so you may have to call it somewhere besides onCreate.

Solution 2

Well, you need to inflate an xml file, or create one from scratch, though I wouldn't recommend it, unless you like the extra work :) But with an Activity, the setContentView() inflates your xml file, and subsequently in onCreate() you can access it the way you are use to. Mind you, if you try accessing a layout View outside of onCreate without the parent View, you will be getting the same issue. Unless you made a global variable. Since onCreate is where a developer will usually always start things off, Android made it easy to just omit the parent View from the findViewById.

Assuming you have an xml file called edit.xml:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//this method is found within your Fragment, which you must ovverride
View view = inflater.inflate(R.layout.edit, container, false);
private EditText editText = (EditText) view.findViewById(R.id.editTextName);
//...
}

Feel free to check out the Android docs on Fragments to get a better understanding of why I chose to do it in that method instead of the Fragments onCreate, etc.

Share:
18,925
float
Author by

float

Updated on June 05, 2022

Comments

  • float
    float almost 2 years

    I wanted to use Fragments with in the ActionBar. Unfortunately it looks like its really complicated. My Fragments have Textviews and I want to be able to communicate with them out of my activity. Before I started to use Fragments I could access them with

    private EditText editText = (EditText) findViewById(R.id.editTextName);
    

    So I was able to receive the editText value when a user clicked on save. How should I do this the Fragment-way?

    Activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_recipe);
        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        getSupportActionBar().setDisplayOptions(0, com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_HOME | com.actionbarsherlock.app.ActionBar.DISPLAY_USE_LOGO | com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_TITLE);
    
        ActionBar.Tab newTab0 = getSupportActionBar()
                .newTab()
                .setText(R.string.fragment_general)
                .setTabListener(
                new MyTabListener<GeneralFragment>(this, "general",
                                GeneralFragment.class));
    
    
        getSupportActionBar().addTab(newTab0);
    
    }
    public static class MyTabListener<T extends Fragment> implements
            TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;
    
        /**
         * * Constructor used each time a new tab is created. * * @param
         * activity * The host Activity, used to instantiate the fragment * @param
         * tag * The identifier tag for the fragment * @param clz * The
         * fragment's Class, used to instantiate the fragment
         */
    
        public MyTabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }
    
        /* The following are each of the ActionBar.TabListener callbacks */
    
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if (mFragment == null) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                // If it exists, simply attach it in order to show it
                //ft.setCustomAnimations(android.R.animator.fade_in, R.animator.animationtest);
                ft.attach(mFragment);
            }
        }
    
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                //ft.setCustomAnimations(android.R.animator.fade_in, R.animator.test);
                ft.detach(mFragment);
            }
        }
    
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }