Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add editText on action bar

Tags:

java

android

I wonder how to achieve this ?

I have a search icon in action bar as image below

enter image description here

When it is clicked, I want the action bar change to editText and has a search icon beside the editText

I know how to make an editText with an image, but how to put the editText on the action bar like image below ?

enter image description here

like image 640
Hoo Avatar asked Jan 15 '16 15:01

Hoo


2 Answers

What you are looking for is called the SearchView or Search Widget

Check it out here

like image 72
Nick H Avatar answered Sep 29 '22 01:09

Nick H


ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_action_search);

LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);

actionBar.setCustomView(v);

create layout search with your EditText.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:focusable="true" >



    <EditText
        android:id="@+id/search_query"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:background="@drawable/bg_search_edit_text"
        android:imeOptions="actionSearch"
        android:inputType="text" />


</LinearLayout>

Hope it helps.

like image 36
Krishna Avatar answered Sep 28 '22 23:09

Krishna