0

I am using one fragment for 2 different activities for that I have inflated fragments layout like this

the problem is that text of textview is not changing with textview.setText("") method.

Here is Activity

public class CloverHome extends AppCompatActivity {

View inflatedView;
View mInflatedView;
LayoutInflater inflater;
TextView roomrate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clover_home);

    inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.impianapricing, null);
    inflatedView = inflater.inflate(R.layout.facilitiesimpiana, viewGroup, false);
    roomrate = viewGroup.findViewById(R.id.roomratet);
    roomrate.setText(getString(R.string.temp));

    BottomNavigationView bt = findViewById(R.id.nav2);
    bt.setOnNavigationItemSelectedListener(navlistener);
    getSupportFragmentManager().beginTransaction().replace(R.id.frame2, new room_frag()).commit();    }


private BottomNavigationView.OnNavigationItemSelectedListener navlistener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment selected = null;

                switch (menuItem.getItemId()) {
                    case R.id.roomf:
                        selected = new room_frag();
                        break;

                    case R.id.facility:
                        selected = new facilitiesimpiana();
                        break;
                    case R.id.contact:
                        selected = new impprice();
                        break;

                }
                roomrate.setText("new");
                getSupportFragmentManager().beginTransaction().replace(R.id.frame2, Objects.requireNonNull(selected)).commit();
                return true;
            }
        };

}

Tanveer Ahmed
  • 426
  • 1
  • 4
  • 15
  • If the `roomratet` `` is in the `Fragment`s' layout, then you should be handling it in the `Fragment`s, not in the `Activity`. The `View`s you're inflating and modifying in `onCreate()` are never added to the on-screen hierarchy, and are essentially discarded at the end of that method, which is why you don't see any changes. – Mike M. Dec 06 '18 at 20:13
  • Then how can I change text from other activity? – Tanveer Ahmed Dec 06 '18 at 20:20
  • Any suggestions? – Tanveer Ahmed Dec 06 '18 at 20:28
  • I'm not sure what you mean by "other activity", but you can put the text into a `Bundle`, set that `Bundle` as the `Fragment`s' arguments, then retrieve that value inside the `Fragment`s, and set it on the `TextView`s there. There's a simple example in [this answer](https://stackoverflow.com/a/44670540). – Mike M. Dec 06 '18 at 20:44
  • you can get fragment by tag and update view inside fragment from activity, instead of above way – GianhTran Dec 07 '18 at 02:03

1 Answers1

0

Try the following -

roomrate.setText(getActivity()
        .getApplicationContext()
        .getResources()
        .getString(R.string.temp));
Nero
  • 1,058
  • 1
  • 9
  • 25