Change screen (Toolboard button)

0

I have the following part of my code:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { 
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.barra_superior, menu);  // toolboard
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.search:
                openSearch();
                return true;
            case R.id.add:
                openAdd();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

I created a menu and by clicking the add button

It should be something like this:

 private  void openAdd() {

        final Button buttonAdd = (Button) findViewById(R.id.add);
           buttonAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), inserir_Llibre.class);
                startActivityForResult(intent, 0);

            }
        });

    }

But it does not work for me, I've only changed screens with buttons, and I see that the toolboard works differently and I do not know how to make it work.

Do you help me?

thanks!

    
asked by Montse Mkd 17.10.2018 в 12:39
source

2 answers

2

I think you want it to be that openAdd() open activity inserir_Llibre.class .

In that case, and assuming that this is a Activity because the openAdd() method is part of Activity the method would be:

private  void openAdd() {
            Intent intent = new Intent(this, inserir_Llibre.class);
            startActivityForResult(intent, 0);

}

If there is another button in the user interface that allows you to do the same then, in onCreate() , yes you set the OnClickListener , but call the same function to not duplicate code.

On onCreate ():

...
final Button botonAddDeLayout= (Button) findViewById(R.id.botonAddDeLayout);
botonAddDeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openAdd();
        }
 });
...
    
answered by 17.10.2018 / 15:22
source
0

You must not create the listener within the "click" action of your button , this is incorrect since when you want to use the button listener (buttonAdd) it will not work if you have not previously called the method openAdd() .

private  void openAdd() {

        final Button buttonAdd = (Button) findViewById(R.id.add);
           buttonAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), inserir_Llibre.class);
                startActivityForResult(intent, 0);

            }
        });

    }

The solution is to add only the action within the method openAdd()

private  void openAdd() {
   //*Acción!
   Intent intent = new Intent(v.getContext(), inserir_Llibre.class);
   startActivityForResult(intent, 0);            
}

and in onCreate() of your Activity define your listener after setContentView() :

private Button buttonAdd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    ...
       buttonAdd = (Button) findViewById(R.id.add);
       buttonAdd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           //*Llama método!               
           openAdd();
        }
    });
   ...
   ...
}
    
answered by 17.10.2018 в 16:33