Reputation: 8268
I've looked all over the Internet but for some reason couldn't find anyone mention how can we instantiate a toolbar 100% programmatically.
Most of them talk about using the XML. I already know how to do that. But I want to know how to use only code to instantiate it.
I tried doing something like:
Toolbar toolbar = new Toolbar(this);
toolbar.setTitle("This is a title");
setSupportActionBar(toolbar);
but it doesn't seem to show up. This is strange that I can't find a single article discussing this online. Maybe it's not possible? Would appreciate guidance.
Upvotes: 0
Views: 218
Reputation: 20930
Create Toolbar
Programatically this way
Toolbar toolbar = new Toolbar(this);
Toolbar.LayoutParams toolBarParams = new Toolbar.LayoutParams(
Toolbar.LayoutParams.MATCH_PARENT,
R.attr.actionBarSize
);
toolbar.setLayoutParams(toolBarParams);
and then attach to your layout.
Upvotes: 1