-1
good evening, I’m having a hard time changing the components of an XML file, when I open the Drawer menu I click on the "tracking" button it opens the xml fragment_tracking.xml, but I can’t access the components of this XML,
Follows my fragment_accompaniment code `
<Button
android:id="@+id/buttonteste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewteste" />
<TextView
android:id="@+id/textViewteste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
`
Below is the code of Activity created through Navigation Drawer Activity
private AppBarConfiguration mAppBarConfiguration;
private FirebaseAuth autenticacao;
public TextView textoemail, textViewNome, textViewTeste;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
autenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_visita, R.id.nav_acompanhamento
)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_sair:
deslogarUsuario();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
public void deslogarUsuario(){
try {
autenticacao.signOut();
}catch (Exception e){
e.printStackTrace();
}
}
}
and now the code of Acompanhaentofragment.java
private Button btnteste;
private TextView txtteste;
public AcompanhamentoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_acompanhamento, container, false);
}
what I need is to modify the screen’s components fragment_accompaniment.xml like a textView, but I don’t know how to inflate this screen in the file Acompanhaentofragment.java, because I can’t access these objects, and if I try to access these elements by Menuactivity.java it accuses Nullpoint error.
If I understood what is this chunk of code, missed you return the inflated xml
return acompanhamento;
– Rafael Tavares
But I need to create method to inflate an object? or just instantiating the View object I can already modify the components of my screen?
– LEANDRO DA SILVA
Ah, now that I see that this is a
Activity
... To display the layout onActivity
better create aFragment
, that will have all the logic of this "screen" and the interface (layout) too– Rafael Tavares
Yes yes I saw, the problem is that I cannot use findViewbyId, it does not allow when a fragment is used, without it I cannot access the components of my screen layout. because Activity does appCompatActivity extends, and a fragment does Fragment extends
– LEANDRO DA SILVA
In the
Fragment
you need to usefindViewById
in some view. In general, it would be inview
that theFragment
is inflating:view.findViewById
– Rafael Tavares
I edited my post, inside the fragment_accompaniment.xml I have a button and a textView equal in the previous image, as I will access them by my accompanying fileFragmento.java?
– LEANDRO DA SILVA
Post code instead of image, please. It’s easier for me (or someone else) to respond
– Rafael Tavares
now posted the code, summarizing, I created a menu of type Navigation Dreawer Activity, and created two fragments that corresponds to two menu items, visit and accompaniment, the moment I click on accompaniment it opens the screen fragment_accompaniment.xml, until then beauty, but I need to access the components of this screen and change the text of a textView with one click, but how do I access the components of this screen? is which file? Menuactivity.java or Accompaniment.java? and how do I do this? i see several examples of how to inflate an xml but it doesn’t work
– LEANDRO DA SILVA