Search code examples
javaandroidandroid-fragmentsandroid-activityfragment

how change fragment textview with own java class?


I tried to change a TextView in Activity of Fragment in three cases, but I can't change the TextView.
Is there any solution to change a TextView of Fragment in own Fragment_Activity or Main_Activity?

Fragment.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


        TextView textView = (TextView) getView().findViewById(R.id.textView1);
        textView.setText("Done!");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText("Done!");
        return view;
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TextView tv = (TextView) getView().findViewById(R.id.textView1);
        tv.setText("Done!");
    }

Fragment.xml

...
 <TextView
        android:textColor="@color/myBlack"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:textAppearance="?android:attr/textAppearanceMedium"
        />
....

calling fragment class for view in MainActivity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_weixin);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        instance = this;

...

 View view2 = mLi.inflate(R.layout.fragment_blank_fragment2, null);
           views.add(view2);
...
}

Solution

  • Thing to know

    1. In onCreate of fragment getView() returns null because still view in not created.
    2. Create a method in fragment to set value for the TextView from activity using fragment instance.

    Example : Fragment

    public class Fragment1 extends Fragment {
    
    
        TextView tv;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
            tv = (TextView) view.findViewById(R.id.textView1);
            tv.setText("Done!");
            return view;
    
        }
    
        public void setViewText(String text){
            tv.setText(text);
        }
    }
    

    Activity

    public class MyActivity extends FragmentActivity {
    
        Fragment1 fragment1;
    
        @Override
        protected void onCreate(Bundle arg0) {
            super.onCreate(arg0);
            setContentView(R.layout.activity_fragment);
            //as you are using static fragment so create fragment instance using findFragmentById(id)
            fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
    
            fragment1.setViewText("Message");
    
        }
    }
    

    Visit Official doc Communicating with Other Fragments