How to get a View from another xml, I write the following code in MainActivity , but it does not work:

  View inflatedView = getLayoutInflater().inflate(R.layout.fragment_navigation_drawer, null); TextView text = (TextView) inflatedView.findViewById(R.id.textView3); text.setText("Hello!"); 
  • What exactly is not working ? The code is correct, it should work. - Vladyslav Matviienko
  • Describe more precisely what does not work? Is the error falling out? Or just no reaction? and where on your activit do you use this fragment_navigation_drawer.xml? - Werder
  • There is no error code in your piece, give more info) - iFr0z
  • no reaction, fragment_navigation_drawer.xml is the first item in the listview, there are two texts and a picture - David Kern
  • A similar problem is here - Yuriy SPb

1 answer 1

You are the third person in three days who LayoutInflater .

It is used only to load new markup from xml and display it on the screen. As long as you do not add the downloaded to the screen, you will not see it.

You do not need to LayoutInflater but look for the markup elements you need and already displayed on the screen using the finViewById methods.


 NavigationView navigationView = findViewById(...); View headerView = navigationView.getHeaderView(0); TextView text = (TextView) headerView.findViewById(R.id.textView3); 
  • Obviously, on version 19 I added the top picture in the navbar itself, in android studio there is already a navbar material GTE already there, I tried the first item with pictures and texts many times to get the first item but it didn’t work and I turned for help. thanks for your help - David Kern
  • @DavidKern, do you need to get HeaderView from NavigationView? .. - Yuriy SPb
  • yes, I probably put it wrong when asking a question - David Kern
  • @DavidKern, see the updated answer - YuriySPb