Android java add the widget to the view problem
I found the following function that allows you to insert a Widget.
Specifying: createWidget(View view, String packageName, String className)
The problem at the moment is that:
Some widgets have a configuration phase, such as a
WidgetConfigureActivity
.
But it is not called when the widget is inserted into the layout, so some widgets are not inserted correctly.Some widgets, take up little space.
But at the time of insertion take all the space available within the layout.
Any suggestions?
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homeScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" />
</LinearLayout>
code:
public boolean createWidget(View view, String packageName, String className)
// Get the list of installed widgets
AppWidgetProviderInfo newAppWidgetProviderInfo = null;
List<AppWidgetProviderInfo> appWidgetInfos;
appWidgetInfos = mAppWidgetManager.getInstalledProviders();
boolean widgetIsFound = false;
for (int j = 0; j < appWidgetInfos.size(); j++)
if (appWidgetInfos.get(j).provider.getPackageName().equals(packageName) && appWidgetInfos.get(j).provider.getClassName().equals(className))
// Get the full info of the required widget
newAppWidgetProviderInfo = appWidgetInfos.get(j);
widgetIsFound = true;
break;
if (!widgetIsFound)
return false;
else
Log.v("Class", "Ok*");
// Create Widget
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
// Add it to your layout
LinearLayout widgetLayout = findViewById(R.id.mylockscreen);
widgetLayout.addView(hostView);
// And bind widget IDs to make them actually work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);
if (!allowed)
// Request permission - https://stackoverflow.com/a/44351320/1816603
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
final int REQUEST_BIND_WIDGET = 1987;
startActivityForResult(intent, REQUEST_BIND_WIDGET);
return true;
ListWidget:
Add Widget Size:
Real Widget Size:
java android android-layout widget android-widget
add a comment |
I found the following function that allows you to insert a Widget.
Specifying: createWidget(View view, String packageName, String className)
The problem at the moment is that:
Some widgets have a configuration phase, such as a
WidgetConfigureActivity
.
But it is not called when the widget is inserted into the layout, so some widgets are not inserted correctly.Some widgets, take up little space.
But at the time of insertion take all the space available within the layout.
Any suggestions?
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homeScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" />
</LinearLayout>
code:
public boolean createWidget(View view, String packageName, String className)
// Get the list of installed widgets
AppWidgetProviderInfo newAppWidgetProviderInfo = null;
List<AppWidgetProviderInfo> appWidgetInfos;
appWidgetInfos = mAppWidgetManager.getInstalledProviders();
boolean widgetIsFound = false;
for (int j = 0; j < appWidgetInfos.size(); j++)
if (appWidgetInfos.get(j).provider.getPackageName().equals(packageName) && appWidgetInfos.get(j).provider.getClassName().equals(className))
// Get the full info of the required widget
newAppWidgetProviderInfo = appWidgetInfos.get(j);
widgetIsFound = true;
break;
if (!widgetIsFound)
return false;
else
Log.v("Class", "Ok*");
// Create Widget
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
// Add it to your layout
LinearLayout widgetLayout = findViewById(R.id.mylockscreen);
widgetLayout.addView(hostView);
// And bind widget IDs to make them actually work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);
if (!allowed)
// Request permission - https://stackoverflow.com/a/44351320/1816603
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
final int REQUEST_BIND_WIDGET = 1987;
startActivityForResult(intent, REQUEST_BIND_WIDGET);
return true;
ListWidget:
Add Widget Size:
Real Widget Size:
java android android-layout widget android-widget
what are trying to do, "how can I make sure to add the selected widget to a view" describe it, cuz it's incomprehensible
– HeyAlex
Nov 15 '18 at 11:35
@HeyAlex: Let me know if I have explained this well.
– Paul
Nov 15 '18 at 14:11
are you trying to make a launcher?
– HeyAlex
Nov 15 '18 at 15:45
I modified the post by putting the images. I do not know if the correct word, I'm doing some attempts. In this case, the thing I'm trying to do, I have a list of widgets like this: ListWidget That clicking on one of them should be placed in a layout. It works, but as you can see the result of: Add Widget Size The dimensions are not correct, the right ones should be similar to: Real Widget Size. I do not know if I explained myself well.
– Paul
Nov 15 '18 at 16:01
add a comment |
I found the following function that allows you to insert a Widget.
Specifying: createWidget(View view, String packageName, String className)
The problem at the moment is that:
Some widgets have a configuration phase, such as a
WidgetConfigureActivity
.
But it is not called when the widget is inserted into the layout, so some widgets are not inserted correctly.Some widgets, take up little space.
But at the time of insertion take all the space available within the layout.
Any suggestions?
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homeScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" />
</LinearLayout>
code:
public boolean createWidget(View view, String packageName, String className)
// Get the list of installed widgets
AppWidgetProviderInfo newAppWidgetProviderInfo = null;
List<AppWidgetProviderInfo> appWidgetInfos;
appWidgetInfos = mAppWidgetManager.getInstalledProviders();
boolean widgetIsFound = false;
for (int j = 0; j < appWidgetInfos.size(); j++)
if (appWidgetInfos.get(j).provider.getPackageName().equals(packageName) && appWidgetInfos.get(j).provider.getClassName().equals(className))
// Get the full info of the required widget
newAppWidgetProviderInfo = appWidgetInfos.get(j);
widgetIsFound = true;
break;
if (!widgetIsFound)
return false;
else
Log.v("Class", "Ok*");
// Create Widget
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
// Add it to your layout
LinearLayout widgetLayout = findViewById(R.id.mylockscreen);
widgetLayout.addView(hostView);
// And bind widget IDs to make them actually work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);
if (!allowed)
// Request permission - https://stackoverflow.com/a/44351320/1816603
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
final int REQUEST_BIND_WIDGET = 1987;
startActivityForResult(intent, REQUEST_BIND_WIDGET);
return true;
ListWidget:
Add Widget Size:
Real Widget Size:
java android android-layout widget android-widget
I found the following function that allows you to insert a Widget.
Specifying: createWidget(View view, String packageName, String className)
The problem at the moment is that:
Some widgets have a configuration phase, such as a
WidgetConfigureActivity
.
But it is not called when the widget is inserted into the layout, so some widgets are not inserted correctly.Some widgets, take up little space.
But at the time of insertion take all the space available within the layout.
Any suggestions?
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/homeScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" />
</LinearLayout>
code:
public boolean createWidget(View view, String packageName, String className)
// Get the list of installed widgets
AppWidgetProviderInfo newAppWidgetProviderInfo = null;
List<AppWidgetProviderInfo> appWidgetInfos;
appWidgetInfos = mAppWidgetManager.getInstalledProviders();
boolean widgetIsFound = false;
for (int j = 0; j < appWidgetInfos.size(); j++)
if (appWidgetInfos.get(j).provider.getPackageName().equals(packageName) && appWidgetInfos.get(j).provider.getClassName().equals(className))
// Get the full info of the required widget
newAppWidgetProviderInfo = appWidgetInfos.get(j);
widgetIsFound = true;
break;
if (!widgetIsFound)
return false;
else
Log.v("Class", "Ok*");
// Create Widget
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, newAppWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo);
// Add it to your layout
LinearLayout widgetLayout = findViewById(R.id.mylockscreen);
widgetLayout.addView(hostView);
// And bind widget IDs to make them actually work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
boolean allowed = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, newAppWidgetProviderInfo.provider);
if (!allowed)
// Request permission - https://stackoverflow.com/a/44351320/1816603
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, newAppWidgetProviderInfo.provider);
final int REQUEST_BIND_WIDGET = 1987;
startActivityForResult(intent, REQUEST_BIND_WIDGET);
return true;
ListWidget:
Add Widget Size:
Real Widget Size:
java android android-layout widget android-widget
java android android-layout widget android-widget
edited Nov 15 '18 at 15:58
Paul
asked Nov 14 '18 at 23:07
PaulPaul
151210
151210
what are trying to do, "how can I make sure to add the selected widget to a view" describe it, cuz it's incomprehensible
– HeyAlex
Nov 15 '18 at 11:35
@HeyAlex: Let me know if I have explained this well.
– Paul
Nov 15 '18 at 14:11
are you trying to make a launcher?
– HeyAlex
Nov 15 '18 at 15:45
I modified the post by putting the images. I do not know if the correct word, I'm doing some attempts. In this case, the thing I'm trying to do, I have a list of widgets like this: ListWidget That clicking on one of them should be placed in a layout. It works, but as you can see the result of: Add Widget Size The dimensions are not correct, the right ones should be similar to: Real Widget Size. I do not know if I explained myself well.
– Paul
Nov 15 '18 at 16:01
add a comment |
what are trying to do, "how can I make sure to add the selected widget to a view" describe it, cuz it's incomprehensible
– HeyAlex
Nov 15 '18 at 11:35
@HeyAlex: Let me know if I have explained this well.
– Paul
Nov 15 '18 at 14:11
are you trying to make a launcher?
– HeyAlex
Nov 15 '18 at 15:45
I modified the post by putting the images. I do not know if the correct word, I'm doing some attempts. In this case, the thing I'm trying to do, I have a list of widgets like this: ListWidget That clicking on one of them should be placed in a layout. It works, but as you can see the result of: Add Widget Size The dimensions are not correct, the right ones should be similar to: Real Widget Size. I do not know if I explained myself well.
– Paul
Nov 15 '18 at 16:01
what are trying to do, "how can I make sure to add the selected widget to a view" describe it, cuz it's incomprehensible
– HeyAlex
Nov 15 '18 at 11:35
what are trying to do, "how can I make sure to add the selected widget to a view" describe it, cuz it's incomprehensible
– HeyAlex
Nov 15 '18 at 11:35
@HeyAlex: Let me know if I have explained this well.
– Paul
Nov 15 '18 at 14:11
@HeyAlex: Let me know if I have explained this well.
– Paul
Nov 15 '18 at 14:11
are you trying to make a launcher?
– HeyAlex
Nov 15 '18 at 15:45
are you trying to make a launcher?
– HeyAlex
Nov 15 '18 at 15:45
I modified the post by putting the images. I do not know if the correct word, I'm doing some attempts. In this case, the thing I'm trying to do, I have a list of widgets like this: ListWidget That clicking on one of them should be placed in a layout. It works, but as you can see the result of: Add Widget Size The dimensions are not correct, the right ones should be similar to: Real Widget Size. I do not know if I explained myself well.
– Paul
Nov 15 '18 at 16:01
I modified the post by putting the images. I do not know if the correct word, I'm doing some attempts. In this case, the thing I'm trying to do, I have a list of widgets like this: ListWidget That clicking on one of them should be placed in a layout. It works, but as you can see the result of: Add Widget Size The dimensions are not correct, the right ones should be similar to: Real Widget Size. I do not know if I explained myself well.
– Paul
Nov 15 '18 at 16:01
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310090%2fandroid-java-add-the-widget-to-the-view-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310090%2fandroid-java-add-the-widget-to-the-view-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what are trying to do, "how can I make sure to add the selected widget to a view" describe it, cuz it's incomprehensible
– HeyAlex
Nov 15 '18 at 11:35
@HeyAlex: Let me know if I have explained this well.
– Paul
Nov 15 '18 at 14:11
are you trying to make a launcher?
– HeyAlex
Nov 15 '18 at 15:45
I modified the post by putting the images. I do not know if the correct word, I'm doing some attempts. In this case, the thing I'm trying to do, I have a list of widgets like this: ListWidget That clicking on one of them should be placed in a layout. It works, but as you can see the result of: Add Widget Size The dimensions are not correct, the right ones should be similar to: Real Widget Size. I do not know if I explained myself well.
– Paul
Nov 15 '18 at 16:01