How to Implement Floating SearchWidget Android

怎甘沉沦 提交于 2019-12-05 14:54:49
Amaresh Jana

In my case I have used like this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add("Search").setIcon(android.R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
            if(item.getTitle().toString().equalsIgnoreCase("Search")){
        showSearchDialog();
    }
    return true;
}

private void showSearchDialog() {
    // TODO Auto-generated method stub
    dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_search);
    final Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    findDialogViews(dialog);
    dialog.show();
}

private void findDialogViews(final Dialog dialog) {
    // TODO Auto-generated method stub
    ImageView iv = (ImageView)dialog.findViewById(R.id.calc_clear_txt_Prise);
    lvSuggestions = (ListView)dialog.findViewById(R.id.listView1);
    lvSuggestions.setOnItemClickListener((android.widget.AdapterView.OnItemClickListener) this);
    final EditText etSearch = (EditText)dialog.findViewById(R.id.calc_txt_Prise);
    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(etSearch.getText().toString().isEmpty()){
                dialog.dismiss();
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
            else
                etSearch.setText("");
        }
    });
    etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            **DO YOUR STUFF**
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });
}

SearchAdapter code:

public class SearchAdapter extends BaseAdapter{

    private Context mContext;
    private List<SearchModel> list;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    public SearchAdapter(Context mContext, List<SearchModel> list) {
        // TODO Auto-generated constructor stub
        this.mContext = mContext;
        this.list = list;
        mRequestQueue = Volley.newRequestQueue(mContext);
        mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache());
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_search   , parent, false);
        }
        NetworkImageView image = (NetworkImageView)convertView.findViewById(R.id.imageView1);
        TextView tvName = (TextView)convertView.findViewById(R.id.textView1);

        SearchModel model = list.get(position);
        image.setImageUrl(model.getImage(), mImageLoader);
        tvName.setText(model.getVName());

        return convertView;
    }
}

SearchAdapter is to set the list...of that drop down.....

dialog_search layout xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/calc_txt_Prise"
        android:layout_width="fill_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/white"
        android:hint="Search Turf"
        android:inputType="text"
        android:paddingLeft="20dp"
        android:singleLine="true"
        android:textColor="@color/text_color"
        android:textColorHint="@android:color/darker_gray"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/calc_clear_txt_Prise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/action_settings"
        android:src="@android:drawable/ic_menu_close_clear_cancel" />
</FrameLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/text_color" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/text_color"
    android:dividerHeight="1dp" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/text_color" />

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!