Reputation: 300
I'm using Android Studio to create an Android Launcher app.
I'm trying to add a feature that allows the user to select an installed widget and place it on my launcher. Similar to what most launchers on the play store provide.
My code is currently is giving me cannot resolve symbol
errors on appWidgetHost
and appWidgetManager
.
I've had a look on SO and Google on how to achieve this but I'm still confused on how to get this to work and how to correct my code.
Here's my code:
package com.example.widgettest;
import android.app.Activity;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
Button widgetButton;
// APPWIDGET_HOST_ID is any number you like
appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetHost = new AppWidgetHost(this, 1);
AppWidgetProviderInfo newAppWidgetProviderInfo = new AppWidgetProviderInfo();
// Get an id
int appWidgetId = appWidgetHost.allocateAppWidgetId();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addwidgetButtonListener();
}
public void createwidget(){
// Get the list of installed widgets
List<AppWidgetProviderInfo> appWidgetInfos = new ArrayList<AppWidgetProviderInfo>();
appWidgetInfos = appWidgetManager.getInstalledProviders();
// Create Widget
AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
// Add it to your layout
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
rl.addView(hostView);
}
public void addwidgetButtonListener() {
widgetButton = (Button) findViewById(R.id.button);
widgetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
createwidget();
}
});
}
}
Upvotes: 2
Views: 1526
Reputation: 1592
One cause of Cannot resolve symbol
would be that you have not declared some variable. In this case, it looks like you forgot to declare appWidgetManager and appWidgetHost.
You can fix this error simply by prefixing the two lines with AppWidgetManager
and AppWidgetHost
respectively. Like so:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
AppWidgetHost appWidgetHost = new AppWidgetHost(this, 1);
On another note, I haven't done any work with the Widget Manager or Widget Host before, but in general anything that takes the Activity context as an argument should be initialized in the onCreate
function. So, if you find that those variables are not being initialized properly while debugging your app, you would want to do something like this instead:
public class MainActivity extends Activity {
...
AppWidgetManager appWidgetManager;
AppWidgetHost appWidgetHost;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetHost = new AppWidgetHost(this, 1);
...
}
Upvotes: 1