1

I'm searching for an example that shows how I can implement a clickable textview that starts the Android default search dialog and displays a selected result line.

It should have the same behaviour and design as the search field in the Google Maps action bar on Android (e. g. magnifier glass icon on the left, a "Search" hint if the textview is empty, a click starts the search dialog defined by a searchable entry):

user640688
  • 321
  • 3
  • 10

3 Answers3

1

That is custom EditText with compound drawable

public class SearchEditText extends EditText {

    private boolean mMagnifyingGlassShown = true;
    private Drawable mMagnifyingGlass;

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMagnifyingGlass = getCompoundDrawables()[0];
    }

    /**
     * Conditionally shows a magnifying glass icon on the left side of the text field
     * when the text it empty.
     */
    @Override
    public boolean onPreDraw() {
        boolean emptyText = TextUtils.isEmpty(getText());
        if (mMagnifyingGlassShown != emptyText) {
            mMagnifyingGlassShown = emptyText;
            if (mMagnifyingGlassShown) {
                setCompoundDrawables(mMagnifyingGlass, null, null, null);
            } else {
                setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
        return super.onPreDraw();
    }

And xml

<view
                class="com.tr.search.SearchEditText"
                android:id="@+id/search_src_text"
                android:layout_height="wrap_content"
                android:layout_width="0dip"
                android:layout_weight="1.0"
                android:singleLine="true"
                android:ellipsize="end"
                android:hint="@string/search_bar_hint"
                android:drawableLeft="@drawable/magnifying_glass"
                android:freezesText="true"
            />

Result enter image description here

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
0

Your should look at the official documentation : http://d.android.com/guide/topics/search/search-dialog.html

4e4c52
  • 304
  • 3
  • 14
0

well first you have to make your textView clickable with following code

TextView txtView =findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener(View v){
new OnClickListener() {

            @Override
        public void onClick(View v) {
            //Your Code for calling search ativity
        }
    });

for search activity plz refer here

there is also one example given for search dialog in android sdk if you are using eclipse, then just click on

File->new->android Project->select sdk
->create project from  existing project sample
select searchableDictionary
Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48
  • Thank you for your quick answer. I know the search dialog respectively the new search widget of honeycomb. The question focuses the layout of the textview (like in the Google Maps app), not the handling of the search dialog (e. g. onSearchRequested etc.). How can I implement a text view that holds a hint, has a magnifier glass on the left side the text view (inside) but acts like a button? Are there any resources/styles that can be applied (android.R.*)? Thanks in advance, Thorsten – user640688 Mar 18 '11 at 20:32