<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eclipsed4utoo&#039;s Blog&#187; 2.0</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/2-0/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsed4utoo.com/blog</link>
	<description>Not Your Ordinary Programmer</description>
	<lastBuildDate>Thu, 08 Sep 2011 17:09:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Android 2.0 &#8211; Changing Widget Layout Dynamically</title>
		<link>http://eclipsed4utoo.com/blog/android-20-changing-widget-layout-dynamically/</link>
		<comments>http://eclipsed4utoo.com/blog/android-20-changing-widget-layout-dynamically/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 18:47:55 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[2.0]]></category>
		<category><![CDATA[MotoTorch LED]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=232</guid>
		<description><![CDATA[I recently wrote my first application for Android, and it was a great learning experience.  One of the more &#8220;advanced&#8221; tasks that I wanted to do was to make the widget icon change depending on whether the LEDs were on or not.  I also wanted to allow the user to be able to choose which [...]]]></description>
			<content:encoded><![CDATA[<p>I recently wrote my<a href="http://eclipsed4utoo.com/blog/mototorch-led-flashlight-android-application/" target="_blank"> first application for Android</a>, and it was a great learning experience.  One of the more &#8220;advanced&#8221; tasks that I wanted to do was to make the widget icon change depending on whether the LEDs were on or not.  I also wanted to allow the user to be able to choose which icons they wanted the widget to use.</p>
<p>So I started out using one layout XML file with multiple ImageViews, and was just showing/hiding and changing the source to the image that I wanted to show.  I started thinking that this seemed to be very convoluted and that there must be an easier way.</p>
<p>I decided to figure out how to create multiple layout XML files and load those dynamically.  This would allow me to have each icon in it&#8217;s own separate XML file so any changes that needed to be made to them would only affect that icon and not the other icons.</p>
<p>First, I created the separate layout XML files.  I am just going to show two that I did.</p>
<p><strong>widget_flame_with_border_off.xml</strong></p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout android:id=&quot;@+id/main&quot;
   android:gravity=&quot;center&quot;
   android:background=&quot;@drawable/widget_frame_portrait1x1_black&quot;
   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
   android:layout_height=&quot;74dip&quot;
   android:layout_width=&quot;74dip&quot;&gt;

   &lt;LinearLayout
      android:id=&quot;@+id/btn_led&quot;
      android:layout_width=&quot;52dip&quot;
      android:layout_height=&quot;52dip&quot;
      android:background=&quot;@drawable/appwidget_button_center&quot;
      android:clickable=&quot;true&quot;
      android:focusable=&quot;true&quot;
      android:gravity=&quot;center&quot;&gt;

      &lt;ImageView
          android:id=&quot;@+id/img_led&quot;
          android:layout_height=&quot;40dip&quot;
          android:layout_gravity=&quot;center&quot;
          android:layout_width=&quot;40dip&quot;
          android:src=&quot;@drawable/mototorch_led_off&quot;
          android:scaleType=&quot;fitXY&quot; /&gt;

   &lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
</pre>
<p><strong>widget_flame_with_border_on.xml</strong></p>
<pre class="brush: xml;">
&lt;pre&gt;&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout android:id=&quot;@+id/main&quot;
   android:gravity=&quot;center&quot;
   android:background=&quot;@drawable/widget_frame_portrait1x1_black&quot;
   xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
   android:layout_height=&quot;74dip&quot;
   android:layout_width=&quot;74dip&quot;&gt;

   &lt;LinearLayout
      android:id=&quot;@+id/btn_led&quot;
      android:layout_width=&quot;52dip&quot;
      android:layout_height=&quot;52dip&quot;
      android:background=&quot;@drawable/appwidget_button_center&quot;
      android:clickable=&quot;true&quot;
      android:focusable=&quot;true&quot;
      android:gravity=&quot;center&quot;&gt;

      &lt;ImageView
          android:id=&quot;@+id/img_led&quot;
          android:layout_height=&quot;40dip&quot;
          android:layout_gravity=&quot;center&quot;
          android:layout_width=&quot;40dip&quot;
          android:src=&quot;@drawable/mototorch_led_on&quot;
          android:scaleType=&quot;fitXY&quot; /&gt;

   &lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
</pre>
<p>With those two layout XML files created, I can now load them dynamically.</p>
<pre class="brush: java;">
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
     RemoteViews views = null;

     int layoutID = 0;

     if (ledIsOn){
         layoutID = R.layout.widget_flame_with_border_on;
     }
     else {
         layoutID = R.layout.widget_flame_with_border_off;
     }

     // the layoutID that is passed in the constructor of the
     //   RemoteViews object is the layout that will be loaded
     //   when the widget is updated.
     views = new RemoteViews(context.getPackageName(), layoutID);

     for (int i = 0; i &lt; appWidgetIds.length; i++) {
          appWidgetManager.updateAppWidget(appWidgetIds[i], views);
     }
}
</pre>
<p><span style="font-family: Verdana,sans-serif; font-size: 14px; line-height: 23px;">In the constructor for the RemoteViews object, you pass in the layout ID of the layout that you want to use. So simply changing this to a different layout than is currently being used, you can change the layout completely.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/android-20-changing-widget-layout-dynamically/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

