<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2631617199049210138</id><updated>2017-09-17T13:26:55.418+03:00</updated><category term="intermediate as3 tutorial"/><category term="Flex 4"/><category term="Adobe AIR"/><category term="SQL"/><category term="OOP in AS3"/><category term="beginner as3 tutorial"/><category term="Making Kirpad step-by-step"/><category term="Android beginner tutorial"/><category term="Making KirSQLite step-by-step"/><category term="Making KirAnnotator step-by-step"/><category term="AS3 and components"/><category term="Making Kirshot step-by-step"/><category term="AS3 and XML"/><category term="Making KirSizer step-by-step"/><category term="Flash and HTML"/><category term="blog related"/><category term="Physics"/><title type='text'>Code for Food</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>986</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-5839295797888751713</id><published>2013-08-24T14:43:00.001+03:00</published><updated>2013-08-24T14:43:19.321+03:00</updated><title type='text'>AS3 Luxe Preloader pack</title><content type='html'>&lt;a href=&quot;http://activeden.net/item/as3-luxe-preloader-15-pack/5427364&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-HRw4a0QhK4M/UhicKWuXUtI/AAAAAAAABx4/A1Vf_UCSHAY/s1600/luxe.png&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Customizable and easy to use AS3 preloader component.&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://activeden.net/item/as3-luxe-preloader-15-pack/5427364&quot;&gt;Visit Activeden for more info and a live example!&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Have fun!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/5839295797888751713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/08/as3-luxe-preloader-pack.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/5839295797888751713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/5839295797888751713'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/08/as3-luxe-preloader-pack.html' title='AS3 Luxe Preloader pack'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-HRw4a0QhK4M/UhicKWuXUtI/AAAAAAAABx4/A1Vf_UCSHAY/s72-c/luxe.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-8836156539671300226</id><published>2013-08-21T21:08:00.002+03:00</published><updated>2013-08-21T21:08:30.780+03:00</updated><title type='text'>Flash AS3 Brush Drawing application - source code</title><content type='html'>&lt;a href=&quot;http://activeden.net/item/as3-brush-drawing-application/5421861&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.s3.envato.com/files/64569084/preview.jpg&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Highly customizable and very smooth AS3 Brush Drawing application source code. Now available for just $8.&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://activeden.net/item/as3-brush-drawing-application/5421861&quot;&gt;Visit Activeden for more info and a live example!&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Have fun!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/8836156539671300226/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/08/flash-as3-brush-drawing-application.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8836156539671300226'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8836156539671300226'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/08/flash-as3-brush-drawing-application.html' title='Flash AS3 Brush Drawing application - source code'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-4738274321209844794</id><published>2013-08-20T18:40:00.000+03:00</published><updated>2013-08-20T18:40:34.650+03:00</updated><title type='text'>Flash AS3 Platform Jump game engine - soure code</title><content type='html'>&lt;a href=&quot;http://activeden.net/item/as3-platform-jump-game-engine/5415414&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://0.s3.envato.com/files/64491703/preview.jpg&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Highly customizable and ridiculously easy to implement game engine for platform jumping game genre. Now available for just $8.&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://activeden.net/item/as3-platform-jump-game-engine/5415414&quot;&gt;Visit Activeden for more info and a live example!&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Have fun!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/4738274321209844794/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/08/flash-as3-platform-jump-game-engine.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/4738274321209844794'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/4738274321209844794'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/08/flash-as3-platform-jump-game-engine.html' title='Flash AS3 Platform Jump game engine - soure code'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-8644625862974224729</id><published>2013-07-22T17:24:00.001+03:00</published><updated>2013-07-22T17:24:26.010+03:00</updated><title type='text'>Flash AS3 Match 3 game engine - source code</title><content type='html'>&lt;img border=&quot;0&quot; src=&quot;http://3.s3.envato.com/files/61925501/preview.jpg&quot; /&gt;&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Ever wanted to make a game like Bejeweled and other similar Match 3 genre-type games? Well, now you can.&lt;br /&gt;&lt;br /&gt;With my engine you&#39;ll be able to easily tweak the settings to create that jewel matching game you ever wanted in minutes. If you&#39;re a skilled coder, you&#39;ll find it helpful that the whole code is commented and explained in the documentation, so you can edit this powerful engine to make it even more customized.&lt;br /&gt;&lt;br /&gt;Take a look at the &lt;a href=&quot;http://activeden.net/item/match-3-game-engine/5199380&quot;&gt;official Activeden page&lt;/a&gt; for more info, and if you think it&#39;s what you need - you can get it for just 10 bucks.&lt;br /&gt;&lt;br /&gt;Have fun!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/8644625862974224729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/07/flash-as3-match-3-game-engine-source.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8644625862974224729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8644625862974224729'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/07/flash-as3-match-3-game-engine-source.html' title='Flash AS3 Match 3 game engine - source code'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-2729331077409994410</id><published>2013-03-01T21:24:00.002+02:00</published><updated>2013-03-01T21:30:13.352+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="blog related"/><title type='text'>Hiatus</title><content type='html'>Today my blog is going on a (temporary?)&amp;nbsp;hiatus. Read more for details.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;For over 2 and a half years I have been updating the site daily with new handwritten tutorials on ActionScript 3, Flex, Adobe AIR, Android SDK and a few other technologies. It has been a good run, but, unfortunately, today it comes to an end.&lt;br /&gt;&lt;br /&gt;This means that the blog will not be updated daily anymore. I don&#39;t know when or what I will post here, but I still have a few things to talk about on Android so chances are I&#39;m still going to be publishing tutorials here. Just not every day.&lt;br /&gt;&lt;br /&gt;When I start learning a new language in the future, I might begin posting here daily again. But it is unknown when will that happen.&lt;br /&gt;&lt;br /&gt;Now, here are some statistics that the site has gotten over the 2 and a half years of daily updates:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;985 posts&lt;/li&gt;&lt;li&gt;602,380 all-time hits&lt;/li&gt;&lt;li&gt;74 followers&lt;/li&gt;&lt;li&gt;650 comments&lt;/li&gt;&lt;li&gt;average daily pageviews: over 1000&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Thanks to everyone who has followed my blog and read my tutorials! It is truly great to know that my work actually helped some people.&lt;br /&gt;&lt;br /&gt;See you later!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/2729331077409994410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/03/hiatus.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/2729331077409994410'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/2729331077409994410'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/03/hiatus.html' title='Hiatus'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-242404413093036657</id><published>2013-02-28T10:00:00.000+02:00</published><updated>2013-02-28T10:00:06.709+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 96 - Frame Animation using code</title><content type='html'>In this tutorial we will learn how to create a Frame Animation using Java code instead of an XML file.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;We&#39;ll be using the same 3 drawable resources (images) as frames in this tutorial as we did last time. But instead of creating the sequence in an XML file, we&#39;ll do that in the MainActivity.java class of our project.&lt;br /&gt;&lt;br /&gt;The acitivty_main.xml layout file remains like this:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&lt;br /&gt;    android:layout_width=&quot;match_parent&quot;&lt;br /&gt;    android:layout_height=&quot;match_parent&quot;&lt;br /&gt;    android:orientation=&quot;vertical&quot;&lt;br /&gt;    tools:context=&quot;.MainActivity&quot;&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;LinearLayout android:layout_width=&quot;match_parent&quot;&lt;br /&gt;        android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;        &amp;gt;&lt;br /&gt;        &amp;lt;Button android:text=&quot;Start&quot;&lt;br /&gt;            android:layout_width=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_weight=&quot;1&quot;&lt;br /&gt;            android:id=&quot;@+id/btn_start&quot;&lt;br /&gt;            /&amp;gt;&lt;br /&gt;        &amp;lt;Button android:text=&quot;Stop&quot;&lt;br /&gt;            android:layout_width=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_weight=&quot;1&quot;&lt;br /&gt;            android:id=&quot;@+id/btn_stop&quot;&lt;br /&gt;            /&amp;gt;&lt;br /&gt;    &amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;ImageView&lt;br /&gt;        android:id=&quot;@+id/image&quot;&lt;br /&gt;        android:layout_width=&quot;match_parent&quot;&lt;br /&gt;        android:layout_height=&quot;match_parent&quot;&lt;br /&gt;        /&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;In MainActivity class, we first declare an AnimationDrawable and ImageView instances:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;AnimationDrawable animation;&lt;br /&gt;ImageView image;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;We then add 2 clicks listeners to the two buttons on the screen. When the Start button is pressed, create 3 BitmapDrawable instances that refer to the 3 drawable resources we have. Then set the value of the animation object to a new AnimationDrawable() instance, set its oneShot property to false using setOneShot() method. Add each frame to it using addFrame() method, which has 2 parameters - reference to the drawable used in this frame and the duration of the frame.&lt;br /&gt;&lt;br /&gt;Then we set the content of the image to this animation using setImageDrawable() method and start the animation after making it visible using the setVisible() method:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;final Button btnStart = (Button)findViewById(R.id.btn_start);&lt;br /&gt;btnStart.setOnClickListener(new View.OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample1);&lt;br /&gt;BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample2);&lt;br /&gt;BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample3);&lt;br /&gt;&lt;br /&gt;animation = new AnimationDrawable();&lt;br /&gt;animation.setOneShot(false);&lt;br /&gt;animation.addFrame(frame1, 500);&lt;br /&gt;animation.addFrame(frame2, 500);&lt;br /&gt;animation.addFrame(frame3, 500);&lt;br /&gt;&lt;br /&gt;image = (ImageView)findViewById(R.id.image);&lt;br /&gt;image.setImageDrawable(animation);&lt;br /&gt;animation.setVisible(true, true);&lt;br /&gt;animation.start();&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;When the Stop button is pressed, stop the animation:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;final Button btnStop = (Button)findViewById(R.id.btn_stop);&lt;br /&gt;btnStop.setOnClickListener(new View.OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;animation.stop();&lt;br /&gt;animation.setVisible(false, false);&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Full code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.graphics.drawable.AnimationDrawable;&lt;br /&gt;import android.graphics.drawable.BitmapDrawable;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.view.View;&lt;br /&gt;import android.widget.Button;&lt;br /&gt;import android.widget.ImageView;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;AnimationDrawable animation;&lt;br /&gt;ImageView image;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;final Button btnStart = (Button)findViewById(R.id.btn_start);&lt;br /&gt;btnStart.setOnClickListener(new View.OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample1);&lt;br /&gt;BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample2);&lt;br /&gt;BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample3);&lt;br /&gt;&lt;br /&gt;animation = new AnimationDrawable();&lt;br /&gt;animation.setOneShot(false);&lt;br /&gt;animation.addFrame(frame1, 500);&lt;br /&gt;animation.addFrame(frame2, 500);&lt;br /&gt;animation.addFrame(frame3, 500);&lt;br /&gt;&lt;br /&gt;image = (ImageView)findViewById(R.id.image);&lt;br /&gt;image.setImageDrawable(animation);&lt;br /&gt;animation.setVisible(true, true);&lt;br /&gt;animation.start();&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;final Button btnStop = (Button)findViewById(R.id.btn_stop);&lt;br /&gt;btnStop.setOnClickListener(new View.OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;animation.stop();&lt;br /&gt;animation.setVisible(false, false);&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/242404413093036657/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-96-frame.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/242404413093036657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/242404413093036657'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-96-frame.html' title='Android beginner tutorial: Part 96 - Frame Animation using code'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-8382040818841892020</id><published>2013-02-27T10:00:00.000+02:00</published><updated>2013-02-27T10:00:00.305+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 95 - Frame Animation</title><content type='html'>In this tutorial we will learn about Frame Animation.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;A Frame Animation is a classic type of animation where the animation consists of multiple still images displayed in order.&lt;br /&gt;&lt;br /&gt;To make a Frame animation in Android, you need to create an xml file with animation-list root node, containing item nodes, which have 2 attributes - drawable and duration. The drawable attribute is set to a drawable resource and represents the image to display, the duration value is the time in milliseconds that this item is displayed for.&lt;br /&gt;&lt;br /&gt;Let&#39;s create such xml file now. Go to res/drawable/ directory and add a new xml file. Call it frame_anim.xml. Here are the contents:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;animation-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:oneshot=&quot;false&quot;&amp;gt;&lt;br /&gt;    &amp;lt;item android:drawable=&quot;@drawable/sample1&quot;&lt;br /&gt;        android:duration=&quot;300&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;item android:drawable=&quot;@drawable/sample2&quot;&lt;br /&gt;        android:duration=&quot;300&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;item android:drawable=&quot;@drawable/sample3&quot;&lt;br /&gt;        android:duration=&quot;300&quot; /&amp;gt;&lt;br /&gt;&amp;lt;/animation-list&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to activity_main.xml layout file of your project. Add 2 buttons - labeled Start and Stop, as well as an ImageView.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&lt;br /&gt;    android:layout_width=&quot;match_parent&quot;&lt;br /&gt;    android:layout_height=&quot;match_parent&quot;&lt;br /&gt;    android:orientation=&quot;vertical&quot;&lt;br /&gt;    tools:context=&quot;.MainActivity&quot;&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;LinearLayout android:layout_width=&quot;match_parent&quot;&lt;br /&gt;        android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;        &amp;gt;&lt;br /&gt;        &amp;lt;Button android:text=&quot;Start&quot;&lt;br /&gt;            android:layout_width=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_weight=&quot;1&quot;&lt;br /&gt;            android:id=&quot;@+id/btn_start&quot;&lt;br /&gt;            /&amp;gt;&lt;br /&gt;        &amp;lt;Button android:text=&quot;Stop&quot;&lt;br /&gt;            android:layout_width=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;            android:layout_weight=&quot;1&quot;&lt;br /&gt;            android:id=&quot;@+id/btn_stop&quot;&lt;br /&gt;            /&amp;gt;&lt;br /&gt;    &amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;ImageView&lt;br /&gt;        android:id=&quot;@+id/image&quot;&lt;br /&gt;        android:layout_width=&quot;wrap_content&quot;&lt;br /&gt;        android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;        /&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Go to the MainActivity.java class now. In the onCreate() function, declare an ImageView instance, which is a reference to the ImageView we have in our layout. Set its background to the frame_anim drawable using setBackgroundResource() method. Then declare an AnimationDrawable instance, set its value to the background of our image, which was set to the frame_anim drawable. Add click listeners for the two buttons, use the animation&#39;s start() and stop() methods to start and stop animation:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.graphics.drawable.AnimationDrawable;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.view.View;&lt;br /&gt;import android.widget.Button;&lt;br /&gt;import android.widget.ImageView;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;ImageView image = (ImageView)findViewById(R.id.image);&lt;br /&gt;image.setBackgroundResource(R.drawable.frame_anim);&lt;br /&gt;final AnimationDrawable animation = (AnimationDrawable)image.getBackground();&lt;br /&gt;&lt;br /&gt;final Button btnStart = (Button)findViewById(R.id.btn_start);&lt;br /&gt;btnStart.setOnClickListener(new View.OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;animation.start();&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;final Button btnStop = (Button)findViewById(R.id.btn_stop);&lt;br /&gt;btnStop.setOnClickListener(new View.OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;animation.stop();&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Easy as that!&lt;br /&gt;&lt;br /&gt;Test your application and you&#39;ll see a frame animation that can be started and stopped using the provided buttons.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/8382040818841892020/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-95-frame.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8382040818841892020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8382040818841892020'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-95-frame.html' title='Android beginner tutorial: Part 95 - Frame Animation'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-7688289121021250343</id><published>2013-02-26T10:00:00.000+02:00</published><updated>2013-02-26T10:00:06.538+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 94 - Applying XML animations</title><content type='html'>In this tutorial we&#39;ll make a demo application that applies scale, rotate and translate animations to a shape on the screen.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;First of all we need to create the animation XML files.&lt;br /&gt;&lt;br /&gt;Go to the res/anim/ directory of your project. The first file we&#39;ll add is rotate.xml:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;set xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    android:shareInterpolator=&quot;false&quot;&amp;gt;&lt;br /&gt;    &amp;lt;rotate android:fromDegrees=&quot;0&quot;&lt;br /&gt;        android:toDegrees=&quot;360&quot;&lt;br /&gt;        android:pivotX=&quot;50%&quot;&lt;br /&gt;        android:pivotY=&quot;50%&quot;&lt;br /&gt;        android:startOffset=&quot;0&quot;&lt;br /&gt;        android:duration=&quot;3000&quot;/&amp;gt;&lt;br /&gt;&amp;lt;/set&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Then goes scale.xml:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;set xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    android:shareInterpolator=&quot;false&quot;&amp;gt;&lt;br /&gt;    &amp;lt;scale android:fromXScale=&quot;1&quot;&lt;br /&gt;        android:fromYScale=&quot;1&quot;&lt;br /&gt;        android:toXScale=&quot;0.5&quot;&lt;br /&gt;        android:toYScale=&quot;0.5&quot;&lt;br /&gt;        android:pivotX=&quot;50%&quot;&lt;br /&gt;        android:pivotY=&quot;50%&quot;&lt;br /&gt;        android:startOffset=&quot;0&quot;&lt;br /&gt;        android:duration=&quot;1500&quot;/&amp;gt;&lt;br /&gt;&amp;lt;/set&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Then we add translate.xml animation, which is a set containing two translate animations that are executed in order:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;set xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    android:shareInterpolator=&quot;false&quot;&amp;gt;&lt;br /&gt;    &amp;lt;translate android:toYDelta=&quot;-100&quot;&lt;br /&gt;        android:startOffset=&quot;0&quot;&lt;br /&gt;        android:duration=&quot;1500&quot;/&amp;gt;&lt;br /&gt;    &amp;lt;translate android:toYDelta=&quot;100&quot;&lt;br /&gt;        android:startOffset=&quot;1500&quot;&lt;br /&gt;        android:duration=&quot;1500&quot;/&amp;gt;&lt;br /&gt;&amp;lt;/set&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to activity_main.xml file. Add an ImageView there, set its layout_width and layout_height values to wrap_content. Set the gravity of the parent to center.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&lt;br /&gt;    android:layout_width=&quot;match_parent&quot;&lt;br /&gt;    android:layout_height=&quot;match_parent&quot;&lt;br /&gt;    android:orientation=&quot;vertical&quot;&lt;br /&gt;    tools:context=&quot;.MainActivity&quot;&lt;br /&gt;    android:gravity=&quot;center&quot;&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;ImageView&lt;br /&gt;        android:id=&quot;@+id/image&quot;&lt;br /&gt;        android:layout_width=&quot;wrap_content&quot;&lt;br /&gt;        android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;        /&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to MainActivity.java class. First we declare the image and draw a red square onto it:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;private ImageView image;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;image = (ImageView)findViewById(R.id.image);&lt;br /&gt;ShapeDrawable shape = new ShapeDrawable(new RectShape());&lt;br /&gt;shape.setIntrinsicHeight(100);&lt;br /&gt;shape.setIntrinsicWidth(100);&lt;br /&gt;shape.getPaint().setColor(Color.RED);&lt;br /&gt;image.setImageDrawable(shape);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Add an onCreateOptionsMenu() function, which adds 3 buttons to the menu. Set the IDs of the menu items to the IDs of the animations:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;@Override&lt;br /&gt;public boolean onCreateOptionsMenu(Menu menu){&lt;br /&gt;menu.add(Menu.NONE, R.anim.rotate, Menu.NONE, &quot;Rotate&quot;);&lt;br /&gt;menu.add(Menu.NONE, R.anim.scale, Menu.NONE, &quot;Scale&quot;);&lt;br /&gt;menu.add(Menu.NONE, R.anim.translate, Menu.NONE, &quot;Translate&quot;);&lt;br /&gt;return(super.onCreateOptionsMenu(menu));&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;In the onOptionsItemSelected() function, we get the animation out of the selected item using the id. Then we apply it to the image using startAnimation() method:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;@Override&lt;br /&gt;public boolean onOptionsItemSelected(MenuItem item){&lt;br /&gt;Animation animation = AnimationUtils.loadAnimation(this, item.getItemId());&lt;br /&gt;image.startAnimation(animation);&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Simple as that. Full code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.graphics.Color;&lt;br /&gt;import android.graphics.drawable.ShapeDrawable;&lt;br /&gt;import android.graphics.drawable.shapes.RectShape;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.view.Menu;&lt;br /&gt;import android.view.MenuItem;&lt;br /&gt;import android.view.animation.Animation;&lt;br /&gt;import android.view.animation.AnimationUtils;&lt;br /&gt;import android.widget.ImageView;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;private ImageView image;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;image = (ImageView)findViewById(R.id.image);&lt;br /&gt;ShapeDrawable shape = new ShapeDrawable(new RectShape());&lt;br /&gt;shape.setIntrinsicHeight(100);&lt;br /&gt;shape.setIntrinsicWidth(100);&lt;br /&gt;shape.getPaint().setColor(Color.RED);&lt;br /&gt;image.setImageDrawable(shape);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onCreateOptionsMenu(Menu menu){&lt;br /&gt;menu.add(Menu.NONE, R.anim.rotate, Menu.NONE, &quot;Rotate&quot;);&lt;br /&gt;menu.add(Menu.NONE, R.anim.scale, Menu.NONE, &quot;Scale&quot;);&lt;br /&gt;menu.add(Menu.NONE, R.anim.translate, Menu.NONE, &quot;Translate&quot;);&lt;br /&gt;return(super.onCreateOptionsMenu(menu));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onOptionsItemSelected(MenuItem item){&lt;br /&gt;Animation animation = AnimationUtils.loadAnimation(this, item.getItemId());&lt;br /&gt;image.startAnimation(animation);&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;That&#39;s all for today.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/7688289121021250343/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-94.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/7688289121021250343'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/7688289121021250343'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-94.html' title='Android beginner tutorial: Part 94 - Applying XML animations'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-980008790900943201</id><published>2013-02-25T10:00:00.000+02:00</published><updated>2013-02-25T10:00:05.708+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 93 - XML animation files</title><content type='html'>In this tutorial we&#39;ll learn about the structure of XML files that are used for defining animations in Android.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;The XML files for animation are put into the res/anim/ directory of the Android project. The file has to have a single root node, which can be either of the possible elements: &lt;set&gt;, &lt;alpha&gt;, &lt;scale&gt;, &lt;translate&gt; or &lt;rotate&gt;.&lt;br /&gt;&lt;br /&gt;The &lt;set&gt; node can act as a container for all of the elements, including other &lt;set&gt; elements.&lt;br /&gt;&lt;br /&gt;Even though the elements are put in a sequence, the animations play at the same time. But you can use the startOffset property to set delay before each animation start.&lt;br /&gt;&lt;br /&gt;There are attributes that can be applied to all the listed elements, as well as attributes that can only be applied to a specific type of animation element.&lt;br /&gt;&lt;br /&gt;First, let&#39;s take a look at the global ones - duration, startOffset, fillBefore, fillAfter, repeatCount, repeatMode, zAdjustment, interpolator.&lt;br /&gt;&lt;br /&gt;The duration value is basically the duration time of the animation in milliseconds.&lt;br /&gt;&lt;br /&gt;The startOffset value is the time delay before the animation starts.&lt;br /&gt;&lt;br /&gt;The fillBefore is a boolean value, which, if set to true, makes sure that the animation transformation is applied before the beginning of the animation (during the startOffset period). &lt;br /&gt;&lt;br /&gt;The fillAfter is a boolean value, which, if set to true, makes sure that the animation transformation is applied after the animation ends.&lt;br /&gt;&lt;br /&gt;The repeatCount value determines how many times the animation repeats.&lt;br /&gt;&lt;br /&gt;The repeatMode can be set to &quot;restart&quot; or &quot;reverse&quot; to make the animation start over or play in reverse when it ends.&lt;br /&gt;&lt;br /&gt;The zAdjustment value sets the adjustment of the Z ordering (depth) of the content. Can be set to &quot;normal&quot; to be treated normally and kept in its current Z order, to &quot;top&quot; to put it on top of everything else and &quot;bottom&quot; to put it behind everything else.&lt;br /&gt;&lt;br /&gt;The interpolator value holds a reference to an interpolator, which is an animation modifier that can affect the rate of change in your animation. There is a number of pre-defined interpolators that you can use, or you can create custom ones. Example: @android:anim/accelerate_interpolator.&lt;br /&gt;&lt;br /&gt;Now let&#39;s take a look at the exclusive attributes.&lt;br /&gt;&lt;br /&gt;The set container supports a shareInterpolator attribute, which can be used for setting one interpolator for use by all the children of the set.&lt;br /&gt;&lt;br /&gt;The alpha element has 2 attributes - fromAlpha and toAlpha, which determine the initial and destination alpha values for the object. Values range from 0 to 1.&lt;br /&gt;&lt;br /&gt;The scale element is used for scaling objects. It has 6 new attributes - fromXScale, toXScale, fromYScale, toYScale, pivotX and pivotY. The first four simply determine the initial and destination scaling values on both axes. The pivotX and pivotY determine the coordinates of the anchor position, relative to which the scaling will ocur. This is similar to registration point in DisplayObjects in Flash.&lt;br /&gt;&lt;br /&gt;The translate element lets us move an object across the screen. Possible new attributes are fromXDelta, toXDelta, fromYDelta and toYDelta. Possible values can be in 3 formats - an absolute numeric value, a relative value in percentages (from -100% to 100%) and value in percentages relative to the size of the object&#39;s parent (-100%p to 100%p).&lt;br /&gt;&lt;br /&gt;The rotate element is used for rotating objects and has 4 attributes - fromDegrees, toDegrees, pivotX, pivotY. The first two determine the initial and destination rotation degrees, the second pair detemine the coordinates of the anchor.&lt;br /&gt;&lt;br /&gt;That&#39;s all for today.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/980008790900943201/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-93-xml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/980008790900943201'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/980008790900943201'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-93-xml.html' title='Android beginner tutorial: Part 93 - XML animation files'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-8715794039152578988</id><published>2013-02-24T10:00:00.000+02:00</published><updated>2013-02-24T10:00:02.018+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 92 - Animation</title><content type='html'>Today we&#39;ll begin learning about Animations in Android.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;In Android, just like in Flash, there are two animation types - Tween Animation and Frame Animation.&lt;br /&gt;&lt;br /&gt;Any View in Android can be transformed in more than one ways - you can move, rotate, resize and change the alpha channel of them.&lt;br /&gt;&lt;br /&gt;The animation transformations are all provided by android.view.animationpackages package. The main animation classes are AnimationSet, AlphaAnimation, RotateAnimation, ScaleAnimation and TranslateAnimation.&lt;br /&gt;&lt;br /&gt;The AnimationSet class is used to group together other animations that are supposed to be played at the same time. If there are any properties set to an AnimationSet class, all the animations that belong to this set also inherit the properties.&lt;br /&gt;&lt;br /&gt;The AlphaAnimation class is used for animations that change the alpha of the object. The RotateAnimation, ScaleAnimation and TranslateAnimation classes are used for rotating, scaling, and translating (moving) objects.&lt;br /&gt;&lt;br /&gt;All animation transformations can be executed in a sequence or at once. Each transformation can have a set of parameters that are suitable only for its type of animation as well as global parameters that can be applied to all types of animations.&lt;br /&gt;&lt;br /&gt;For example, all animations have start time as well as animation length time that you can set. If you want two animations to start at the same time, you set their start time to the same value.&lt;br /&gt;&lt;br /&gt;Sequence of these animation commands can be created in Java or in an XML file, which might sound weird and inconvenient at first (especially if you are used to animations in Flash), but it&#39;s actually pretty easy to use. Moreover, it&#39;s really flexible, as by creating one animation XML file you can reuse it multiple times. Animating using an XML file is even more preferred than programmatically.&lt;br /&gt;&lt;br /&gt;We&#39;ll take a closer look at these XML files used for animating in the next part.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/8715794039152578988/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-92.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8715794039152578988'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8715794039152578988'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-92.html' title='Android beginner tutorial: Part 92 - Animation'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-2288290403418881609</id><published>2013-02-23T10:00:00.000+02:00</published><updated>2013-02-23T10:00:00.876+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 91 - Drawing on Canvas</title><content type='html'>Today we&#39;ll see how to draw on Canvas in Android.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;As I mentioned before, drawing on Canvas is usually done in performance intensive applications like video games.&lt;br /&gt;&lt;br /&gt;There are 2 ways to draw things on a canvas in Android - the first one is to draw on a View, the second way is to draw on a SurfaceView.&lt;br /&gt;&lt;br /&gt;Drawing on a View is more suitable when your application does not need to update the screen really fast. It can be used for a game of chess or a similar slow-paced application. The Android system provides a Canvas object that will display our graphics in a View.&lt;br /&gt;&lt;br /&gt;To use this method, you need to create a custom class that extends View and has the onDraw() callback method. The onDraw() method is called by Android to tell your View to update its graphics. This is where you can put all the code that draws things using the Canvas type object that is passed as the parameter to this function. The class has a set of methods for drawing that you can use, which include drawBitmap(), drawRect(), drawText() and others.&lt;br /&gt;&lt;br /&gt;Note, however, that Android will only call onDraw() of your View if it is necessary. If you decide your application is ready to be drawn, you need to call the View&#39;s invalidate() method. This tells Android that the contents should be redrawn. However, using this method does not guarantee that the redraw will occur instantly.&lt;br /&gt;&lt;br /&gt;What&#39;s interesting is that other classes also have draw() method, which lets you take those Drawables and pass them onto the Canvas to be drawn there.&lt;br /&gt;&lt;br /&gt;The other way to draw on a canvas is using the SurfaceView class.&lt;br /&gt;&lt;br /&gt;This subclass of View lets us create a dedicated drawing surface inside a View, which is treated just like any other View in the application. This surface is offered a separate thread where all the drawing happens, so our main application thread does not hold it back from updating.&lt;br /&gt;&lt;br /&gt;In this case you will also need to create a custom class. This time it extends SurfaceView. It should also implement SurfaceHolder.Callback class. This subclass is used to inform you when the underlying Surface dispatches an event, for example, when it is created or changed.&lt;br /&gt;&lt;br /&gt;The secondary thread should be defined inside your SurfaceView class. You shouldn&#39;t handle the Surface object directly - it can be done using SurfaceHandler. When the SurfaceView object is initialized, you can access the SurfaceHolder object using getHolder() method. Then you tell SurfaceHolder that you want to receive SurfaceHolder callbacks from SurfaceHolder.Callback by calling addCallback(this).&lt;br /&gt;&lt;br /&gt;You can then override all the SurfaceHolder.Callback methods inside your SurfaceView class. Those methods are surfaceChanged, surfaceCreated and surfaceDestroyed.&lt;br /&gt;&lt;br /&gt;To draw to the SurfaceCanvas from withing your secondary thread, pass the SurfaceHandler object to the thread and retrieve the Canvas with lockCanvas(). You can then take and use the Canvas to draw on it. When you&#39;re done drawing, call unlockCanvasAndPost(), passing it your Canvas object. The Surface then draws the Canvas. You need to lock and unlock every time you want to update the screen.&lt;br /&gt;&lt;br /&gt;That&#39;s all for today. We haven&#39;t written a single line of code today but hopefully you now understand the basic mechanism behind drawing graphics in Android. The code itself is pretty self explanatory and will be shown in future tutorials as practical examples.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/2288290403418881609/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-91.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/2288290403418881609'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/2288290403418881609'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-91.html' title='Android beginner tutorial: Part 91 - Drawing on Canvas'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-5450529037352790699</id><published>2013-02-22T10:00:00.000+02:00</published><updated>2013-02-22T10:00:08.921+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 90 - Path and ArcShape</title><content type='html'>In this tutorial we will learn about drawing primitive shapes in Android using Path and ArcShape classes.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;The Path class is similar in usage to the way shapes are drawn using AS3. It has the moveTo() and lineTo() methods that you&#39;ve probably worked with before if you ever tried drawing graphics using Actionscript3.&lt;br /&gt;&lt;br /&gt;Using this class it is possible to draw unordinary primitive shapes, like, for example, a star. &lt;br /&gt;&lt;br /&gt;First we set up the path for the lines to follow:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;Path p = new Path();&lt;br /&gt;p.moveTo(50, 0);&lt;br /&gt;p.lineTo(25, 100);&lt;br /&gt;p.lineTo(100, 50);&lt;br /&gt;p.lineTo(0, 50);&lt;br /&gt;p.lineTo(75, 100);&lt;br /&gt;p.lineTo(50, 0);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;You can then apply the drawing to a ShapeDrawable object.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;ShapeDrawable shape = new ShapeDrawable(new PathShape(p, 100, 100));&lt;br /&gt;shape.setIntrinsicHeight(100);&lt;br /&gt;shape.setIntrinsicWidth(100);&lt;br /&gt;shape.getPaint().setColor(Color.RED);&lt;br /&gt;shape.getPaint().setStyle(Paint.Style.STROKE);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://1.bp.blogspot.com/-TquDT1fxgDI/URu-uHn9JII/AAAAAAAABIs/8tFQZA0eS84/s1600/a_star.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://1.bp.blogspot.com/-TquDT1fxgDI/URu-uHn9JII/AAAAAAAABIs/8tFQZA0eS84/s1600/a_star.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Next class is ArcShape. This one lets us draw arcs, basically. There are two values you need to pass to the ArcShape constructor - float startAngle and float sweepAngle.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;ShapeDrawable shape = new ShapeDrawable(new ArcShape(0, 250));&lt;br /&gt;shape.setIntrinsicHeight(100);&lt;br /&gt;shape.setIntrinsicWidth(100);&lt;br /&gt;shape.getPaint().setColor(Color.RED);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;http://2.bp.blogspot.com/-nmYQBoR8Kso/URu_GLAZDfI/AAAAAAAABI8/w_QqMRpWUSI/s1600/a_arc.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://2.bp.blogspot.com/-nmYQBoR8Kso/URu_GLAZDfI/AAAAAAAABI8/w_QqMRpWUSI/s1600/a_arc.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Now we&#39;ve covered all the primitive shape drawing classes.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/5450529037352790699/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-90-path.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/5450529037352790699'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/5450529037352790699'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-90-path.html' title='Android beginner tutorial: Part 90 - Path and ArcShape'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-TquDT1fxgDI/URu-uHn9JII/AAAAAAAABIs/8tFQZA0eS84/s72-c/a_star.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-6205400101153748337</id><published>2013-02-21T10:00:00.000+02:00</published><updated>2013-02-21T10:00:04.749+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 89 - ShapeDrawable shapes</title><content type='html'>Today we&#39;ll learn about drawing shapes in Android using the ShapeDrawable class.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;If you want to dynamically draw 2d shapes, the ShapeDrawable class might be what you&#39;re looking for. Similar to the Flash Drawing API used in Actionscript 3, this Android gives us the ability to draw and stylize simple primitive shapes.&lt;br /&gt;&lt;br /&gt;There is a set of classes extended from Shape, which we can use to create different shapes. These classes are PathShape, RectShape, ArcShape, OvalShape and RoundRectShape.&lt;br /&gt;&lt;br /&gt;The ShapeDrawable class is an extension of Drawable, so it can be used just like any other Drawable object. When drawing in a ShapeDrawable - remember to set the color and the boundaries of the object. If the color is not specified - black will be used by default. If the boundaries are not set - the drawing will not be visible. &lt;br /&gt;&lt;br /&gt;Firstly, let&#39;s see how to draw lines with this. There isn&#39;t a separate class for displaying lines, but there&#39;s RectShape, which can work as lines too if we make them thing enough.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;ShapeDrawable shape = new ShapeDrawable(new RectShape());&lt;br /&gt;shape.setIntrinsicHeight(2);&lt;br /&gt;shape.setIntrinsicWidth(200);&lt;br /&gt;shape.getPaint().setColor(Color.RED);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;An oval is drawn similarly to a rectangle. Simply set the width and height:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;ShapeDrawable shape = new ShapeDrawable(new OvalShape());&lt;br /&gt;shape.setIntrinsicHeight(100);&lt;br /&gt;shape.setIntrinsicWidth(200);&lt;br /&gt;shape.getPaint().setColor(Color.RED);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Drawing rounded rectangles is a little bit more complex, but not hard at all. The RoundRectShape constructor has 3 parameters - outerRadii, inset and innerRadii. &lt;br /&gt;&lt;br /&gt;The outerRadii value is an array of float values that represent the radii of each corner. Each corner has 2 radii, so there are 8 values in total. The first 2 values are the radii of the top left corner, each next 2 correspond to the next corner clockwise. If you don&#39;t want rounded corners, pass null.&lt;br /&gt;&lt;br /&gt;The inset value is a RectF class object, which represents the distance between the inner rectangle and the outer one. The constructor of the RectF class has 4 parameters - pairs of coordinates (x and y) of top left corner and bottom right corner of the inner rectangle. If there&#39;s no inner rectangle, pass null.&lt;br /&gt;&lt;br /&gt;The innerRadii value is an array of radii for the corners of the inner rectangle. Works the same way as outerRadii.&lt;br /&gt;&lt;br /&gt;A simple example:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;float[] outR = new float[] {6, 6, 6, 6, 6, 6, 6, 6};&lt;br /&gt;RectF rectF = nwe RectF(8, 8, 8, 8);&lt;br /&gt;float[] inR = new float[] {6, 6, 6, 6, 6, 6, 6, 6};&lt;br /&gt;&lt;br /&gt;ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(outR, rectF, inR));&lt;br /&gt;shape.setIntrinsicHeight(100);&lt;br /&gt;shape.setIntrinsicWidth(200);&lt;br /&gt;shape.getPaint().setColor(Color.RED);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;That&#39;s all for today. Next time we&#39;ll look at more shapes to draw.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/6205400101153748337/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-89.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/6205400101153748337'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/6205400101153748337'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-89.html' title='Android beginner tutorial: Part 89 - ShapeDrawable shapes'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-3425232882408149895</id><published>2013-02-20T10:00:00.000+02:00</published><updated>2013-02-20T10:00:04.692+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 88 - TransitionDrawable</title><content type='html'>In this tutorial we will take a look at the TransitionDrawable class, which lets us display a smooth cross-fade effect between two pictures.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Firstly, we&#39;ll need to add 2 pictures to the drawable directory of the project. In my case, they are sample1.jpg and sample2.jpg, and I put both of them in res/drawable-hdpi/ directory.&lt;br /&gt;&lt;br /&gt;Add an xml file in the same directory, call it transition.xml. Here we will add a transition node with 2 item nodes inside, each representing an image:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;transition xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&amp;gt;&lt;br /&gt;    &amp;lt;item android:drawable=&quot;@drawable/sample1&quot;/&amp;gt;&lt;br /&gt;    &amp;lt;item android:drawable=&quot;@drawable/sample2&quot;/&amp;gt;&lt;br /&gt;&amp;lt;/transition&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to activity_main.xml layout file. Add an ImageView, set its source to the first image:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&lt;br /&gt;    android:layout_width=&quot;match_parent&quot;&lt;br /&gt;    android:layout_height=&quot;match_parent&quot;&lt;br /&gt;    android:orientation=&quot;vertical&quot;&lt;br /&gt;    tools:context=&quot;.MainActivity&quot; &amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;ImageView&lt;br /&gt;        android:id=&quot;@+id/image&quot;&lt;br /&gt;        android:layout_width=&quot;match_parent&quot;&lt;br /&gt;        android:layout_height=&quot;match_parent&quot;&lt;br /&gt;        android:src=&quot;@drawable/sample1&quot;&lt;br /&gt;        /&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;When that&#39;s done, go to MainActivity.java class.&lt;br /&gt;&lt;br /&gt;Declare 2 variables - instances of ImageView and TransitionDrawable.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;private ImageView image;&lt;br /&gt;private TransitionDrawable trans;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;In the onCreate() function of the Activity, set the image value to the ImageView we declared in activity_main.xml. Set trans value to the transition resource we created in the drawable directory:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;image = (ImageView)findViewById(R.id.image);&lt;br /&gt;Resources res = this.getResources();&lt;br /&gt;trans = (TransitionDrawable)res.getDrawable(R.drawable.transition);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now add a click listener to the ImageView object. In the event of a click, use the setImageDrawable() method to apply the transition resource to the image.&lt;br /&gt;&lt;br /&gt;Then we call the reverseTransition() method of the transition, passing 1000 as the parameter, which is the effect time.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;image.setOnClickListener(new OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;image.setImageDrawable(trans);&lt;br /&gt;trans.reverseTransition(1000);&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Instead of reverseTransition(), you can call startTransition(), which always starts with the first layer fading out and the second layer fading it. The reverseTransition() method handles the transition both ways.&lt;br /&gt;&lt;br /&gt;Full code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.content.res.Resources;&lt;br /&gt;import android.graphics.drawable.TransitionDrawable;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.view.View;&lt;br /&gt;import android.view.View.OnClickListener;&lt;br /&gt;import android.widget.ImageView;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;private ImageView image;&lt;br /&gt;private TransitionDrawable trans;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;image = (ImageView)findViewById(R.id.image);&lt;br /&gt;Resources res = this.getResources();&lt;br /&gt;trans = (TransitionDrawable)res.getDrawable(R.drawable.transition);&lt;br /&gt;&lt;br /&gt;image.setOnClickListener(new OnClickListener() {&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View v) {&lt;br /&gt;image.setImageDrawable(trans);&lt;br /&gt;trans.reverseTransition(1000);&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;If you run your application now, you&#39;ll see that tapping the image smoothly changes the displayed image.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/3425232882408149895/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-88.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3425232882408149895'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3425232882408149895'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-88.html' title='Android beginner tutorial: Part 88 - TransitionDrawable'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-8437004585985055350</id><published>2013-02-19T10:00:00.000+02:00</published><updated>2013-02-19T10:00:07.223+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 87 - 2D Graphics in Android</title><content type='html'>In this part we will talk about drawing graphics in Android.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;The Android SDK provides a set of drawing APIs for rendering 2D graphics. There are two ways to draw in Android: directly into a View object, or into a Canvas.&lt;br /&gt;&lt;br /&gt;Drawing inside a View object means simply giving the View a picture to display from your layout. The drawing of the graphics is done using the system&#39;s normal View hierarchy drawing process. This method is perfect for drawing simple graphics that aren&#39;t changed dynamically in real time and shouldn&#39;t be used in performance intentsive video games, but can be used to display an animation in a static application.&lt;br /&gt;&lt;br /&gt;Drawing to a Canvas means using the appropriate class&#39; onDraw() callback method (with your Canvas object passed as the parameter) or using one of the Canvas draw..() methods (for example, drawPicture() or drawBitmap()). This method is used when the application redraws the graphics regularly (for example, in a video game). But there are more than one ways to draw using the Canvas.&lt;br /&gt;&lt;br /&gt;The first option is to handle the drawing in the same thread as the main Activity of your application, calling invalidate() each time the graphics need to be redrawn and handling the onDraw() callback function.&lt;br /&gt;&lt;br /&gt;The second option is to draw in a separate thread, where you can manage a SurfaceView object and draw graphics in the Canvas as fast as it is possible in that thread.&lt;br /&gt;&lt;br /&gt;Android SDK provides a library for drawing 2D shapes and images - Drawables. The Drawable class is extended by multiple subclasses, such as BitmapDrawable, NinePatchDrawable, PictureDrawable, ShapeDrawable, etc.&lt;br /&gt;&lt;br /&gt;There are 3 ways to instantiate a Drawable object - using an image that is stored in res/drawable/ directory (which we already did in some of the previous tutorials), using an XML file, which contains properties of the Drawable object,or using the normal class constructors.&lt;br /&gt;&lt;br /&gt;The easiest way to add graphics to an application is to use an external image stored in the resources. The supported file formats are PNG, JPEG and GIF, where PNG is the most preferable one and GIF is the least preferable.&lt;br /&gt;&lt;br /&gt;Using images from the resources is suitable for displaying icons, images and other graphics in an application. &lt;br /&gt;&lt;br /&gt;All the image resources located in res/drawable/ are optimized using aapt utility during compilation of the project. The quality of the pictures remain the game, but the file size is less. If you need to load bitmap images without compressing them, store them in res/raw/ directory, where they won&#39;t be reached by aapt. In that case you&#39;ll have to manually load and read the image data and display it, though.&lt;br /&gt;&lt;br /&gt;That is all for today. Next time we&#39;ll take a closer look at Drawables.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/8437004585985055350/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-87-2d.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8437004585985055350'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8437004585985055350'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-87-2d.html' title='Android beginner tutorial: Part 87 - 2D Graphics in Android'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-8100489189208958108</id><published>2013-02-18T10:00:00.000+02:00</published><updated>2013-02-18T10:00:09.644+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 86 - Localization</title><content type='html'>In this part we will talk about application localization.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;After deployment, your application may be used on Android devices in many regions. To get the most users, you should think about making your application suitable for people speaking different languages - in other words, you should localize it.&lt;br /&gt;&lt;br /&gt;Localization is actually fairly easy to implement. Up until now, when we set labels to buttons and text values to textviews and other components, we stored those values in strings.xml file (except some individual cases where we simply hard-coded the text in the xml files for testing purposes). Localization system lets us create multiple string, audio, visual, etc. files for different regions.&lt;br /&gt;&lt;br /&gt;By using the res/values/strings.xml file, you&#39;re setting the default values for all regions. When told to look for a resource, the Android system picks the one that suits the region of the user. If no such item exists, the default value is picked. That&#39;s why if you&#39;re using resources in your application, you should always include all of the used values that would be used by default.&lt;br /&gt;&lt;br /&gt;The default values stored in res/values/ directory should be using the most common language you expect your application users to speak. Normally, it&#39;s English.&lt;br /&gt;&lt;br /&gt;To set resources for other regions, use the values-&amp;lt;qualifiers&amp;gt; pattern to create new directories and store your resources there. The &amp;lt;qualifiers&amp;gt; can be replaced by one or a group of qualifiers (for example, we know we can store images for different pixel density using qualifiers such as mdpi, hdpi, etc.). &lt;br /&gt;&lt;br /&gt;As the Android official docs state: the language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase &quot;r&quot;). &lt;br /&gt;&lt;br /&gt;Examples of such language codes are &quot;en&quot;, &quot;fr&quot;, &quot;ca&quot;, as well as &quot;en-rUS&quot;, &quot;fr-rFR&quot; and &quot;fr-rCA&quot;.&lt;br /&gt;&lt;br /&gt;This means that we can store values in directories like res/values/strings.xml, res/values-fr/strings.xml, res/values-ja/strings.xml, etc.&lt;br /&gt;&lt;br /&gt;And that is pretty much it. Using this information you&#39;ll be able to make a flexible Android application which adapts to the user&#39;s region.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/8100489189208958108/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-86.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8100489189208958108'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8100489189208958108'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-86.html' title='Android beginner tutorial: Part 86 - Localization'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-2530239463094140597</id><published>2013-02-17T10:00:00.000+02:00</published><updated>2013-02-17T10:00:03.914+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 85 - Embedding fonts using Assets</title><content type='html'>In this tutorial we will learn how to load and use a font from an asset.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Assets are somewhat similar to resources, yet different. While resources are embedded into the application and can be referred to using the R class, assets are raw files that are stored in the assets directory and require us to manually read them to use them in our applications.&lt;br /&gt;&lt;br /&gt;Today we&#39;ll learn how to embed and use a raw .ttf font file as an asset in our application.&lt;br /&gt;&lt;br /&gt;First of all you need to find the ttf file. You can download fonts online, I use fontsquirrel.com - all the fonts there are free and pretty good.&lt;br /&gt;&lt;br /&gt;Once you have the .ttf file, put it in the assets directory of your project.&lt;br /&gt;&lt;br /&gt;Then go to activity_main.xml of your application and add a TextView there:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&lt;br /&gt;    android:layout_width=&quot;match_parent&quot;&lt;br /&gt;    android:layout_height=&quot;match_parent&quot;&lt;br /&gt;    android:orientation=&quot;vertical&quot;&lt;br /&gt;    tools:context=&quot;.MainActivity&quot; &amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;TextView android:layout_width=&quot;match_parent&quot;&lt;br /&gt;        android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;        android:id=&quot;@+id/myText&quot;&lt;br /&gt;        android:textSize=&quot;36sp&quot;&lt;br /&gt;        android:text=&quot;Hello world!&quot;&lt;br /&gt;        /&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to MainActivity.java class and load the font using Typeface.createFromAsset() static method. Once the font is extracted, apply it to the text using setTypeface() method:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;TextView mytext = (TextView)findViewById(R.id.myText);&lt;br /&gt;Typeface face = Typeface.createFromAsset(getAssets(), &quot;yukarimobil.ttf&quot;);&lt;br /&gt;mytext.setTypeface(face);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Here&#39;s the full code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.graphics.Typeface;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.widget.TextView;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;TextView mytext = (TextView)findViewById(R.id.myText);&lt;br /&gt;Typeface face = Typeface.createFromAsset(getAssets(), &quot;yukarimobil.ttf&quot;);&lt;br /&gt;mytext.setTypeface(face);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;It is that easy!&lt;br /&gt;&lt;br /&gt;You can use assets to store all sorts of files and read them byte by byte or using what&#39;s provided by the Android SDK, like the Typeface.createFromAsset() method just now.&lt;br /&gt;&lt;br /&gt;That&#39;s all for today.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/2530239463094140597/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-85.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/2530239463094140597'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/2530239463094140597'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-85.html' title='Android beginner tutorial: Part 85 - Embedding fonts using Assets'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-3522106985138001292</id><published>2013-02-16T10:00:00.000+02:00</published><updated>2013-02-16T10:00:06.678+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 84 - Styles and Themes</title><content type='html'>Today we will learn about styles and themes in Android.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;A Style is a collection of properties that set the appearance of a single View or window. The idea behind styles is similar to CSS - you define styles of an object somewhere outside of the layout code and then just apply the style to one or multiple elements.&lt;br /&gt;&lt;br /&gt;A Theme is a style that is applied to an entire Activity or all Activities of an application.&lt;br /&gt;&lt;br /&gt;The process for creating styles and themes is the same.&lt;br /&gt;&lt;br /&gt;Create a new XML style in res/values/ directory, you can use whatever name you want.&lt;br /&gt;&lt;br /&gt;Add this code inside:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;&amp;lt;style name=&quot;SpecialText&quot; parent=&quot;@android:style/TextAppearance.Medium&quot;&amp;gt;&lt;br /&gt;    &amp;lt;item name=&quot;android:layout_width&quot;&amp;gt;fill_parent&amp;lt;/item&amp;gt;&lt;br /&gt;    &amp;lt;item name=&quot;android:layout_height&quot;&amp;gt;wrap_content&amp;lt;/item&amp;gt;&lt;br /&gt;    &amp;lt;item name=&quot;android:textColor&quot;&amp;gt;#FF0000&amp;lt;/item&amp;gt;&lt;br /&gt;&amp;lt;/style&amp;gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;As you can see, it&#39;s a simple XML containing a style node which has 3 items. Each item represents an attribute and its value. The style node also has a &quot;name&quot; attribute and a &quot;parent&quot; one. &lt;br /&gt;&lt;br /&gt;The name is the unique identificator for this style, this is the name that you will later use to refer to this style.&lt;br /&gt;&lt;br /&gt;The parent is an optional vattribute that specifies the resource ID of another style from which this style should inherit properties. This way you can override the inherited style properties if needed. The resource can be of any type as long as it contains the needed style. It is recommended that you always inherit the default styles for the specific views - directly or indirectly. &lt;br /&gt;&lt;br /&gt;After the style is created, you can use it by simply setting the style property of a View.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;TextView&lt;br /&gt;    style=&quot;@style/SpecialText&quot;&lt;br /&gt;    android:text=&quot;Special text!&quot; /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Note that the style attribute does not need an android: prefix.&lt;br /&gt;&lt;br /&gt;If you apply a style to a ViewGroup container, the style will NOT be inherited by its children. To make it inheritable, set the style as a theme.&lt;br /&gt;&lt;br /&gt;To set a theme to the whole application (all of its Activities), go to the AndroidManifest XML file and set the android:theme property of the application node:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;application android:theme=&quot;@style/MyTheme&quot;&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;If you want to set it to a single Activity, just do the same with the activity tag.&lt;br /&gt;&lt;br /&gt;There are also a few pre-built themes that you can use. Theme.Dialog, for instance, displays an Activity like a dialog window:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;activity android:theme=&quot;@android:style/Theme.Dialog&quot;&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;If you want to create a new theme based on an existing one, you can add the base theme as the parent of the style.&lt;br /&gt;&lt;br /&gt;That&#39;s all for today.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/3522106985138001292/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-84.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3522106985138001292'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3522106985138001292'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-84.html' title='Android beginner tutorial: Part 84 - Styles and Themes'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-3552696920992281162</id><published>2013-02-15T10:00:00.000+02:00</published><updated>2013-02-15T10:00:19.725+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 83 - Parsing XML</title><content type='html'>In this tutorial we will learn how to read and parse an XML file in Android.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Firstly, create an XML file and put it in res/xml/ directory of the project.&lt;br /&gt;&lt;br /&gt;My XML file is called products.xml and looks like this:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;catalog&amp;gt;&lt;br /&gt;  &amp;lt;product name=&quot;Chair&quot; description=&quot;Something to sit on&quot; price=&quot;50&quot; /&amp;gt;&lt;br /&gt;  &amp;lt;product name=&quot;Sofa&quot; description=&quot;Something soft to sit on&quot; price=&quot;100&quot; /&amp;gt;&lt;br /&gt;  &amp;lt;product name=&quot;Table&quot; description=&quot;Something to put stuff on&quot; price=&quot;70&quot; /&amp;gt;&lt;br /&gt;&amp;lt;/catalog&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to activity_main.xml layout file of your main Activity and add a List, which will display the items from the xml:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&lt;br /&gt;    android:layout_width=&quot;match_parent&quot;&lt;br /&gt;    android:layout_height=&quot;match_parent&quot;&lt;br /&gt;    android:orientation=&quot;vertical&quot;&lt;br /&gt;    tools:context=&quot;.MainActivity&quot; &amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;ListView android:id=&quot;@+id/listview&quot;&lt;br /&gt;        android:layout_width=&quot;match_parent&quot;&lt;br /&gt;        android:layout_height=&quot;match_parent&quot;&lt;br /&gt;        /&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to MainActivity.java class. This is where the magic starts - we need to load and parse the data from the XML file using a XmlPullParser object. This part is very different from AS3 or anything else I have worked with. Android&#39;s xml parser creates a row of Events which we can read and parse to create a data set of our own.&lt;br /&gt;&lt;br /&gt;Firstly let&#39;s declare an ArrayList of Strings in the onCreate() function:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Then we add a try...catch statement, displaying an error message using a Toast in case of an error. We try to create a new XmlPullParser object and use it to load our xml file. Using a while loop, we check each event of the parser using getEventType() to see if it doesn&#39;t equal XmlPullParser.END_DOCUMENT, which means that we&#39;re parsing until we reach the end of the document. Then we check if the event equals XmlPullParser.START_TAG and the name of the tag equals &quot;product&quot;.&lt;br /&gt;&lt;br /&gt;We can then add items to our list object using the add() method.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;try{&lt;br /&gt;XmlPullParser parser = getResources().getXml(R.xml.products);&lt;br /&gt;&lt;br /&gt;while(parser.getEventType()!=XmlPullParser.END_DOCUMENT){&lt;br /&gt;if(parser.getEventType() == XmlPullParser.START_TAG &amp;&amp; parser.getName().equals(&quot;product&quot;)){&lt;br /&gt;list.add(parser.getAttributeValue(0) + &quot; (&quot; + parser.getAttributeValue(2) + &quot;)\n&quot; + parser.getAttributeValue(1));&lt;br /&gt;}&lt;br /&gt;parser.next();&lt;br /&gt;}&lt;br /&gt;}catch(Throwable t){&lt;br /&gt;Toast.makeText(this, &quot;Error loading XML.&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;After the try...catch statement, set the adapter of the list view to list.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;ListView listView = (ListView)findViewById(R.id.listview);&lt;br /&gt;listView.setAdapter(new ArrayAdapter&amp;lt;String&amp;gt;(this, android.R.layout.simple_list_item_1, list));&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Full code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;&lt;br /&gt;import org.xmlpull.v1.XmlPullParser;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.widget.ArrayAdapter;&lt;br /&gt;import android.widget.ListView;&lt;br /&gt;import android.widget.Toast;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;&lt;br /&gt;try{&lt;br /&gt;XmlPullParser parser = getResources().getXml(R.xml.products);&lt;br /&gt;&lt;br /&gt;while(parser.getEventType()!=XmlPullParser.END_DOCUMENT){&lt;br /&gt;if(parser.getEventType() == XmlPullParser.START_TAG &amp;&amp; parser.getName().equals(&quot;product&quot;)){&lt;br /&gt;list.add(parser.getAttributeValue(0) + &quot; (&quot; + parser.getAttributeValue(2) + &quot;)\n&quot; + parser.getAttributeValue(1));&lt;br /&gt;}&lt;br /&gt;parser.next();&lt;br /&gt;}&lt;br /&gt;}catch(Throwable t){&lt;br /&gt;Toast.makeText(this, &quot;Error loading XML.&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;ListView listView = (ListView)findViewById(R.id.listview);&lt;br /&gt;listView.setAdapter(new ArrayAdapter&amp;lt;String&amp;gt;(this, android.R.layout.simple_list_item_1, list));&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Those are the basics!&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/3552696920992281162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-83.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3552696920992281162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3552696920992281162'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-83.html' title='Android beginner tutorial: Part 83 - Parsing XML'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-893511209910992299</id><published>2013-02-14T10:00:00.000+02:00</published><updated>2013-02-14T10:00:11.237+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 82 - PreferenceCategory and PreferenceScreen</title><content type='html'>In this tutorial we&#39;ll learn about grouping preferences in categories and screens.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;In some cases you might find that our settings screen is clustered with lots of preference items. You&#39;ll want to find a way to group them all nice and tidy, so that the user doesn&#39;t have trouble finding the settings he is looking for.&lt;br /&gt;&lt;br /&gt;The first way to do so is group preferences of the same topic under PreferenceCategories. By doing this, the grouped preferences will have a title over them, which explains the role of the grouped components.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;PreferenceCategory &lt;br /&gt;        android:title=&quot;@string/pref_visual&quot;&lt;br /&gt;        android:key=&quot;pref_visual&quot;&amp;gt;&lt;br /&gt;    &amp;lt;EditTextPreference&lt;br /&gt;        android:key=&quot;pref_textsize&quot;&lt;br /&gt;        android:title=&quot;@string/pref_textsize&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_textsize_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_textsize_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;14&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;ListPreference&lt;br /&gt;        android:key=&quot;pref_theme&quot;&lt;br /&gt;        android:title=&quot;@string/pref_theme&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_theme_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_theme_summ&quot;&lt;br /&gt;        android:entries=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:entryValues=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:defaultValue=&quot;Blue&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/PreferenceCategory&amp;gt;&lt;br /&gt;    &amp;lt;PreferenceCategory &lt;br /&gt;        android:title=&quot;@string/pref_sound&quot;&lt;br /&gt;        android:key=&quot;pref_sound&quot;&amp;gt;&lt;br /&gt;    &amp;lt;RingtonePreference&lt;br /&gt;        android:key=&quot;pref_tone&quot;&lt;br /&gt;        android:title=&quot;@string/pref_tone&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_tone_summ&quot;&lt;br /&gt;        android:showDefault=&quot;true&quot;&lt;br /&gt;        android:showSilent=&quot;true&quot;/&amp;gt;&lt;br /&gt;    &amp;lt;CheckBoxPreference&lt;br /&gt;        android:key=&quot;pref_mute&quot;&lt;br /&gt;        android:title=&quot;@string/pref_mute&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_mute_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;false&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/PreferenceCategory&amp;gt;&lt;br /&gt;&amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;You can see that I grouped all the visual-related preferences and all sound-related ones in different categories. Their titles are stored in strings.xml:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;string name=&quot;app_name&quot;&amp;gt;CodeForFood Test Two&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute&quot;&amp;gt;Mute&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute_summ&quot;&amp;gt;Disable all the sound in the application&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize&quot;&amp;gt;Text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_descr&quot;&amp;gt;Enter text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_summ&quot;&amp;gt;Set text size of the interface&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme&quot;&amp;gt;Theme&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme_descr&quot;&amp;gt;Pick a color&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme_summ&quot;&amp;gt;Set the application\&#39;s color theme&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_tone&quot;&amp;gt;Ringtone&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_tone_summ&quot;&amp;gt;Set ringtone&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_visual&quot;&amp;gt;Visual settings&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_sound&quot;&amp;gt;Sound settings&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;menu_settings&quot;&amp;gt;Settings&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;clear_settings&quot;&amp;gt;Reset settings&amp;lt;/string&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The second way to group preferences is using PreferenceScreen nodes.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;PreferenceScreen&lt;br /&gt;        android:title=&quot;@string/pref_visual&quot;&lt;br /&gt;        android:key=&quot;pref_visual&quot;&amp;gt;&lt;br /&gt;    &amp;lt;EditTextPreference&lt;br /&gt;        android:key=&quot;pref_textsize&quot;&lt;br /&gt;        android:title=&quot;@string/pref_textsize&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_textsize_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_textsize_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;14&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;ListPreference&lt;br /&gt;        android:key=&quot;pref_theme&quot;&lt;br /&gt;        android:title=&quot;@string/pref_theme&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_theme_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_theme_summ&quot;&lt;br /&gt;        android:entries=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:entryValues=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:defaultValue=&quot;Blue&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;    &amp;lt;PreferenceScreen &lt;br /&gt;        android:title=&quot;@string/pref_sound&quot;&lt;br /&gt;        android:key=&quot;pref_sound&quot;&amp;gt;&lt;br /&gt;    &amp;lt;RingtonePreference&lt;br /&gt;        android:key=&quot;pref_tone&quot;&lt;br /&gt;        android:title=&quot;@string/pref_tone&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_tone_summ&quot;&lt;br /&gt;        android:showDefault=&quot;true&quot;&lt;br /&gt;        android:showSilent=&quot;true&quot;/&amp;gt;&lt;br /&gt;    &amp;lt;CheckBoxPreference&lt;br /&gt;        android:key=&quot;pref_mute&quot;&lt;br /&gt;        android:title=&quot;@string/pref_mute&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_mute_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;false&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;&amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;By including PreferenceScreens inside a PreferenceScreen, the parent displays a clickable item for each PreferenceScreen, which opens that screen with all its contents as a separate Activity.&lt;br /&gt;&lt;br /&gt;That&#39;s all for today.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/893511209910992299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-82.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/893511209910992299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/893511209910992299'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-82.html' title='Android beginner tutorial: Part 82 - PreferenceCategory and PreferenceScreen'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-4560703027809347869</id><published>2013-02-13T10:00:00.000+02:00</published><updated>2013-02-13T10:00:19.814+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 81 - RingtonePreference</title><content type='html'>In this part we will learn how to create and handle a RingtonePreference object.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;A RingtonePreference is a settings item that lets the user pick a ringtone from their system. The selected ringtone&#39;s URI address is saved in the preferences file, which can then be used by the application.&lt;br /&gt;&lt;br /&gt;Firstly go to preferences.xml file and add a new RingtonePreference object. The attributes we&#39;re going to set are android:key, android:title, android:summary, android:showDefault and android:showSilent. The first 3 were already used and explained in the previous tutorials, the last 2 are boolean values that determine whether the ringtone list should display the options to select default ringtone or set ringtone to silent.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;    &amp;lt;RingtonePreference&lt;br /&gt;        android:key=&quot;pref_tone&quot;&lt;br /&gt;        android:title=&quot;@string/pref_tone&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_tone_summ&quot;&lt;br /&gt;        android:showDefault=&quot;true&quot;&lt;br /&gt;        android:showSilent=&quot;true&quot;/&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;So far the preferences.xml file looks like this:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&amp;gt;&lt;br /&gt;    &amp;lt;CheckBoxPreference&lt;br /&gt;        android:key=&quot;pref_mute&quot;&lt;br /&gt;        android:title=&quot;@string/pref_mute&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_mute_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;false&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;EditTextPreference&lt;br /&gt;        android:key=&quot;pref_textsize&quot;&lt;br /&gt;        android:title=&quot;@string/pref_textsize&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_textsize_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_textsize_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;14&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;ListPreference&lt;br /&gt;        android:key=&quot;pref_theme&quot;&lt;br /&gt;        android:title=&quot;@string/pref_theme&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_theme_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_theme_summ&quot;&lt;br /&gt;        android:entries=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:entryValues=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:defaultValue=&quot;Blue&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;RingtonePreference&lt;br /&gt;        android:key=&quot;pref_tone&quot;&lt;br /&gt;        android:title=&quot;@string/pref_tone&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_tone_summ&quot;&lt;br /&gt;        android:showDefault=&quot;true&quot;&lt;br /&gt;        android:showSilent=&quot;true&quot;/&amp;gt;&lt;br /&gt;&amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Here&#39;s the updated strings.xml file with the new string values:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;string name=&quot;app_name&quot;&amp;gt;CodeForFood Test Two&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute&quot;&amp;gt;Mute&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute_summ&quot;&amp;gt;Disable all the sound in the application&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize&quot;&amp;gt;Text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_descr&quot;&amp;gt;Enter text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_summ&quot;&amp;gt;Set text size of the interface&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme&quot;&amp;gt;Theme&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme_descr&quot;&amp;gt;Pick a color&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme_summ&quot;&amp;gt;Set the application\&#39;s color theme&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_tone&quot;&amp;gt;Ringtone&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_tone_summ&quot;&amp;gt;Set ringtone&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;menu_settings&quot;&amp;gt;Settings&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;clear_settings&quot;&amp;gt;Reset settings&amp;lt;/string&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;In the onResume() function of MainActivity.java, add a line that displays the URI of the ringtone:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;Toast.makeText(this, &quot;Ringtone: &quot; + prefs.getString(&quot;pref_tone&quot;, &quot;none&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Full class code looks like this:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.content.Intent;&lt;br /&gt;import android.content.SharedPreferences;&lt;br /&gt;import android.content.SharedPreferences.Editor;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.preference.PreferenceManager;&lt;br /&gt;import android.view.Menu;&lt;br /&gt;import android.view.MenuItem;&lt;br /&gt;import android.view.View;&lt;br /&gt;import android.view.View.OnClickListener;&lt;br /&gt;import android.widget.Button;&lt;br /&gt;import android.widget.Toast;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;static final int IDM_SETTINGS = 101;&lt;br /&gt;public SharedPreferences prefs;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;Button btn_clear = (Button)findViewById(R.id.clearButton);&lt;br /&gt;btn_clear.setOnClickListener(new OnClickListener() {&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View arg0) {&lt;br /&gt;Editor editor = prefs.edit();&lt;br /&gt;editor.clear();&lt;br /&gt;editor.commit();&lt;br /&gt;onResume();&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void onResume(){&lt;br /&gt;prefs = PreferenceManager.getDefaultSharedPreferences(this);&lt;br /&gt;if(prefs.getBoolean(&quot;pref_mute&quot;, false)){&lt;br /&gt;Toast.makeText(this, &quot;Muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}else{&lt;br /&gt;Toast.makeText(this, &quot;Not muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}&lt;br /&gt;Toast.makeText(this, &quot;Text size: &quot; + prefs.getString(&quot;pref_textsize&quot;, &quot;14&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;Toast.makeText(this, &quot;Theme color: &quot; + prefs.getString(&quot;pref_theme&quot;, &quot;Blue&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;Toast.makeText(this, &quot;Ringtone: &quot; + prefs.getString(&quot;pref_tone&quot;, &quot;none&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;&lt;br /&gt;super.onResume();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onCreateOptionsMenu(Menu menu){&lt;br /&gt;menu.add(Menu.NONE, IDM_SETTINGS, Menu.NONE, R.string.menu_settings);&lt;br /&gt;return(super.onCreateOptionsMenu(menu));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onOptionsItemSelected(MenuItem item){&lt;br /&gt;if(item.getItemId()==IDM_SETTINGS){&lt;br /&gt;Intent intent = new Intent();&lt;br /&gt;intent.setClass(this, TestPreferences.class);&lt;br /&gt;startActivity(intent);&lt;br /&gt;}&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;And that&#39;s it. If you test your application now, you&#39;ll see that there&#39;s a new preference item in the settings of your application.&lt;br /&gt;&lt;br /&gt;The ringtone list lets you preview and select from default ringtones as well as the music library.&lt;br /&gt;&lt;br /&gt;That&#39;s all for today!&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/4560703027809347869/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-81.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/4560703027809347869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/4560703027809347869'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-81.html' title='Android beginner tutorial: Part 81 - RingtonePreference'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-7900912912859486898</id><published>2013-02-12T10:00:00.000+02:00</published><updated>2013-02-12T10:00:11.941+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 80 - ListPreference and resetting settings</title><content type='html'>In this tutorial we are going to add a ListPreference item to our Settings, as well as the ability to reset preferences.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;A ListPreference is a settings item which displays a list of choices when selected. The choices are all radio-buttons so there can be 0 or 1 choices selected at the same time.&lt;br /&gt;&lt;br /&gt;We can supply choice labels and their values to a ListPreference by first declaring them in an array. Create a new xml file in res/values/ directory called arrays.xml.&lt;br /&gt;&lt;br /&gt;Inside, we create a single-array named theme_colors with 4 possible choices:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;    &amp;lt;string-array name=&quot;theme_colors&quot;&amp;gt;&lt;br /&gt;        &amp;lt;item&amp;gt;Blue&amp;lt;/item&amp;gt;&lt;br /&gt;        &amp;lt;item&amp;gt;Red&amp;lt;/item&amp;gt;&lt;br /&gt;        &amp;lt;item&amp;gt;Green&amp;lt;/item&amp;gt;&lt;br /&gt;        &amp;lt;item&amp;gt;Yellow&amp;lt;/item&amp;gt;&lt;br /&gt;    &amp;lt;/string-array&amp;gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to preferences.xml file and add a new ListPreference item:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&amp;gt;&lt;br /&gt;    &amp;lt;CheckBoxPreference&lt;br /&gt;        android:key=&quot;pref_mute&quot;&lt;br /&gt;        android:title=&quot;@string/pref_mute&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_mute_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;false&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;EditTextPreference&lt;br /&gt;        android:key=&quot;pref_textsize&quot;&lt;br /&gt;        android:title=&quot;@string/pref_textsize&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_textsize_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_textsize_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;14&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;ListPreference&lt;br /&gt;        android:key=&quot;pref_theme&quot;&lt;br /&gt;        android:title=&quot;@string/pref_theme&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_theme_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_theme_summ&quot;&lt;br /&gt;        android:entries=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:entryValues=&quot;@array/theme_colors&quot;&lt;br /&gt;        android:defaultValue=&quot;Blue&quot; /&amp;gt;&lt;br /&gt;&amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;You can see 2 new attributes used in that item - entries and entryValues. They both have the same value of theme_colors array we&#39;ve just defined in arrays.xml. Of course, they don&#39;t need to be the same. The difference between the two is that &quot;entries&quot; is an array of labels of the items in the list, while &quot;entryValues&quot; is an array of the values that are not visible to the user but can be used by the programmer to determine which item was selected.&lt;br /&gt;&lt;br /&gt;Go to MainActivity.java class. You can read and display the selected item&#39;s value using a Toast like this:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;@Override&lt;br /&gt;public void onResume(){&lt;br /&gt;prefs = PreferenceManager.getDefaultSharedPreferences(this);&lt;br /&gt;if(prefs.getBoolean(&quot;pref_mute&quot;, false)){&lt;br /&gt;Toast.makeText(this, &quot;Muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}else{&lt;br /&gt;Toast.makeText(this, &quot;Not muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}&lt;br /&gt;Toast.makeText(this, &quot;Text size: &quot; + prefs.getString(&quot;pref_textsize&quot;, &quot;14&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;Toast.makeText(this, &quot;Theme color: &quot; + prefs.getString(&quot;pref_theme&quot;, &quot;Blue&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;&lt;br /&gt;super.onResume();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Note how the &quot;prefs&quot; variable is not declared in this function. That is because I made it public in the beginning of the class:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;public SharedPreferences prefs;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now let&#39;s also add the ability to reset preferences. Go to activity_main.xml and add a button:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&lt;br /&gt;    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&lt;br /&gt;    android:layout_width=&quot;match_parent&quot;&lt;br /&gt;    android:layout_height=&quot;match_parent&quot;&lt;br /&gt;    android:orientation=&quot;vertical&quot;&lt;br /&gt;    tools:context=&quot;.MainActivity&quot; &amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;Button android:id=&quot;@+id/clearButton&quot;&lt;br /&gt;        android:layout_width=&quot;match_parent&quot;&lt;br /&gt;        android:layout_height=&quot;wrap_content&quot;&lt;br /&gt;        android:text=&quot;@string/clear_settings&quot;&lt;br /&gt;        /&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/LinearLayout&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Back in MainActivity.java class, we add a click listener to that button and reset the settings by retreiving an Editor object from the preferences and calling its clear() and commit() methods:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;Button btn_clear = (Button)findViewById(R.id.clearButton);&lt;br /&gt;btn_clear.setOnClickListener(new OnClickListener() {&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View arg0) {&lt;br /&gt;Editor editor = prefs.edit();&lt;br /&gt;editor.clear();&lt;br /&gt;editor.commit();&lt;br /&gt;onResume();&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Full class code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.content.Intent;&lt;br /&gt;import android.content.SharedPreferences;&lt;br /&gt;import android.content.SharedPreferences.Editor;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.preference.PreferenceManager;&lt;br /&gt;import android.view.Menu;&lt;br /&gt;import android.view.MenuItem;&lt;br /&gt;import android.view.View;&lt;br /&gt;import android.view.View.OnClickListener;&lt;br /&gt;import android.widget.Button;&lt;br /&gt;import android.widget.Toast;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;static final int IDM_SETTINGS = 101;&lt;br /&gt;public SharedPreferences prefs;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;&lt;br /&gt;Button btn_clear = (Button)findViewById(R.id.clearButton);&lt;br /&gt;btn_clear.setOnClickListener(new OnClickListener() {&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void onClick(View arg0) {&lt;br /&gt;Editor editor = prefs.edit();&lt;br /&gt;editor.clear();&lt;br /&gt;editor.commit();&lt;br /&gt;onResume();&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void onResume(){&lt;br /&gt;prefs = PreferenceManager.getDefaultSharedPreferences(this);&lt;br /&gt;if(prefs.getBoolean(&quot;pref_mute&quot;, false)){&lt;br /&gt;Toast.makeText(this, &quot;Muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}else{&lt;br /&gt;Toast.makeText(this, &quot;Not muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}&lt;br /&gt;Toast.makeText(this, &quot;Text size: &quot; + prefs.getString(&quot;pref_textsize&quot;, &quot;14&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;Toast.makeText(this, &quot;Theme color: &quot; + prefs.getString(&quot;pref_theme&quot;, &quot;Blue&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;&lt;br /&gt;super.onResume();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onCreateOptionsMenu(Menu menu){&lt;br /&gt;menu.add(Menu.NONE, IDM_SETTINGS, Menu.NONE, R.string.menu_settings);&lt;br /&gt;return(super.onCreateOptionsMenu(menu));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onOptionsItemSelected(MenuItem item){&lt;br /&gt;if(item.getItemId()==IDM_SETTINGS){&lt;br /&gt;Intent intent = new Intent();&lt;br /&gt;intent.setClass(this, TestPreferences.class);&lt;br /&gt;startActivity(intent);&lt;br /&gt;}&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;You also need to declare all the string values that were used in the application in strings.xml:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;string name=&quot;app_name&quot;&amp;gt;CodeForFood Test Two&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute&quot;&amp;gt;Mute&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute_summ&quot;&amp;gt;Disable all the sound in the application&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize&quot;&amp;gt;Text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_descr&quot;&amp;gt;Enter text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_summ&quot;&amp;gt;Set text size of the interface&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme&quot;&amp;gt;Theme&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme_descr&quot;&amp;gt;Pick a color&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_theme_summ&quot;&amp;gt;Set the application\&#39;s color theme&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;menu_settings&quot;&amp;gt;Settings&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;clear_settings&quot;&amp;gt;Reset settings&amp;lt;/string&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;That&#39;s all for today.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/7900912912859486898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-80.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/7900912912859486898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/7900912912859486898'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-80.html' title='Android beginner tutorial: Part 80 - ListPreference and resetting settings'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-3986749111689689286</id><published>2013-02-11T10:00:00.000+02:00</published><updated>2013-02-11T10:00:25.021+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 79 - EditTextPreference, Reading Preferences</title><content type='html'>In this tutorial we&#39;ll add an EditTextPreference setting to our settings, as well as read and display saved preferences data in the application.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;First of all, go to the preferences.xml file and edit the contents to add a new EditTextPreference object. Just like the existing CheckBoxPreference item that we have, we&#39;ll give this one key, title, summary and defaultValues attributes. We&#39;ll also give it a dialogTitle attribute, which represents the text that&#39;s displayed in the dialog window that opens when the item is clicked by the user.&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&amp;gt;&lt;br /&gt;    &amp;lt;CheckBoxPreference&lt;br /&gt;        android:key=&quot;pref_mute&quot;&lt;br /&gt;        android:title=&quot;@string/pref_mute&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_mute_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;false&quot; /&amp;gt;&lt;br /&gt;    &amp;lt;EditTextPreference&lt;br /&gt;        android:key=&quot;pref_textsize&quot;&lt;br /&gt;        android:title=&quot;@string/pref_textsize&quot;&lt;br /&gt;        android:dialogTitle=&quot;@string/pref_textsize_descr&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_textsize_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;14&quot; /&amp;gt;&lt;br /&gt;&amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Go to strings.xml file and add the new string values:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;string name=&quot;app_name&quot;&amp;gt;CodeForFood Test Two&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute&quot;&amp;gt;Mute&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute_summ&quot;&amp;gt;Disable all the sound in the application&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize&quot;&amp;gt;Text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_descr&quot;&amp;gt;Enter text size&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_textsize_summ&quot;&amp;gt;Set text size of the interface&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;menu_settings&quot;&amp;gt;Settings&amp;lt;/string&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to MainActivity.java class. Here we will read the values of the preferences when the application is resumed - in the onResume() callback function. Doing it there will make sure that the data is updated from the saved settings file every time the application is launched or resumed if it was paused.&lt;br /&gt;&lt;br /&gt;In the onResume() function, load preferences like this:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Then read the values and handle them by simply displaying them in a Toast:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;if(prefs.getBoolean(&quot;pref_mute&quot;, false)){&lt;br /&gt;Toast.makeText(this, &quot;Muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}else{&lt;br /&gt;Toast.makeText(this, &quot;Not muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}&lt;br /&gt;Toast.makeText(this, &quot;Text size: &quot; + prefs.getString(&quot;pref_textsize&quot;, &quot;&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Full MainActivity.java class code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.content.Intent;&lt;br /&gt;import android.content.SharedPreferences;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.preference.PreferenceManager;&lt;br /&gt;import android.view.Menu;&lt;br /&gt;import android.view.MenuItem;&lt;br /&gt;import android.widget.Toast;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;static final int IDM_SETTINGS = 101;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void onResume(){&lt;br /&gt;SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);&lt;br /&gt;if(prefs.getBoolean(&quot;pref_mute&quot;, false)){&lt;br /&gt;Toast.makeText(this, &quot;Muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}else{&lt;br /&gt;Toast.makeText(this, &quot;Not muted!&quot;, Toast.LENGTH_SHORT).show();&lt;br /&gt;}&lt;br /&gt;Toast.makeText(this, &quot;Text size: &quot; + prefs.getString(&quot;pref_textsize&quot;, &quot;&quot;), Toast.LENGTH_SHORT).show();&lt;br /&gt;&lt;br /&gt;super.onResume();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onCreateOptionsMenu(Menu menu){&lt;br /&gt;menu.add(Menu.NONE, IDM_SETTINGS, Menu.NONE, R.string.menu_settings);&lt;br /&gt;return(super.onCreateOptionsMenu(menu));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onOptionsItemSelected(MenuItem item){&lt;br /&gt;if(item.getItemId()==IDM_SETTINGS){&lt;br /&gt;Intent intent = new Intent();&lt;br /&gt;intent.setClass(this, TestPreferences.class);&lt;br /&gt;startActivity(intent);&lt;br /&gt;}&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;That&#39;s all for today!&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/3986749111689689286/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-79.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3986749111689689286'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3986749111689689286'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-79.html' title='Android beginner tutorial: Part 79 - EditTextPreference, Reading Preferences'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-3992235796346494930</id><published>2013-02-10T10:00:00.000+02:00</published><updated>2013-02-10T10:00:10.609+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 78 - Creating Preferences Activity</title><content type='html'>In this tutorial we will create a preferences activity that can be invoked using the application menu.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;First of all we&#39;ll need to create the xml that contains all the preferences in the settings menu. Go to the layout directory of your application&#39;s project and add a new file preferences.xml.&lt;br /&gt;&lt;br /&gt;Add this code:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&amp;gt;&lt;br /&gt;    &amp;lt;CheckBoxPreference&lt;br /&gt;        android:key=&quot;pref_mute&quot;&lt;br /&gt;        android:title=&quot;@string/pref_mute&quot;&lt;br /&gt;        android:summary=&quot;@string/pref_mute_summ&quot;&lt;br /&gt;        android:defaultValue=&quot;false&quot; /&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/PreferenceScreen&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;The key property is used as an identificator for this setting. It has to be a unique string. The title value is the text representation of the setting, and summary is the description of the setting item.&lt;br /&gt;&lt;br /&gt;Go to strings.xml file to set the missing string values:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;br /&gt;&amp;lt;resources&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;string name=&quot;app_name&quot;&amp;gt;CodeForFood Test Two&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute&quot;&amp;gt;Mute&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;pref_mute_summ&quot;&amp;gt;Disable all the sound in the application&amp;lt;/string&amp;gt;&lt;br /&gt;    &amp;lt;string name=&quot;menu_settings&quot;&amp;gt;Settings&amp;lt;/string&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/resources&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Create a new Activity in the same directory as the MainActivity.java class, call it TestPreferences.java.&lt;br /&gt;&lt;br /&gt;Here&#39;s the code that applies the xml layout of the preferences page:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.preference.PreferenceActivity;&lt;br /&gt;&lt;br /&gt;public class TestPreferences extends PreferenceActivity {&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;    super.onCreate(savedInstanceState);&lt;br /&gt;    addPreferencesFromResource(R.layout.preferences);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now go to AndroidManifest.file of the application and add this line to the application node:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;activity android:name=&quot;TestPreferences&quot;&amp;gt;&amp;lt;/activity&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Doing so will tell Android that there&#39;s an Activity with that name in our application.&lt;br /&gt;&lt;br /&gt;Now go to MainActivity.java class, add some code that creates an options menu and handles item selection in the menu. When the Settings item is selected, the preferences screen pops up:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;package com.example.codeforfoodtest_two;&lt;br /&gt;&lt;br /&gt;import android.app.Activity;&lt;br /&gt;import android.content.Intent;&lt;br /&gt;import android.os.Bundle;&lt;br /&gt;import android.view.Menu;&lt;br /&gt;import android.view.MenuItem;&lt;br /&gt;&lt;br /&gt;public class MainActivity extends Activity{&lt;br /&gt;&lt;br /&gt;static final int IDM_SETTINGS = 101;&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;protected void onCreate(Bundle savedInstanceState) {&lt;br /&gt;super.onCreate(savedInstanceState);&lt;br /&gt;setContentView(R.layout.activity_main);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onCreateOptionsMenu(Menu menu){&lt;br /&gt;menu.add(Menu.NONE, IDM_SETTINGS, Menu.NONE, R.string.menu_settings);&lt;br /&gt;return(super.onCreateOptionsMenu(menu));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean onOptionsItemSelected(MenuItem item){&lt;br /&gt;if(item.getItemId()==IDM_SETTINGS){&lt;br /&gt;Intent intent = new Intent();&lt;br /&gt;intent.setClass(this, TestPreferences.class);&lt;br /&gt;startActivity(intent);&lt;br /&gt;}&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;And that&#39;s all for today! Changing the preference doesn&#39;t really do anything yet, but we&#39;ll work on that next time.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/3992235796346494930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-78.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3992235796346494930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/3992235796346494930'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-78.html' title='Android beginner tutorial: Part 78 - Creating Preferences Activity'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2631617199049210138.post-8048414480995009135</id><published>2013-02-09T10:00:00.000+02:00</published><updated>2013-02-09T10:00:02.795+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android beginner tutorial"/><title type='text'>Android beginner tutorial: Part 77 - Settings</title><content type='html'>Today we start learning about settings and preferences in Android.&lt;br /&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;Settings can be an important part of some Android applications. They allow users to change the behavior of the application using a list of components that let the user control their preferences.&lt;br /&gt;&lt;br /&gt;There is a native way of providing modifiable settings for your application using the Android&#39;s Preference APIs.&lt;br /&gt;&lt;br /&gt;To create a settings Activity for your application, you don&#39;t need to display and handle all the Views on your own. The whole process can be done by providing an XML file which is built using subclasses of the Preference class.&lt;br /&gt;&lt;br /&gt;Each Preference is an item in a list and provides an approporiate native way to modify a setting. Each Preference that you add has a key-value pair that is used to save the respective setting in a SharedPreferences file for your application&#39;s settings. The file is used to read the setting values in your application to modify its behaviour.&lt;br /&gt;&lt;br /&gt;The values stored in that file for each setting can be one of the following types: Boolean, Float, Int, Long, String or Set of Strings.&lt;br /&gt;&lt;br /&gt;The class used for building the settings Activity is PreferenceActivity. It is supported by all Android versions. There is another way to display the settings screen for Android versions 3.0 and higher - that is to create a normal Activity and use a PreferenceFragment inside of it that displays the settings.&lt;br /&gt;&lt;br /&gt;As I said before, each setting in the application is represented by an instance of a Preferences subclass. Some of the most commonly used ones are CheckBoxPreference, ListPreference and EditTextPreference. &lt;br /&gt;&lt;br /&gt;The CheckBoxPreference component is an item with a checkbox and works like a switch - possible values are true and false, so the data type is Boolean.&lt;br /&gt;&lt;br /&gt;The ListPreference class is an item that opens a dialog with a list of radio buttons that the user can select. The saved data type can be any type listed above.&lt;br /&gt;&lt;br /&gt;The EditTextPreference preference opens a dialog that lets the user input their text, saved data type is a String.&lt;br /&gt;&lt;br /&gt;And that will be all for today. Next time we&#39;ll try creating a preference screen ourselves.&lt;br /&gt;&lt;br /&gt;Thanks for reading!</content><link rel='replies' type='application/atom+xml' href='http://kirill-poletaev.blogspot.com/feeds/8048414480995009135/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-77.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8048414480995009135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2631617199049210138/posts/default/8048414480995009135'/><link rel='alternate' type='text/html' href='http://kirill-poletaev.blogspot.com/2013/02/android-beginner-tutorial-part-77.html' title='Android beginner tutorial: Part 77 - Settings'/><author><name>Kirill Poletaev</name><uri>http://www.blogger.com/profile/10345000564393362213</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='20' src='http://1.bp.blogspot.com/_l1TO0E4U6fA/S9Hf-vF3wSI/AAAAAAAAABw/HUFpPKEajsQ/S220/dealwithit.gif'/></author><thr:total>0</thr:total></entry></feed>