How to Implement Floating SearchWidget Android

I am trying to implement a search widget in current android apps, but just can't seem to do it Google I / O app, and I couldn't implement it. Below is my code

MainActivity

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search_bar).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    } else if (id == R.id.search_bar) {
        //startActivity(new Intent(this, SearchResultActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

      

MenuItem

 <item android:id="@+id/search_bar"
    android:title="Search"
    android:orderInCategory="100"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="always|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

      

SearchActivity

 public class SearchResultActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_custom_text);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
    }
}
}

      

manifesto

<activity android:name=".MainActivity"
        android:enabled="true">

        <!--<meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>-->

        <meta-data android:name="android.app.default_searchable"
            android:value=".SearchResultActivity"/>

    </activity>

    <activity android:name=".SearchResultActivity"
        android:enabled="true"
        android:launchMode="singleTop"
        android:parentActivityName=".MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <!--<meta-data android:name="android.app.default_searchable"
            android:value=".SearchResultActivity" />-->

        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>

    </activity>

      

This is what I think, but I have not tried it yet. In my project, I have implemented a floating action. From below, I think it is a floating action. Any help would be appreciated.

+3


source to share


1 answer


In my case, I used this like

@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
        }
    });
}

      

Search 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 - set list ... from this dropdown list.



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" />

      

Rendered view

+3


source







All Articles