Dynamically adding a View to an android widget

I am programmically creating a view at runtime and I want to this view to be added to my linearlayout dynamically at runtime. Here is the code I got:

public class Widget extends AppWidgetProvider {

@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.main); DemoView dv = new DemoView(context); // Stuck here...How do I add my new dv View to my android widget appWidgetManager.updateAppWidget(appWidgetIds, updateViews); super.onUpdate(context, appWidgetManager, appWidgetIds); } private class DemoView extends View { public DemoView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint LedColor = new Paint(); Paint BlankColor = new Paint(); Path p,p1; Matrix m = new Matrix(); LedColor.setStyle(Paint.Style.FILL); LedColor.setColor(0xffffffff); BlankColor.setStyle(Paint.Style.FILL); BlankColor.setColor(0xff111111); DigitPaths dp = new DigitPaths(); p = dp.GetDigitPath(-1); p1 = dp.GetDigitPath(5); p.offset(50, 50); p1.offset(50, 50); m.setScale(6.5f, 6.5f); p.transform(m); p1.transform(m); canvas.drawPath(p,BlankColor); canvas.drawPath(p1, LedColor); } }


Any help would be appreciated!

-------------Problems Reply------------

It's not possible to add a custom View to an app widget. See the "Creating the App Widget Layout" section of the App Widgets Dev Guide for what View types are allowed.

Android 3.0 adds support for some views to display collections. See the "Using App Widgets with Collections" section for details.

Otherwise, to dynamically add an allowed View to an App Widget, after inflating the RemoteViews and getting a reference to it, you can use its addView(View) method, or the addView(View) method on any of the View objects already in the RemoteViews.

As Bruce correctly answered, it is not possible to use a custom view. Since it seems like your view doesn't respond to input, and just draws, you can achieve the same functionality with an ImageView. Just create a Bitmap, create a Canvas with it, and then draw on it using what was your onDraw code. Then create an ImageView (which is allowed in a widget) and set the image it displays to your Bitmap.

Category:android Views:0 Time:2011-06-12

Related post

  • Dynamically adding inflated views to ViewFlipper in Android causes NullPointerException 2011-11-28

    I'm getting NullPointerException when I'm trying to programmatically add inflated views to my ViewFlipper: -- onCreate setContentView(R.layout.event_report); final LayoutInflater inflater = getLayoutInflater(); mFlipper = (ViewFlipper) findViewById(R

  • unobtrusive validation on dynamically added partial view (not working) 2012-02-17

    I know this subject's been discussed many times, but I have tried everything I found for over the past week and nothing seems to work for me. I am currently facing a problem, as the subject suggests, with validation after dynamically adding content.

  • Dynamically added partial view with DatePicker not working as expected 2014-08-15

    I have a Partial view (_SubView) which is having multiple controls <script type="text/javascript"> $(document).ready(function () { $(".datepicker").datepicker({ showOn: 'both', dateFormat: 'dd-MM-yy', buttonImage: "@Url.Content("~/Content/Image

  • Memory leaks on dynamically added Text View on Android 2012-04-03

    I have LinearLayout in which i dinamically add and remove TextView. To remove them i just call .removeAllViews(); This off course causes a huge memory leak. Is their any way i release the memory or it's just an architectural design flaw on my side. p

  • dynamically adding two views one below other 2012-02-24

    I want to add two views one after the other, I used this way but I am getting an error. This is my XML. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="m

  • Can I dynamically add a View to home screen widget? 2010-03-05

    I am writing a home screen widget. Is it possible to add a View, e.g. ImageView, to a home screen widget through RemoteViews? I want to generate the views for the home screen widget dynamically. Thanks. updateViews = new RemoteViews(context.getPackag

  • dynamically adding a view to activity layout 2010-08-18

    I have a custom view (an extension of a TextView) that I want to dynamically add to my Layout (don't want to include it in the main.xml file). The book says to fetch the RelativeLayout using findViewById() in my java code then create a new instance o

  • Dynamically adding items to listview in android 2012-02-15

    I am making an android application that needs to add items to a dynamic ListView. How can I do this? I wish to add the item once the "AddItemToListViewButton" is pressed. I am trying to use the code below, but however, it's not working. How can I fix

  • dynamically adding smaller Views on a large zoomable ImageView? 2012-04-11

    i have a large zooming, panning ImageView containing an image as it's bitmap. i want to add many smaller Views on top of it at the point where the user performs a LongClick action on it. these Views have to be positioned at the fixed location and mus

  • WRAP_CONTENT not working after dynamically adding views 2014-01-07

    I'm trying to create a fragment that lays out a series of custom views dynamically. The main content for this layout is a RelativeLayout nested in a LinearLayout (to center it horizontally), nested in a ScrollView. The RelativeLayout has a few TextVi

  • dynamic number of gui elements in Android? 2011-04-13

    I want to create a gui application for android where the user will be able to add or remove fields of certain type (4 different type of fields) to the application. Is there a way to do so in xml? The only way I could figure to do so is by edditing th

  • Dynamically adding textviews to layout and adjusting accordingly 2012-04-26

    I'm adding TextViews dynamically with code. Now I'm using a LinearLayout with horizontal orientation which i thought would add a new line when the textviews doesnt fit into the row. And how wrong I was. Thing is I don't know how many rows there will

  • Android-widget : Change app widget background on the fly 2010-12-22

    I have been trying to change the background color of the app widget on the fly for sometime now, with no success. I had found one solution (http://stackoverflow.com/questions/2726295/android-changing-linearlayout-in-a-widget) which we have been using

  • How do i create the above view in my android application? 2011-04-28

    Because i'm create an app similar to what was shown below but i have no idea how to create a view similar to what was shown above after i click a buttton to navigate to this page where all the video and map log file is shown.. I'm kinna new in androi

  • Refresh Grid View Dyanmically in Android 2011-06-24

    I have a problem that I am showing images in grid view which is parsed from Url, I have designed a refine search dialog to refine the search in grid view but the problem is that parsing works fine but Grid view does not refreshed next time when I pro

  • error dynamically populate the Expandable list in Android 2011-08-08

    I am trying to populate an expandable list with my items and on the subitem(children) i have errors. I get a NullPointerException at com.android.app.resources.PersonalizedExpandableListAdapter.getChildView(PersonalizedExpandableListAdapter.java:106)

  • Error inflating class android.widget.RelativeLayout 2015-01-19

    anyone knows why i get this error? I wanted to make a customList for my ListFragment inside my tabbed activity. Here are all my classes: ListViewDemoAdapter: public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> { public ListViewD

  • Android Access Dynamically Added View 2011-12-20

    My problem is accessing dynamically added component. If we assign id's of views via xml (such as android:id="@+id/xyz", we can reach this view by findViewById(R.id.xyz). However, I want to assign id dynamically, because I will use a ListView or a Tab

  • Dynamically adding a custom view to RemoteViews 2011-03-25

    Could any help me to do this? My code is like: public CustomClass extends View { //uses ondraw() to do something } For displaying my custom view on the home screen I created a class to extend Broadcast Receiver: public class customAppWidgetProvider e

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.171 (s). 11 q(s)