2

The following first image is taken from the Material Design specification for the Search pattern. I have just implemented the Custom Search suggestions in my example app by following this guide. But it appears like the second image. That is the first one SearchView+Suggestions list appears like a whole RecyclerView or ListView, but in the second one, the search suggestions list appears to be a different list.

What can I do to look like the first one.

WHAT I WANT:

enter image description here

WHAT I HAVE:

enter image description here

SSCCE CODE:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.mainActivity_textView);
        toolbar = (Toolbar) findViewById(R.id.mainActivity_toolbar);

        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(true);

        handleIntent(getIntent());
    }

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

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

            SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                    MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
            suggestions.saveRecentQuery(query, null);

            doMySearch(query);
        }
    }

    private void doMySearch(String searchQuery) {
        textView.setText(searchQuery); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);

        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

        final ActionMenuView.LayoutParams lp = new ActionMenuView.LayoutParams(ActionMenuView.LayoutParams.MATCH_PARENT, ActionMenuView.LayoutParams.MATCH_PARENT);
        searchView.setLayoutParams(lp);

        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();
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity" >

    <include android:id="@+id/mainActivity_toolbar"
        layout="@layout/app_bar"/>

    <TextView
        android:id="@+id/mainActivity_textView"
        android:layout_below="@id/mainActivity_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary" >


</android.support.v7.widget.Toolbar>

res/values/styles.xml

<resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primaryDark</item>
        <item name="colorAccent">@color/accent</item>
    </style>

    <style name="AppTheme" parent="AppBaseTheme"></style>

</resources>

res/xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint" 
    android:searchSuggestAuthority="practice_projects.materialdesigngooglenowlikesearchviewfive.MySuggestionProvider"
    android:searchSuggestSelection=" ?" >
</searchable>

MySuggestionProvider.java

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
    public final static String AUTHORITY = "practice_projects.materialdesigngooglenowlikesearchviewfive.MySuggestionProvider";
    public final static int MODE = DATABASE_MODE_QUERIES;

    public MySuggestionProvider() {
        setupSuggestions(AUTHORITY, MODE);
    }
}

EDIT 1

enter image description here

Solace
  • 8,612
  • 22
  • 95
  • 183
  • maybe a duplicate of https://stackoverflow.com/questions/27556623/creating-a-searchview-that-looks-like-the-material-design-guidelines ? – Adam Burley Jan 26 '21 at 17:28

1 Answers1

2

Did you take both screenshots from the same device? Android has device compatibility framework so even if the code is the same it will render slightly differently on different devices.

If you have multiple devices do the test there. If not use device emulators with different versions of android and you'll see that the same code will have slightly varying results.

user1610950
  • 1,837
  • 5
  • 33
  • 49
  • You are right, I observed that difference. But I tested it on Android 2.2, 4.1, and Lollipop. The first one is from the [Material design specification.](http://www.google.com/design/spec/patterns/search.html#search-in-app-search) and the second one is from my emulator. – Solace Aug 14 '15 at 11:10
  • 1
    that's what I mean, material design might be implemented in the android compatibility framework but it won't look exactly like material design in the specs just because the older model android OS can't do all that the new one can. Is your emulator running lollipop? – user1610950 Aug 14 '15 at 16:28
  • Initiallly I was trying to run it on different Android emulators in eclipse. Now I tried it with a Genymotion lollipop emulator and my real phone running Android 4.1. This problem is reproduced in every device. Just added a screenshot in the question edit. – Solace Aug 14 '15 at 19:22