0

im trying to set text inside my fragment file from the main activity. here is my code

this is my mainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new SQLiteHandler(getApplicationContext());

    session = new SessionManager(getApplicationContext());

    if (!session.isLoggedIn()) {
        logoutUser();
    }

    HashMap<String, String> user = db.getUserDetails();

    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.main_container, new HomeFragment());
    fragmentTransaction.commit();

    getSupportActionBar().setTitle("Home Fragment");

    navigationView = (NavigationView)findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            switch (item.getItemId()){
                case R.id.home_id:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new HomeFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Home Fragment");
                    item.setChecked(true);
                    break;

                case R.id.help_id:
                    fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new HelpFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Help Fragment");
                    item.setChecked(true);
                    break;

                case R.id.logout:
                    logoutUser();
            }

            return false;
        }
    });

}

and i have this (HomeFragment.java)

public class HomeFragment extends Fragment {

 public HomeFragment() {
    // Required empty public constructor
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
 }
}

and i have this on my fragment_home.xml

<TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="@color/lbl_name"
        android:textSize="24dp" />

so can i set 'name' from the mainActivity? if yes, please let me know how?

xbee
  • 161
  • 1
  • 8

2 Answers2

2

When adding your fragment you can assign a tag to it:

fragmentTransaction.add(R.id.main_container, new HomeFragment(), "MY_FRAGMENT");
fragmentTransaction.commit();

When you need to change the TextView you can recover the fragment with the used tag and call a method of the fragment that changes the text:

HomeFragment fragment = (HomeFragment) getSupportFragmentManager()
    .findFragmentByTag("MY_FRAGMENT");
fragment.changeText("New text");

If you are not using the support library, replace getSupportFragmentManager() with getFragmentManager().

That method inside the HomeFragment class would be:

public void changeText(String newText) {
    TextView textview = (TextView) getView().findViewById(R.id.name);
    textview.setText(newText);
}
JMSilla
  • 1,326
  • 10
  • 17
0

Here is your error you are not proving any view for textview ing oncreate view, try this

HomeFragment Class

public static TextView mTextView;



   @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
      View  view = inflater.inflate(R.layout.fragment_home, container, false);

     mTextView=(TextView)view.findviewById(R.id.name);

    return view;
    }

And In your class Activity

after adding fragment call like this

HomeFragment.mTextView.setText("Name");

Try the above code

Quick learner
  • 10,632
  • 4
  • 45
  • 55