Set google as search bar in home screen Custom launcher programmatically

I am creating ‘my own launcher. In that case I want to putQuick search bar` in my home screen i.e. Google now launcher.

How can I do that. I have gone through multiple threads but not found any relevant answer.

  • “This application is available to over 0 devices.”
  • How to Add Smiley/Emojis in Edittext?
  • Android share display on another devices
  • How to update view pager item TITLE dynamically
  • How do I suppress warnings when compiling an android library with gradle?
  • How to define a circle shape in an android xml drawable file?
  • I don’t want to show the widget picker.I want asap user install this launcher search bar should be there.

    Thanks in advance.

    Related posts:

    Get the field value with a Cursor
    How to create an AVD for Android 4.0.3? (Unable to find a 'userdata.img')
    Android : restart application after update - ACTION_PACKAGE_REPLACED
    Remove black background on custom dialog
    Resume playing VideoView from onResume
    How to update/upgrade your Android APK during Staged Rollout in Google Play
  • How to build my own Android SDK
  • How to use su command over adb shell?
  • Is it safe to support only armeabi-v7a for Android 4 and above?
  • CoordinatorLayout layout_anchor does not work
  • animation to transition between views with rotation on z-axis with depth perception
  • Command pattern to pass application's activity methods?
  • 2 Solutions collect form web for “Set google as search bar in home screen Custom launcher programmatically”

    The search bar is not displayed because the launcher doesn’t have permissions to show the widget.

    As you probably already figured out, if a user explicitly adds the search bar widget to any screen, he will be prompted to give the permission, and the search bar on top also appears.

    The problem is that getOrCreateQsbBar method in Launcher class (I assume you forked Launcher3) doesn’t ask for permissions if it can’t instantiate the widget, it instead silently fails. The problem is in this snippet inside of getOrCreateQsbBar:

        if (!AppWidgetManagerCompat.getInstance(this)
                .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
            mAppWidgetHost.deleteAppWidgetId(widgetId);
            widgetId = -1;
        }
    

    Instead of just resetting widgetId to -1 you want to ask for permissions to install the widget and call getOrCreateQsbBar again. Here’s an example code that asks for permission:

        boolean hasPermission = appWidgetManager.bindAppWidgetIdIfAllowed(widgetId, searchProvider);
        if (!hasPermission)
        {
            mAppWidgetHost.deleteAppWidgetId(widgetId);
            widgetId = -1;
    
            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, searchProvider);
            startActivityForResult(intent, REQUEST_BIND);
        }
    

    Then overload onActivityResult in the Launcher class, something along the lines of:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_BIND) {
            mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
        } 
    }
    

    I really recommend you to clone Launcher3 repository from AOSP.

    https://android.googlesource.com/platform/packages/apps/Launcher3/

    If you checkout nougat-mr5 branch you can look at the code to show searchbar by taking the component from GMS package and including it as a row element. Please look for QSB in the code.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.