Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add EditText to Toolbar

My Toolbar and EditText look like this:

enter image description here

I tried adding it as an ActionView but I got this result:

enter image description here

It basically just added my words 'Tap Below' as a title without the TextView

Here's what I did:

menu.xml

<menu>
    ... 
    ...
    <item
        android:id="@+id/edit_text_menu"
        android:orderInCategory="300"
        android:title="Tap Below"
        android:actionLayout="@layout/edit_text_layout"
        app:showAsAction="ifRoom|collapseActionView"
        />
</menu>

edit_text_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myEditText"
    android:background="#000000">

</EditText>

I thought by doing this I add my EditText layout to the Toolbar... Can someone please help?

Alternatively, is there a trick where I can overlay the EditText on the Toolbar?

I tried adding <item name="windowActionModeOverlay">true</item> but it didn't do anything.

like image 709
user2456977 Avatar asked May 01 '15 01:05

user2456977


People also ask

How do I add a search bar to my toolbar?

Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How do I add text in EditText?

You'd need editText. setText(editText. getText() + "string"); .


2 Answers

Just add the EditText to the XML for your ToolBar. That is probably the easiest way.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
      <EditText
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/myEditText"
         android:background="#000000" />
</android.support.v7.widget.Toolbar>
like image 173
Jared Burrows Avatar answered Oct 24 '22 08:10

Jared Burrows


  1. Add the EditText to the Toolbar xml.

  2. Add the Toolbar to your Activity's xml layout, at the top.

  3. Set the toolbar as ActionBar:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    

This will make the toolbar "become" the ActionBar with EditText inside.

like image 6
onosendai Avatar answered Oct 24 '22 08:10

onosendai