3

I am a new stackoverflow registered user but have been using it for a long time now. Getting to the point : I am creating an android launcher and want to implement appWidgets. For testing, I am using the com.android.quicksearchbox widget and adding it to the top of the screen. Problem is, I am using AppWidgetHostView and it seems not to notice any click or touch events the user does. Can anybody tell me why ? Here is all my code for appWidgets (in the onCreate(Bundle) function) :

    android.appwidget.AppWidgetManager appWidgetManager = android.appwidget.AppWidgetManager.getInstance(this);
    android.appwidget.AppWidgetHost appWidgetHost = new android.appwidget.AppWidgetHost(this, 0);
    android.appwidget.AppWidgetProviderInfo newAppWidgetProviderInfo = new android.appwidget.AppWidgetProviderInfo();


    int appWidgetId = appWidgetHost.allocateAppWidgetId();


    List<android.appwidget.AppWidgetProviderInfo> appWidgetInfos = new ArrayList<android.appwidget.AppWidgetProviderInfo>();
    appWidgetInfos = appWidgetManager.getInstalledProviders();

    for(int j = 0; j < appWidgetInfos.size(); j++)
    {
        if (appWidgetInfos.get(j).provider.getPackageName().equals("com.android.quicksearchbox"))
        {

            newAppWidgetProviderInfo = appWidgetInfos.get(j);
            break;
        }
     }



    android.appwidget.AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, newAppWidgetProviderInfo);
    hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);


    android.widget.LinearLayout ll = (android.widget.LinearLayout) findViewById(R.id.loll);
    ll.addView(hostView, 0);

loll is my LinearLayout.

So anybody knows how can I enable events ? Or if there is another method of adding app-widgets to my launcher ?

Thank you.

user1779764
  • 91
  • 2
  • 5

1 Answers1

0

Use FrameLayout then add a simple View as overlap to handle the click event

Layout:

<layout>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/container"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:gravity="center">


        <View
            android:id="@+id/overlap"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            android:focusable="true" />
    </FrameLayout>
</layout>

Code:

// make sure AppWidgetHostView in bottom layer
AppWidgetHostView.z = -1f
binding.container.addView(AppWidgetHostView, width,height)
binding.overlap.tag = item
binding.overlap.setOnClickListener {
                val widget = it.tag as Widget
                val intent = Intent(context, widget.getConfigActivity())
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget.id.toInt())
                context.startActivity(intent)
            }
GreatC
  • 342
  • 4
  • 9