开发者

Android clickable TextView with hint and image to start search dialog and present selected result

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 searchab开发者_运维百科le entry):


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

Android clickable TextView with hint and image to start search dialog and present selected result


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜