Robin Rodricks
Robin Rodricks

Reputation: 113946

Create and configure an Android Toolbar fully in code

I need to dynamically create a Toolbar inside in activity. Due to design reasons I cannot create it in the AXML and must create it fully programatically. Sadly most of the advanced design properties do not have coded equivalents.

How can I configure a toolbar purely in code, to match the following AXML? Please note I'm using a Support.V7.Widget.Toolbar so that my app is backward compatible with Android 5.0.

 <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
   android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    />

Method 1 : Create then Configure

My current code:

var toolbar = new Toolbar(this);
toolbar.Title = "View Options";

// the following properties exist but are only GETTERS! .. how do I change them?
toolbar.Width = "parent_width";
toolbar.Height = "wrap_content";

// the following properties don't exist.. what do I do?
toolbar.MinHeight = "?aatr/actionBarSize";
toolbar.Background = "?attr/colorPrimary";
toolbar.Theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar";
toolbar.PopupTheme = "@style/ThemeOverlay.AppCompat.Light";

Method 2 : Use Attributes

Alternatively I can pass "attributes" to the Toolbar at the time of construction, but there is no way to "create" an IAttributeSet or do change attributes within a set!

I need to create the PINK toolbar in the following image:

toolbar

Edit: Sorry, my question is different from this which is only about creating views. I need to create a toolbar in a view with a lot of more properties.

Upvotes: 0

Views: 165

Answers (1)

Mohammed Abuiriban
Mohammed Abuiriban

Reputation: 500

The first Thing to do is to create a style for the toolbar inside styles.xml

<style name="toolbarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:background">#ff4081</item>
        <item name="android:paddingLeft">16dp</item>
</style>

Then you have to add a reference to it inside attrs.xml

<resources>
    <attr name="toolbarTheme" format="reference"/>
</resources>

Then inisde your app main theme you have to link the attr with the style inside styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="@attr/toolbarTheme">@style/toolbarTheme</item>
</style>

Let's create the toolbar

// specify theme [ specify theme in attrs.xml ]
Toolbar toolbar = new Toolbar(this,null,R.attr.toolbarTheme);
// first params => width
// second params => Height
Toolbar.LayoutParams params = new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                150);
toolbar.setLayoutParams(params);

// reference to root view ( RelativeLayout )
RelativeLayout view = (RelativeLayout) findViewById(R.id.activity_main);
// add the toolbar
view.addView(toolbar, 0); // 0 means the top you should change it.

You can set the popupTheme using the method setPopupTheme()

You can set the background color using the method setBackgroundColor()

Upvotes: 1

Related Questions