miqqo
miqqo

Reputation: 233

How to properly add custom view to the toolbar?

I am using toolbar with extended height (56dp + 80dp) and want to add EditText to the bottom of the toolbar. The problem I have is that EditText DOES NOT expands itself to the right edge, like in picture below:

enter image description here

The code looks like below:

toolbar_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Title"
    android:singleLine="true" />

Adding layout to toolbar:

LayoutInflater inflater = LayoutInflater.from(mActivity.getActionBarToolbar().getContext());
    mToolbarLayout = (EditText) inflater.inflate(R.layout.toolbar_edit_text, null);
    Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.BOTTOM;
    mActivity.getActionBarToolbar().addView(mToolbarLayout, layoutParams);

Upvotes: 18

Views: 11893

Answers (2)

mdelolmo
mdelolmo

Reputation: 6447

I believe the toolbar behaves more or less like a LinearLayout, even when it doesn't extend it.

If I'm right, you wouldn't be able to use "two rows" like you intend.

Maybe you can take that EditView out of the toolbar, or alternatively, use ActionBar (With the 'X' icon and the actions) and below a toolbar with the EditText.

Upvotes: 3

wichnator
wichnator

Reputation: 91

You can add a custom layout with EditText below the Toolbar, with the same background color.

Upvotes: -2

Related Questions