Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 575
miqqo Avatar asked Oct 26 '14 13:10

miqqo


People also ask

Can we customize toolbar?

You can make other changes like font, size, color, etc in custom_toolbar. xml file. If you want to change the text of custom toolbar, you can do in this way: ....

How do I add menu button to my toolbar?

This is going to be in res/menu/main_menu . Right click the res folder and choose New > Android Resource File. Type main_menu for the File name. Choose Menu for the Resource type.


1 Answers

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.

like image 57
mdelolmo Avatar answered Oct 14 '22 17:10

mdelolmo