How to access and change objects within an XML file by Activity?

Asked

Viewed 184 times

-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;

  • 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?

  • Ah, now that I see that this is a Activity... To display the layout on Activity better create a Fragment, that will have all the logic of this "screen" and the interface (layout) too

  • 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

  • In the Fragment you need to use findViewById in some view. In general, it would be in view that the Fragment is inflating: view.findViewById

  • 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?

  • Post code instead of image, please. It’s easier for me (or someone else) to respond

  • 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

Show 3 more comments

1 answer

1


By which file to access the elements?

To access the components of a layout.xml, you must access by whom inflated the .xml, that in your case is being the AcompanhamentoFragment.java.

How to access the elements?

To access an element from a Activity just use the findViewById (the element will be searched in the Layout you used setContentView), as you already do in your Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    // Procurar pelo elemento no layout R.layout.activity_menu
    Toolbar toolbar = findViewById(R.id.toolbar);
}

In the Fragment is very similar, you need to indicate in which View you are looking for this element. Follow example:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_acompanhamento, container, false);

    // Procurar pelo elemento no "view", que é o R.layout.fragment_acompanhamento
    Button meuBotao = view.findViewById(R.id.buttonteste);
    TextView meuTextView = view.findViewById(R.id.textViewteste);

    return view;
}

From that moment you can already make the changes in the interface you want, put a Listener on the button etc.

  • Show, thank you very much, now it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.