Posts

Showing posts from August, 2012

Gradient in XML– easy as pi

  Tired of using solid background colors but you think it’s a bit trouble to use you favorite graphics tool too create interesting backgrounds for you controls or layouts? well, creating gradients in your android app is easy enough, here’s an example: In your drawable folder , create an xml file. the next code creates a gradient which is from top to bottom, with start color of white and end color of somewhat orange, it begins to get a strong fade after 90% of the measure of the container view. <? xml version ="1.0" encoding ="utf-8" ? > < selector xmlns:android ="http://schemas.android.com/apk/res/android" > < item > < shape > < gradient android:angle ="90" android:startColor ="#fff" android:endColor ="#FFA812" android:type ="linear" android:centerY ="0.9" /> </ shape > </ item

Boil an egg app - step by step (step 1 - planning)

Image
     In this tutorial , I will demonstrate how I built the app, from the design, to the logic and the code itself.    This app was written for educational purposes and is free do download on the market at :  Boil An Egg   - for devices with API 1.6 and above.   So what's the deal with this app?   it's main goal is to provide users with an easy and useful tool to boil an egg, sounds funny and a bit redundant, but for me it's actually a real need, for I'm known to make boiled eggs which are too soft, or on the other hand -too hard. so I thought to myself that there's must be an app on the play store which will enable me to time the eggs so they'll be perfect each time.   After searching the market and installing couple of apps I found that they are too complicated for my needs so I decided to write an app for my own.   1. name for the app: simple is good – “Boil An Egg”      package name: because I work at Sela group, I wanted to        leverage t

How to upload an app to the Google Play store?

     You wrote an amazing app, it works on various resolutions and many tested and trusted devices, so the next step is to upload it to the Google Play store ! but wait, what's the best way to do it? This link explains all the important things you should do in order to prepare your app for publishing we'll cover the more important stuff. follow these next steps and you'll be on the market in no time: 1. Go over the code and remove all unnecessary code (Logs..unused code, unused     resources..etc..) 2. Choose a good package name - this will be your identifier for the store (on upgrade/update     we must use the same package name) 3. remove the android:debuggable attribute from your manifest file if exists 4. Don't forget an icon for the app 5. If you're supporting different resolutions - include matching graphics resources for them 6. We'll be using Eclipse's export wizard in order to get our app to "release" mode     To create a sign

A simple Countdown Timer in Android

Of course we can implement a simple timer using threads but there's a better way out of the box.      The Android SDK offers us a rather simple way to use timers in our apps, Let's see how it works: CountDownTimer timer = new CountDownTimer(maxmillis, 1000) { @Override public void onTick(long lastknowtimer) { int seconds = (int) (lastknowtimer / 1000) % 60 ; int minutes = (int) ((lastknowtimer / (1000*60)) % 60);   tvTimerLabel.setVisibility(View.VISIBLE); tvTimer.setText(String.valueOf(minuts*seconds));     } @Override public void onFinish() { tvTimer.setText("finished"); } }; The  abstract class -  CountdownTimer's constructor receives two parameters (maxmillis which is the amount of milliseconds to countdown from, and the second parameter is the interval in which the tick happens). It has two methods which must be implemented:   1. onTick()   - the action which should happen on every tick (su

How to set the drawableRight resource by code?

Image
     Sometimes we want to include a small icon inside a Button, how to achieve this? Prepare your PNG file and put it your res/drawable folder along with the other image resources in your application. just like any other view handling - this can be done by Code or by XML. By XML                    <Button                        android:id="@+id/buttonId"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_gravity="center"                        android:drawableRight="@drawable/timer_red"                        android:text="@string/some_text"                     /> this is good for an initial state of the button, in some cases it will change dynamically to other icon (change position within the button etc..) By Code btTimer.setCompoundDrawablesWithIntrinsicBounds (0, 0,R.drawable.timer_red, 0);

How to play a simple audio file in android

     In our application, it's sometimes necessary to play an audio file. The android sdk offers us a simple way to do it. let's examine the small snippet of code which does it: MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.audiofile); try { mPlayer.prepare();      } catch (IllegalStateException e) { e.printStackTrace();      } catch (IOException e) {         e.printStackTrace();      } mPlayer.start(); explanation: MediaPlayer -  MediaPlayer class can be used to control playback of audio/video files and   streams.  we need to pass : context and an id of the audio file, we will save the  f ile under the "raw" folder under the familiar "res" folder. after which we must call mPlayer.prepare() -  Prepares the player for playback, synchronously.  After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For files, it is OK to call prepare(), which blocks until M

Android Face Detection API

A cool feature I came across couple of days ago is the "face recognition" API available from SDK 4.0 (API level 14). Basically, it allows us to do is to detect faces from a bitmap , but also detect specific facial featues, such as eyes and mouth. How : The Camera class allows you programmers to set a face detection listener with a callback method which is called when a face is detected. this method is passed with an Array which contains data for each face it detects. Each face object in the array holds: 1. A unique ID which it is tracking so you'll be able to distinguish between the faces detected in the picture. 2. Data with the location of the user's eyes and mouth 3. Data with the boundary of the entire face A tutorial will be published soon enough

Android Annotations - cleans your code

     Did you ever find yourself starting a new project or even a new activity and going through all the tiring process off binding UI controls to XML elements such as TextView , EditTexts , ListView etc .. ? I know I did. but there's a solution for this, along with some other code shortening elements for us to use. presenting : Android Annotations -  http://androidannotations.org/ A free to use library which can be added quite easily to an Android project with some minor calibrations. what does it allow us to do ? (taken from the androidannotations website) Dependency injection : inject views, extras, system services, resources, ... Simplified threading model : annotate your methods so that they execute on the UI thread or on a background thread. Event binding : annotate methods to handle events on views,   no more ugly anonymous listener classes ! REST client : create a client interface, AndroidAnnotations generates the implementation. AndroidAnnotations provide

It's all about ListViews

     One of the most frequent components used in an android application is a   ListView , it's basically a UI component which displays a list of items (simple String items or richer items), with an   adapter   which can be bound to a   ListView   and a collection of some sort. What do we need in order to create a   ListView ? Two main options: 1. within the context of an Activity which extends the   Activity   Class 2. Within the context of an Activity which extends   ListActivity (that being said if the whole activity is a      list) We’ll look at the first option 1. Create an Activity, and a matching Xml layout file.     In the layout file, add a   ListView   control using   <ListView></ListView> , don't forget to give it        layout_width,layout_height,id.     for example:     <ListView           android:id="@+id/your_listview_id"           android:layout_width="fill_parent"           android:layout

Native or Non Native?

     You know exactly what i'm talking about. Every once in a while, could be at work, or with other friends from the industry, this guy is going on and on about how magical and easy to use and cross platform is HTML 5. And there we are.. me and all of you android JAVA developers, sometimes wondering if it's true.. will all apps in the future be written in HTML and JS under various nifty platform such as PhoneGap or Sencha touch or Appaccelarator's Titanium studio.. or other great platforms. I find myself contemplating on that question from time to time, and to be honest - it's never a 100% native answer. let me explain why, It's a true fact indeed that native application will be better in practically every way, for the following reasons: Better user experience, using the native controls of android. no lagging from browser..you are not dependent on browser version (supporting html5 specific apis or no) the ability to target specific devices (due to di