1

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

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.