Android find fragment in activity

Fragment is a view in android development. If you have gui elements which are repeating on different pages then you can use fragment. Android activity can be a collection of more than one fragments. Now in activity if you want to access any GUI element of fragment like Textview then you first need to get fragment and from fragment get that GUI element.

In first step give a tag to fragment in xml layout file

android:tag="tagtopfrg"

then in activity write following code

Fragment FragmentTopBar=getSupportFragmentManager().findFragmentByTag("tagtopfrg");
       TextView txtCityname=FragmentTopBar.getView().findViewById(R.id.LblCityName);
       txtCityname.setText(citytext);

I am freelancer software developer you can find me at UPWORK

 

Leave a Reply

Your email address will not be published. Required fields are marked *