I want know if in a method I can add a button programmatically in the right corner of the ToolBar, using the toolbar reference:
mToolbar = (Toolbar) findViewById(R.id.toolbar);
Toolbar is just a ViewGroup . So just as you add views to any ViewGroup programmatically , do the same treatment for toolbar .
Button bt = new Button(this);
bt.setText("A Button");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.RIGHT;
button.setLayoutParams(params);
toolbar.addView(bt);