Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change numColumns in GridView in Widget (RemoteViews) has no effect

I have a widget(which works) with a GridView, which shows information in 1 or more columns/rows. I want to set the number of columns programmatically, cause the users shall choose. If I set the numColumns inside the Layout-XML to "1", it works fine. If I try to set the numColumns in the following way, it has no effect:

    rViews.setInt(R.id.duration_view, "setNumColumns", 1);

Layout looks like this:

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/duration_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="64dp"
        android:verticalSpacing="0dp"
        android:columnWidth="192dp"
        android:numColumns="auto_fit"
    />

My widget onUpdate() method, using the RemoteAdapter:

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // ....
        Intent intent = new Intent(context, ViewFlipperWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        rViews.setRemoteAdapter(R.id.duration_view, intent);
        rViews.setEmptyView(R.id.duration_view, R.id.empty_view);

        // This doesnt have any effect...:-(
        rViews.setInt(R.id.duration_view, "setNumColumns", 1); 

        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.duration_view);            

        AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, rViews);

        // ....
        super.onUpdate(context, appWidgetManager, appWidgetIds);

Not only the setNumColumns has no effect, other method calls as well. What I'm doing wrong?

like image 244
user1013443 Avatar asked Dec 05 '12 14:12

user1013443


People also ask

Is GridView deprecated?

Both GridView and GridLayout are safe for use in production code. They are obsolete, and I would not recommend that anyone use them, but they are safe.

How use GridView inside column flutter?

You just need to put your GridView Widget into the Expanded Widget, for example: body: new Column( children: <Widget>[ new Expanded( child: GridView. count( // Create a grid with 2 columns. If you change the scrollDirection to // horizontal, this would produce 2 rows.

Which attribute is used to define the number of columns to be added to the GridView?

android:numColumns: This attribute of GridView will be used to decide the number of columns that are to be displayed in Grid. android:horizontalSpacing: This attribute is used to define the spacing between two columns of GridView.


1 Answers

The only methods you can call against a RemoteViews view are those with the @android.view.RemotableViewMethod annotation. There are only three such methods for GridView:

@android.view.RemotableViewMethod
public void setRemoteViewsAdapter(Intent intent) {

@android.view.RemotableViewMethod
public void smoothScrollToPosition(int position) {

@android.view.RemotableViewMethod
public void smoothScrollByOffset(int offset) {

You can verify this yourself:

https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/GridView.java

like image 108
j__m Avatar answered Sep 27 '22 23:09

j__m