Xamarin.Android ile widget oluşturma, Uygulamaları cihazımıza yüklediğimizde uygulamayı çalıştırmadan hızlı olarak ihtiyacımızı gidermek için widget’ları kullanıyoruz bizde bu makalede android widget nasıl geliştirebileceğimize gözatacağız ve bazı püfnoktaları sizle paylaşacağım.
Xamarin.Android ile widget oluşturma
widget_message.xml(XML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" style="@style/WidgetBackground"> <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dip" android:padding="10dip" android:gravity="center" android:text="@string/widget_loading" style="@style/Text.Loading" /> </LinearLayout> |
widget_word.xml(XML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" style="@style/WidgetBackground"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/star_logo" /> <TextView android:id="@+id/blog_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="14dip" android:layout_marginBottom="1dip" android:includeFontPadding="false" android:singleLine="true" android:ellipsize="end" style="@style/Text.BlogTitle" /> <TextView android:id="@+id/bullet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/blog_title" android:paddingRight="4dip" android:includeFontPadding="false" android:singleLine="true" style="@style/BulletPoint" /> <TextView android:id="@+id/creator" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/blog_title" android:layout_toRightOf="@id/bullet" android:paddingRight="5dip" android:paddingBottom="4dip" android:includeFontPadding="false" android:lineSpacingMultiplier="0.9" android:maxLines="4" android:fadingEdge="vertical" style="@style/Text.Creator" /> </RelativeLayout> |
strings.xml(XML)
1 2 3 4 5 6 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MHG Widget Örneği</string> <string name="widget_name">MHG Widget Örneği</string> <string name="widget_loading">Son makale yükleniyor\nlütfen bekleyiniz...</string> </resources> |
styles.xml(XML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?xml version="1.0" encoding="utf-8"?> <resources> <style name="WidgetBackground"> <item name="android:background">@drawable/widget_bg</item> </style> <style name="BulletPoint"> <item name="android:textSize">13sp</item> <item name="android:textColor">@android:color/black</item> </style> <style name="Text" /> <style name="Text.Loading"> <item name="android:textSize">14sp</item> <item name="android:textColor">@android:color/black</item> </style> <style name="Text.BlogTitle"> <item name="android:textSize">16sp</item> <item name="android:textStyle">bold</item> <item name="android:textColor">@android:color/black</item> </style> <style name="Text.WordType"> <item name="android:textSize">14sp</item> <item name="android:textStyle">italic</item> <item name="android:textColor">@android:color/black</item> </style> <style name="Text.Creator"> <item name="android:textSize">13sp</item> <item name="android:textColor">@android:color/black</item> </style> </resources> |
widget_word.xml(XML)
1 2 3 4 5 6 7 | <?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="250dip" android:minHeight="56dip" android:updatePeriodMillis="86400000" android:previewImage="@drawable/icon" android:initialLayout="@layout/widget_message" /> |
BlogPost.cs(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; using System.Xml.Linq; namespace MHG.Widget { class BlogPost { public string Title { get; set; } public string Creator { get; set; } public string Link { get; set; } public BlogPost () { Title = string.Empty; Creator = string.Empty; Link = string.Empty; } public static BlogPost GetBlogPost () { var entry = new BlogPost (); try { string url = "http://www.muratoner.net/feed"; XDocument doc = XDocument.Load (url); XElement latest = doc.Root.Element ("channel").Element ("item"); entry.Title = latest.Element ("title").Value; entry.Creator = latest.Element ("description").Value; entry.Link = latest.Element ("link").Value; } catch (Exception ex) { entry.Title = "Error"; entry.Creator = string.Format("{0}-{1}", ex.Message, ex.StackTrace); } return entry; } } } |
WordWidget.cs(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using Android.App; using Android.Appwidget; using Android.Content; namespace MHG.Widget { [BroadcastReceiver (Label = "@string/widget_name")] [IntentFilter (new string [] { "android.appwidget.action.APPWIDGET_UPDATE" })] [MetaData ("android.appwidget.provider", Resource = "@xml/widget_word")] public class WordWidget : AppWidgetProvider { public override void OnUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // To prevent any ANR timeouts, we perform the update in a service context.StartService (new Intent (context, typeof (UpdateService))); } } } |
UpdateService.cs(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | using Android.App; using Android.Appwidget; using Android.Content; using Android.OS; using Android.Widget; namespace MHG.Widget { [Service] public class UpdateService : Service { public override void OnStart (Intent intent, int startId) { // Build the widget update for today RemoteViews updateViews = buildUpdate (this); // Push update for this widget to the home screen ComponentName thisWidget = new ComponentName (this, Java.Lang.Class.FromType (typeof (WordWidget)).Name); AppWidgetManager manager = AppWidgetManager.GetInstance (this); manager.UpdateAppWidget (thisWidget, updateViews); } public override IBinder OnBind (Intent intent) { // We don't need to bind to this service return null; } // Build a widget update to show the current Wiktionary // "Word of the day." Will block until the online API returns. public RemoteViews buildUpdate (Context context) { var entry = BlogPost.GetBlogPost (); // Build an update that holds the updated widget contents var updateViews = new RemoteViews (context.PackageName, Resource.Layout.widget_word); updateViews.SetTextViewText (Resource.Id.blog_title, entry.Title); updateViews.SetTextViewText (Resource.Id.creator, entry.Creator); // When user clicks on widget, launch to Wiktionary definition page if (!string.IsNullOrEmpty (entry.Link)) { Intent defineIntent = new Intent (Intent.ActionView, Android.Net.Uri.Parse (entry.Link)); PendingIntent pendingIntent = PendingIntent.GetActivity (context, 0, defineIntent, 0); updateViews.SetOnClickPendingIntent (Resource.Id.widget, pendingIntent); } return updateViews; } } } |
Makaledeki örneğimize ve xamarin ile hazırlanmış diğer tüm örneklere altta sizinle paylaştığım github hesabımdan erişebilirsiniz.
GitHub: https://github.com/muratoner/xamarin/tree/master/MHG.Widget
Hocam xamarin form(cross platform) ile hazırladığımız uygulamamız için geçerli mi? yani sadece android tarafına widget ekleyebilir miyiz?
Evet xamarin forms’da android tarafında geçerlidir.