tag:blogger.com,1999:blog-89710580166409671312024-02-07T11:40:43.379-08:00Android BuddysThis blog is intended to be a useful help util for android developers, a kind of buddy if you want. It shall be a guide for those, who are looking for new cool stuff regarding android and its development.Anonymoushttp://www.blogger.com/profile/16931235036087382032noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-8971058016640967131.post-3811811975869464032015-07-06T03:09:00.000-07:002015-07-06T04:53:32.155-07:00How to show data from cache and simultaneously refresh it from network with RxJava This blog post is inspired by the <a href="http://blog.danlew.net/2015/06/22/loading-data-from-multiple-sources-with-rxjava/">blog post</a> of Dan Lew, who is explaining how you can load data from multiple sources with RxJava in a single stream. It would probably make sense to read his post first, because I'm referring to it quite well (no worries, it's short).<br />
<br />
I like the idea of having a single data stream responsible for fetching data, either from the cache or from the network, so I don't have to care where it comes from while I would like to fetch and display it.<br />
I also like the approach to get fresh data from the network once your current data is stale...<b>but </b>unfortunately there is one drawback in the approach of Dan. The client has to know when it's data is stale, and that's not always the case, at least for me. In my application the client always asks the server for fresh data and if the server doesn't has any, it responses with a 304 (nothing changed).<br />
<br />
So the use case for the client I would like to achieve is the following:<br />
- fetch data from cache/database and display it<br />
- request data from server in background simultaneously<br />
- if the server has fresh data, persist it in the database and update the UI with the fresh data<br />
- if the server doesn't have fresh data, just do nothing since the data from cache is already displayed<br />
- do this by using a single stream<br />
<br />
Since we know already by Dan Lew how to load the data from multiple sources in a single stream, the first thing to achieve what I would like to have from above is to remove the <code class=" language-java=">first()</code> call to be able to run all source observables after each other:
<br />
<br />
<pre><code class=" language-java=">// Our sources
Observable<Data> cache = getCacheObservable();
Observable<Data> network = getNetworkObservable();
// Retrieve the first source with data
Observable<Data> source = Observable
.concat(cache, network)
<strike>.first();</strike>
</code></pre>
<br />
Now the cache and network observables are running both, but as said before <b>after each other </b>and not simultaneously because that's what <code class=" language-java=">concat()</code> does. Fortunately there is a method of RxJava which also solves this problem and it's called <code class=" language-java=">merge()</code>. It does basically the same as <code class=" language-java=">concat()</code> but in parallel.
<br />
<br />
<pre><code class="language-java">Observable<Data> source = Observable
.merge(cache, network);
</code></pre>
<br />
Since we want to have all this stuff happening in the background but display it int he UI we need to take care of it by using the right schedulers:
<br />
<br />
<pre><code class="language-java">Observable.merge(cache.subscribeOn(Schedulers.newThread(),
network.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((data) -> handleShowData(data));
</code></pre>
<br />
Here it's important to subscribe <b>each</b> of the single observables on a new thread to ensure it will run in parallel. Otherwise if you would subscribe only the resulting observable on a new thread it would run in background but still after each other (like if we would use <code class=" language-java=">concat()</code>).<br />
<br />
<h2>
<b>Conclusion</b></h2>
<div>
I think this is a nice and clean way to get data synced on the client side and I hope you think the same. I didn't cover the part of how to store the data in the database, because it's already covered by the post of Dan Lew. </div>
<div>
But I have created a small and simple application on <a href="https://github.com/TobiasBuchholz/DataLoadingWithRxJava/blob/master/app/src/main/java/com/tobishiba/dataloadingwithrxjava/MainActivity.java">github</a> which fulfills all the points from my use case above, so check it out :)</div>
Anonymoushttp://www.blogger.com/profile/16931235036087382032noreply@blogger.com0tag:blogger.com,1999:blog-8971058016640967131.post-9144872434789986052014-11-27T13:54:00.001-08:002014-12-08T11:02:46.896-08:00Let your washing machine tell you when it's finished<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">I had a nice idea for lazy people like me:</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Wouldn't it be cool if your washing machine told you when it is done cleaning
your dirty undies?</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Some of you may think now: "wtf?" or "why would I need
this?"</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">I always have the same problem: I turn on my washing machine at 8 o'clock
in the evening, then I start working on my laptop till around midnight and by
that time I have totally forgotten about the laundry. After having worked I am
normally tired and I just want to go to bed, but when I come into the bathroom
(where my washing machine is waiting for me) I see the red LED's blinking,
basically yelling at me "Heeey! Did you forget about me? YOU HAVE TO HANG
THE FUCKING LAUNDRY! AND IT IS NOT JUST SOME T-SHIRTS! I HAVE MANY SOCKS AND
UNDIES!!!!". </span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">So I have to hang the laundry absolutely tired and annoyed. </span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">To put an end to this and not be annoyed every fucking time I come into the
bathroom at midnight I thought: How can the washing machine just tell me when
it is finished?</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">There are some washing machines that make beeping noises when they are
finished - but I don't own any of those and I don't want to spend the money for
a new one.</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">At AVM GmbH I found a nice device called Dect!200 which is a
switchable socket connected to the Fritz!Box via DECT. This socket can measure
the voltage flow and you can check the data and work with it in the web
interface of the Fritz!Box.</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Let's get the washing machine a bit smarter</span></b><b><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></b><br />
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"><br /></span></b>
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Things you need:</span><br />
<br />
<ul>
<li><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Fritz!Box from AVM GmbH (I had the 7490, costs: ca. 199€, you can use any Fritz!Box which supports DECT)</span></li>
<li><span style="color: #111111; font-family: Arial, sans-serif; font-size: 19px; line-height: 1.3;">AVM FRITZ!DECT 200 (<span style="color: black; font-size: 18px; line-height: normal;">switchable smart socket, costs </span>ca. 45€)</span></li>
<li><span style="color: #111111; font-family: Arial, sans-serif; font-size: 19px; line-height: 1.3;">Time to spend: max. 30 minutes</span></li>
</ul>
<br />
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"><br /></span></b></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Easy idea: measure the voltage when the washing machine is done and in
standby mode. Now I can trigger the socket to turn off when the standby voltage
is flowing for some time, e.g. 10 minutes. </span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<br />
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Step 1:</span></b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"> Go to the web interface
and navigate to home network (Heimnetz) -> Smart Home</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjd2BJyEwrbEYF-ZKMk70yGZ_5d8ffRXPeFZO0-tuqUTu_0hQyY51lRVefR2GpSbXzu8-UL1i1oEH9C2GpaQhDzIDCS07dOqfNHQwfPy5S1rWzUl0AHlenQA51LU4CVtr6CQJBjYi7OS9Sp/s1600/smarthome.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Verdana, sans-serif;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjd2BJyEwrbEYF-ZKMk70yGZ_5d8ffRXPeFZO0-tuqUTu_0hQyY51lRVefR2GpSbXzu8-UL1i1oEH9C2GpaQhDzIDCS07dOqfNHQwfPy5S1rWzUl0AHlenQA51LU4CVtr6CQJBjYi7OS9Sp/s1600/smarthome.jpg" height="257" width="640" /></span></a></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">There you choose the settings menu of the Dect!200. I called it
"Waschmaschine" (washing machine).</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<br />
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Step 2:</span></b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"> Go to the tab
"Automatisch Schalten" (not sure how it's called in English, maybe
something like automatic)</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirMSt3GJ35saloLEcBURmxyLi6vOWKh20u2fDnwdA8D4qZ5aJn66d-jKFQ1-xRNdy4UemS8NrD8q9FFg4qMjJAiule7m0YnPEGCVxQnkv5aCyMUbbNoTNj17FZgMR7ptGjmQo-DrQogmfT/s1600/automatischschalten.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Verdana, sans-serif;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirMSt3GJ35saloLEcBURmxyLi6vOWKh20u2fDnwdA8D4qZ5aJn66d-jKFQ1-xRNdy4UemS8NrD8q9FFg4qMjJAiule7m0YnPEGCVxQnkv5aCyMUbbNoTNj17FZgMR7ptGjmQo-DrQogmfT/s1600/automatischschalten.jpg" height="304" width="640" /></span></a></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Step 3:</span></b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"> Scroll down to
"Abschalten bei Standby" (turn off on standby) and enter the Watt
value which your machine has in standby mode. To know the value just turn on
the machine and wait a bit - go to the Smart Home section and read the actual
consumption. In my case it was 1,14 Watt so I put in 1,15 just to be sure and
10 minutes, also just to be sure. You can also set it to 5 minutes but that
will not make the cake big.</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBwiKAXFDBAFfAjYpx5hXfoxzxfA1cYiAiUdrjg3BcffZKs7vdJOFNEQQibK1AfWcx1POoKTsafqLGiUzXW4AnMbyjottCG7Cp9v3bcmPDVDCZnvFkKi2kcr2efiBBU38gSmBsZu2PvDPI/s1600/leistungsschwelle.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Verdana, sans-serif;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBwiKAXFDBAFfAjYpx5hXfoxzxfA1cYiAiUdrjg3BcffZKs7vdJOFNEQQibK1AfWcx1POoKTsafqLGiUzXW4AnMbyjottCG7Cp9v3bcmPDVDCZnvFkKi2kcr2efiBBU38gSmBsZu2PvDPI/s1600/leistungsschwelle.jpg" height="362" width="640" /></span></a></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Done - but this will not tell me when the washing machine is done - it will
just shut off the yelling LED's which would make it even more likely for me to forget
about the laundry and leave it in the machine until I try to wash some clothes
the next time.</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Push Notification</span></b><b><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Thanks to the Fritz!Box there is a push notification feature which sends
you an e-mail every time there is a toggle, i.e. the socket has been switched
on or off. I just configured this and BAM! I get an email every time the
washing machine turns on and off (because you have to toggle the socket).</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">When I receive an e-mail I get a push notification on my smart phone:</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">(the text says: "your washing machine was turned off")</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVGgmZb5Hy7ihaKbqv45NyD6i1SmM1doIWEuOHeZK21BK1eU2f0lEb0w8SDNhbN0gRcmbCKuHOtMehYycp3u48V10PZtkOgWV_dYsxHEqG2vMoKDepk2Bp6u-HMhUzxYUfbhoc-JDvmPCM/s1600/mail.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Verdana, sans-serif;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVGgmZb5Hy7ihaKbqv45NyD6i1SmM1doIWEuOHeZK21BK1eU2f0lEb0w8SDNhbN0gRcmbCKuHOtMehYycp3u48V10PZtkOgWV_dYsxHEqG2vMoKDepk2Bp6u-HMhUzxYUfbhoc-JDvmPCM/s1600/mail.jpg" height="640" width="360" /></span></a></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span><br />
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Nice side effect:</span></b><span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;"> now I am
getting informed when my girlfriend is using my washing machine and I can
immediately react by sending her some smilies and kisses from where ever I am
to say "THANKS for washing my shit!" </span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Now that I know I can trigger stuff - I am planning some new cool things.</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">The next project will be: How can I turn off the lights in the living room after
having forgotten to turn them after having
hung my laundry while being extremely tired and annoyed from the yelling LED's?</span><span lang="EN-US" style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<br /></div>
<br />
<div class="MsoNormal" style="background: white; margin-left: 0cm;">
<span lang="EN-US" style="font-family: Arial, sans-serif; font-size: 13.5pt;">Answer: By creating a power cable with luster terminals on each side and connect
it with my overhead lights and the Dect!200 switchable socket! I will
explain how to do this next time.</span><br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/07921547075459694423noreply@blogger.com0tag:blogger.com,1999:blog-8971058016640967131.post-48180058670409859192014-08-13T07:54:00.001-07:002014-08-13T07:54:43.934-07:00SnappingSeekBarI have developed my own implementation of a snapping seek bar. It automatically creates indicators depending on how it is configured. The seek bar is able to display short texts for each indicator. If an indicator gets tapped, the thumb animates smoothly to the tapped indicator. The thumb can also be slided and after releasing it snaps to the most nearest indicator. The seek bar is very customizable: you can change several colors, drawables or strings for the items you are using.
</br>
</br>
The usage is very simple. You can put it in your xml or add it programmatically:
</br>
</br>
<b>From xml:</b>
</br>
</br>
<div style="background: #f0f0f0; overflow:auto;width:auto;"><pre style="margin: 0; line-height: 125%"><span style="color: #666666"><</span>com<span style="color: #666666">.</span><span style="color: #4070a0">tobishiba</span><span style="color: #666666">.</span><span style="color: #4070a0">SnappingSeekBarSample</span><span style="color: #666666">.</span><span style="color: #4070a0">views</span><span style="color: #666666">.</span><span style="color: #4070a0">SnappingSeekBar</span>
<span style="color: #002070; font-weight: bold">android:</span>layout_width<span style="color: #666666">=</span><span style="color: #4070a0">"match_parent"</span>
<span style="color: #002070; font-weight: bold">android:</span>layout_height<span style="color: #666666">=</span><span style="color: #4070a0">"wrap_content"</span>
<span style="color: #002070; font-weight: bold">app:</span>progressDrawable<span style="color: #666666">=</span><span style="color: #4070a0">"@drawable/seekbar_progress"</span>
<span style="color: #002070; font-weight: bold">app:</span>thumb<span style="color: #666666">=</span><span style="color: #4070a0">"@drawable/seekbar_thumb"</span>
<span style="color: #002070; font-weight: bold">app:</span>progressColor<span style="color: #666666">=</span><span style="color: #4070a0">"@color/blue"</span>
<span style="color: #002070; font-weight: bold">app:</span>thumbnailColor<span style="color: #666666">=</span><span style="color: #4070a0">"@color/blue_light"</span>
<span style="color: #002070; font-weight: bold">app:</span>indicatorColor<span style="color: #666666">=</span><span style="color: #4070a0">"@color/white"</span>
<span style="color: #002070; font-weight: bold">app:</span>textIndicatorColor<span style="color: #666666">=</span><span style="color: #4070a0">"@color/white"</span>
<span style="color: #002070; font-weight: bold">app:</span>textSize<span style="color: #666666">=</span><span style="color: #4070a0">"12dp"</span>
<span style="color: #002070; font-weight: bold">app:</span>indicatorSize<span style="color: #666666">=</span><span style="color: #4070a0">"12dp"</span>
<span style="color: #002070; font-weight: bold">app:</span>itemsArrayId<span style="color: #666666">=</span><span style="color: #4070a0">"@array/big_indicators_items"</span><span style="color: #666666">/></span>
</pre></div>
</br>
</br>
<b>From code:</b>
</br>
</br>
<div style="background: #f0f0f0; overflow:auto;width:auto;"><pre style="margin: 0; line-height: 125%"><span style="color: #007020; font-weight: bold">private</span> SnappingSeekBar <span style="color: #06287e">createSnappingSeekBarProgrammatically</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #007020; font-weight: bold">final</span> SnappingSeekBar snappingSeekBar <span style="color: #666666">=</span> <span style="color: #007020; font-weight: bold">new</span> SnappingSeekBar<span style="color: #666666">(</span><span style="color: #007020; font-weight: bold">this</span><span style="color: #666666">);</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setProgressDrawable</span><span style="color: #666666">(</span>R<span style="color: #666666">.</span><span style="color: #4070a0">drawable</span><span style="color: #666666">.</span><span style="color: #4070a0">seekbar_progress</span><span style="color: #666666">;</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setThumbDrawable</span><span style="color: #666666">(</span>R<span style="color: #666666">.</span><span style="color: #4070a0">drawable</span><span style="color: #666666">.</span><span style="color: #4070a0">seekbar_thumb</span><span style="color: #666666">);</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setItems</span><span style="color: #666666">(</span><span style="color: #007020; font-weight: bold">new</span> String<span style="color: #666666">[]{</span><span style="color: #4070a0">"Wow"</span><span style="color: #666666">,</span> <span style="color: #4070a0">"such"</span><span style="color: #666666">,</span> <span style="color: #4070a0">"amazing"</span><span style="color: #666666">});</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setProgressColor</span><span style="color: #666666">(</span>resources<span style="color: #666666">.</span><span style="color: #4070a0">getColor</span><span style="color: #666666">(</span>R<span style="color: #666666">.</span><span style="color: #4070a0">color</span><span style="color: #666666">.</span><span style="color: #4070a0">green_darker</span><span style="color: #666666">));</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setThumbnailColor</span><span style="color: #666666">(</span>resources<span style="color: #666666">.</span><span style="color: #4070a0">getColor</span><span style="color: #666666">(</span>R<span style="color: #666666">.</span><span style="color: #4070a0">color</span><span style="color: #666666">.</span><span style="color: #4070a0">yellow_light</span><span style="color: #666666">));</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setTextIndicatorColor</span><span style="color: #666666">(</span>resources<span style="color: #666666">.</span><span style="color: #4070a0">getColor</span><span style="color: #666666">(</span>R<span style="color: #666666">.</span><span style="color: #4070a0">color</span><span style="color: #666666">.</span><span style="color: #4070a0">red_darker</span><span style="color: #666666">));</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setIndicatorColor</span><span style="color: #666666">(</span>resources<span style="color: #666666">.</span><span style="color: #4070a0">getColor</span><span style="color: #666666">(</span>R<span style="color: #666666">.</span><span style="color: #4070a0">color</span><span style="color: #666666">.</span><span style="color: #4070a0">green_light</span><span style="color: #666666">));</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setTextSize</span><span style="color: #666666">(</span><span style="color: #40a070">14</span><span style="color: #666666">);</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setIndicatorSize</span><span style="color: #666666">(</span><span style="color: #40a070">14</span><span style="color: #666666">);</span>
snappingSeekBar<span style="color: #666666">.</span><span style="color: #4070a0">setOnItemSelectionListener</span><span style="color: #666666">(</span><span style="color: #007020; font-weight: bold">this</span><span style="color: #666666">);</span>
<span style="color: #007020; font-weight: bold">return</span> snappingSeekBar<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
</pre></div>
</br>
</br>
Just check it out on <a href="https://github.com/TobiasBuchholz/SnappingSeekBar">github</a> or at <a href="https://play.google.com/store/apps/details?id=com.tobishiba.SnappingSeekBarSample">Google Play</a>! Anonymoushttp://www.blogger.com/profile/16931235036087382032noreply@blogger.com0tag:blogger.com,1999:blog-8971058016640967131.post-53751753793940706812014-08-12T06:35:00.001-07:002014-08-12T06:35:26.816-07:00How to create an ProgressCircle instead of a ProgressBar<h2>
<span style="font-size: small; font-weight: normal;">In the picture you see the result of this little tutorial. A ProgressCircle where the progress is visualized as a circle from 0% to 100%.</span></h2>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICeze-nMHU71zbb39cFQZBFvNg2dFKnACbyfgRKMbzc-NamFczpr01ZkcXWAY81LMR0DkcpMzA20w4mnMli2rrRpjUN8gyuhQ6dNjdm-3lsOXMgCTMTY1l1GGFsE8S1ieUeBWpJbpq3La/s1600/ProgressCircle.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjICeze-nMHU71zbb39cFQZBFvNg2dFKnACbyfgRKMbzc-NamFczpr01ZkcXWAY81LMR0DkcpMzA20w4mnMli2rrRpjUN8gyuhQ6dNjdm-3lsOXMgCTMTY1l1GGFsE8S1ieUeBWpJbpq3La/s1600/ProgressCircle.jpg" height="191" width="200" /></a></div>
<br /></div>
<div>
First of all you will need a new progress drawable.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><?xml version="1.0" encoding="UTF-8" ?></span>
<span style="color: #007700;"><layer-list</span> <span style="color: #0000cc;">xmlns:android=</span><span style="background-color: #fff0f0;">"http://schemas.android.com/apk/res/android"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><item</span> <span style="color: #0000cc;">android:id=</span><span style="background-color: #fff0f0;">"@android:id/background"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><shape</span>
<span style="color: #0000cc;">android:innerRadiusRatio=</span><span style="background-color: #fff0f0;">"2.2"</span>
<span style="color: #0000cc;">android:shape=</span><span style="background-color: #fff0f0;">"ring"</span>
<span style="color: #0000cc;">android:useLevel=</span><span style="background-color: #fff0f0;">"false"</span>
<span style="color: #0000cc;">android:thicknessRatio=</span><span style="background-color: #fff0f0;">"90.0"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><solid</span> <span style="color: #0000cc;">android:color=</span><span style="background-color: #fff0f0;">"@color/white"</span><span style="color: #007700;">/></span>
<span style="color: #007700;"></shape></span>
<span style="color: #007700;"></item></span>
<span style="color: #007700;"><item</span> <span style="color: #0000cc;">android:id=</span><span style="background-color: #fff0f0;">"@android:id/progress"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><shape</span>
<span style="color: #0000cc;">android:innerRadiusRatio=</span><span style="background-color: #fff0f0;">"2.2"</span>
<span style="color: #0000cc;">android:shape=</span><span style="background-color: #fff0f0;">"ring"</span>
<span style="color: #0000cc;">android:thicknessRatio=</span><span style="background-color: #fff0f0;">"25.0"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><solid</span> <span style="color: #0000cc;">android:color=</span><span style="background-color: #fff0f0;">"@color/attribute_green"</span><span style="color: #007700;">/></span>
<span style="color: #007700;"></shape></span>
<span style="color: #007700;"></item></span>
<span style="color: #007700;"></layer-list></span>
</pre>
</div>
<br />
Inside of the drawable xml you have the background and the progress of the ProgressBar.<br />
After creating the ProgressDrawable you have to put it as progressDrawable to the ProgressBar Widget as shown below:<br />
<br /></div>
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007700; line-height: 125%;"><ProgressBar</span></pre>
<pre style="line-height: 125%; margin: 0;"> <span style="color: #0000cc;">android:id=</span><span style="background-color: #fff0f0;">"@+id/manufacture_count_progress"</span>
<span style="color: #0000cc;">style=</span><span style="background-color: #fff0f0;">"?android:attr/progressBarStyleHorizontal" </span>
<span style="color: #0000cc;">android:layout_width=</span><span style="background-color: #fff0f0;">"100dp"</span>
<span style="color: #0000cc;">android:layout_height=</span><span style="background-color: #fff0f0;">"100dp"</span>
<span style="color: #0000cc;">android:rotation=</span><span style="background-color: #fff0f0;">"90"</span>
<span style="color: #0000cc;">android:indeterminate=</span><span style="background-color: #fff0f0;">"false"</span>
<span style="color: #0000cc;">android:layout_centerInParent=</span><span style="background-color: #fff0f0;">"true"</span>
<span style="color: #0000cc;">android:max=</span><span style="background-color: #fff0f0;">"100"</span>
<span style="color: #0000cc;">android:progress=</span><span style="background-color: #fff0f0;">"10"</span>
<span style="color: #0000cc;">android:progressDrawable=</span><span style="background-color: #fff0f0;">"@drawable/progressbar"</span> <span style="color: #007700;">/></span></pre>
</div>
<br />
Done! Now you will see a CircleProgressBar. You can use it the exact same way like with the normal drawable. Have Fun.Anonymoushttp://www.blogger.com/profile/07921547075459694423noreply@blogger.com0tag:blogger.com,1999:blog-8971058016640967131.post-19000607104150687152014-08-02T09:32:00.001-07:002014-08-02T09:54:33.850-07:00<span style="font-family: Verdana, sans-serif;">ähm...hello world :)</span><br />
<br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%">@Override
<span style="color: #000080; font-weight: bold">public</span> <span style="color: #000080; font-weight: bold">void</span> onCreate(<span style="color: #000080; font-weight: bold">final</span> Bundle savedInstanceState) {
<span style="color: #000080; font-weight: bold">super</span>.<span style="color: #FF0000">onCreate</span>(savedInstanceState);
Toast.<span style="color: #FF0000">makeText</span>(<span style="color: #000080; font-weight: bold">this</span>, <span style="color: #0000FF">"Hello world!"</span>, Toast.<span style="color: #FF0000">LENGTH_LONG</span>).<span style="color: #FF0000">show</span>();
}
</pre></div>
Anonymoushttp://www.blogger.com/profile/16931235036087382032noreply@blogger.com0