Posts

Showing posts with the label israel tabadi

Bullets List in Android?

  A much needed, sometimes hidden in the framework, and mimicked with unnecessary WebViews and sort of inner HTML injections to xml resources – a bullet list is much easier to implement then you would expect. When taking the easy solution – using WebViews , you are making the UI hierarchy much heavier then it should be, and when your UI is already rich with layouts and widgets, it seems bad to overload it with heavy WebViews. The Android.text framework gives us – the developers an easy to use API to build special text spans, in the next code snippet we’ll see how a bullet list can be implemented: let’s say we have a simple TextView which we need to hold a text with a bullet at the left hand of it: TextView mTv = (TextView)findViewById(R.id.tv_1); CharSequence cs = getText(R.String.sample_text); SpannableString ss = new SpannableString(cs); ss.setSpan(new BulletSpan(15), 0, cs.length(), 0); mTv.setText(ss);   Explanation: 1. Get a hook on a TextView 2. Construct ...

Xamarin – Use of an ORM , SQLite.Net

  When dealing with a mobile application, storage is a feature we cannot live without, whether it’s to handle relational data or persistent data in general, which is queried for different operations throughout it’s lifecycle. Most if not all mobile platforms offer SQLite database storage method which consists of a helper class and some method of initialization and querying which are both declared by the programmer. In most cases, it’s just some boilerplate code consisting of declaring table names, column names and types, primary keys, foreign keys.. and all that jazz. But we are discussing cross platform native development here, let’s see what’s what in the area of local storage in Xamarin. Ok, so the classical model of a cross platform application consists of a Core project, which is actually a .Net class library, and a couple of platform specific projects which have a link to the Core project. Each of the platform specific projects have – you guessed correctly, platform specif...

Introduction to Android native development using Xamarin’s Mono for android.

  It’s been couple of months since I’ve had a chance to post here, but it’s been an amazing time for me in terms of adapting new/old technologies for my development skills, and I have a bag full of new knowledge which has been accumulated and is waiting to be put on the blog, So in the coming weeks I’ll start posting a lot of code samples, architectural guidelines and how-to-do’s. In My professional career as a software developer, the first contact I had with OOP was with C#, back in 2007 as part of a course I took for web development which involved mostly ASP.Net, Windows forms and at the time - the diapers of WPF and Silverlight which were the latest buzz by Microsoft along with c# 3.0 and the 2.5 .Net framework. As time passed, I took on Java in order to acquire the skills which will allow me to program for the Android platform, which is my (Native) passion in the last 3 years or so. Developing for Android in Java is really something all java programmers are able to adapt to as...

The SELA Developer Practice Android app

Image
  For the last couple of weeks, I’ve been working on an App which offers the users interactive information regarding the Sela Developer Practice conference to take place on may 5th and will include dozens of lectures and international speakers which will present new technologies and trends, as well as advanced topics in the fields of Client/Server and Cloud/ALM and VS. The app communicates with couple of web services to consume the JSON data which is than processed on the client to show it from different points of view and arrange it in dynamically generated views. I took the inspiration from both the official website http://www.seladeveloperpractice.com and the app for Google’s IO which is published every year. The app shows info on the Speakers including twitter and blogs, as well as extensive info on the abstract of each session. You are welcome to try the app and rate in on Google Play: https://play.google.com/store/apps/details?id=com.selagroup.sdpmay2013

Properly save battery Life Throughout App’s lifecycle

On every Google IO, many advocates for the Android platform demonstrate and teach us how to build great apps. The term “Great Apps” consists of many aspects – user experience, performance, saving battery life, not wasting expensive 3G quota and more..in this post i’ll try to demonstrate a simple way to not waste battery life in an Animation oriented activity. Let’s say that after displaying a nifty splash screen, your main activity consists of some animations which act in some sort of an infinite loop, to change pictures, articles, maybe polling data from a web service (asynctask of some sort). needless to say, that all of these processes take a lot of juice from the battery, so why worry about it? well, if we are focused in that activity, there’s not much we can do because it’s in the foreground and the user probably has some interactions with the current activity. but what happens once it’s not focused anymore? all these tasks don’t just stop themselves, it’s in our respons...

Actionbar implementation in earlier Android versions

  The ActionBar was introduced in Android 3.0 (API 11) and enable us to develop applications with a more fluid navigation using Fragments. Ever since, we are using the Action Bar in pretty much every application. The question is, how to be able to implement the Action bar in earlier < 3.0 API’s ? The support library which enables us to use Fragments for devices with API<3 is great and does the work just like we want and need. but what about action bar support? it’s not out of the box even in the support library. the excepted method of implementing Action bar for those earlier versions is actionbarsherlock(or ABS) just to keep things short. The ABS is an open-source expansion of the Android support library and it enables us to write application without having to make a implicit adjustment to out activities in order to support Action bars. The usage is simple: 1. download the project from the ABS website at : http://actionbarsherlock.com/ 2. inside the archive you’ll fi...

Utilizing Http response cache

     A typical mobile application will probably consume some resources from a web server using http requests. This repeated task has some drawbacks: Bandwidth has a cost, polling the same data over and over again results in a waste of user’s quota. the responsiveness and user experience is compromised once the user keeps waiting for data. If the requested data hasn’t changed on the server, then there’s no reason for us to fetch it. Of course, local caching of data is a must, but we also need to be able to refresh the data if it has changed on the server. A simple solution which comes out of the box for API 4+ is http response caching. Http response caching is a mechanism which enables us as developers to save a footprint of the response utilizing the abilities of the Http protocol. Basically, is holds a simple cache of the response which can be defined and enabled in code, and it prevents from fetching unchanged data from the server which was already fetched a...

Design Patterns in Android

       As Android developers or developers in general, we encounter the Design Patterns paradigms pretty much in every job. Most of us as I roughly assume – go over some of the more common design patterns as an integral part in preparing for a job interview.      It is common to assume that software developers should be familiar with what design patterns are and should be able to fire couple of them and explain their usage in modern software architecture. the common patterns that you’ll probably be asked to explain are Singleton, Factory, Strategy and maybe some more exotic ones.      So the question asked, is how do classical Design patterns come handy when planning the  architecture of an android app?      The answer to this somewhat bizarre question can be divided into two mini answers : The platform which we are developing for is not important. In other words, the fact that we are dealin...

ORM for android

     ORM – Object relational mapping has been around for quite some time now. Basically it’s a piece of software (regardless of targeted platform), which simplifies the connection between objects (as in OOP) and data in database (mostly used is tables based relational database), and it exists in order to help simplify bridging the gap between the two in our development process.     Microsoft's Entity framework is a great example for a robust easy to use framework which allows creating and querying the database from an object’s point of view with classic object oriented programming paradigms while maintaining all DB construction’s needs such as relationships between objects (tables) such as one to many or many to many including all the indexing, optimization etc..      In android, we have SQLite as a database platform, from simple to advanced application, it’s likely to assume that we’ll need some relational database to store and r...

How to update UI from a different thread

     Google has set it’s tone on some tough guidelines for developing android apps, basically there aren’t any strict rules but one very easy to misunderstand. there is a single Thread which is called the “main” thread, or “UI thread” on some places. this is the thread which is in charge on the views and interaction with the users gestures. based on the guidelines of any mobile platform at the moment, all the work that shouldn’t be done on the main thread should be done on background thread. this can be achieved in many ways, you can use AsyncTask, Or A Handler, Or a simple thread with an implementation of the   run() method within the declaration of the runnable. ok, so if I’m pulling some server data, or performing long calculations, pulling stuff from or to the Sqlite database I’ll probably use an AsyncTask. but let’s say for instance that I want to simply update the UI once every couple of seconds (update a TextView or an ImageView’s content etc..),...

How to maintain global application state

       In Android application, it’s sometimes important to keep a global application state. by global application state I mean to be able to have an entry point to the application which gives us a starter place to do stuff before the application starts. this is important in many aspects, for example, the application can start from different entry points, such as the launcher, a notification, or a service, or even by responding to a certain broadcast.     There are many approaches to achieve this goal, one of them which a prefer is the Application Class, once extending the application class, we are able to load resources, setting different contexts , or even starting a certain service before the first activity starts, this is done simply by overriding the onCreate() method. Example: public class MyApplication extends Application { boolean hasAgreed; @Override public void onCreate() { // TODO Auto-generated method stub ...

Fragments–applications made modular

  The Android SDK 3.0 (Honeycomb) introduced the Fragments API. The Fragments API is basically a set of tools which allows us to create, manage and re-use Chuncks of logic and UI. For example, let’s assume I’m starting to develop an Application, which should be compatible for both Smartphones and Tablet, as well as support both orientation(landscape, portrait). It’s fairly easy to say that the same “lisviews” or forms will be used on both versions in terms of business logic and behavior with some UI adjustments. the fragments API is exactly what we need in order to do it in an elegant manner. let’s check out the following sample: public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); Button btn = (Button) findViewById(R.id.button); btn.setOnClickListener( new OnClickListener() { ...

Testing Android Applications

Android Testing testing in Android application can be really tricky, there’s a lot to test if we want to deliver a proper application which supports as much users as posible, here are a few tips on the what to test and how to test: Testing in Android: · Screen sizes Testing how the app is shown in different screen sizes · OS versions Apps can behave differently or even crush in some OSD versions due to lack of targeting to specific device · Densities Screen densities change from device to device, and from a UI point of view this can be real issues, having apps to look far from the designers aim · Resolutions When not targeting a specific resolution the graphics are used with OS manipulations like stretch and scale, which can be resulting in a disastrous UI and UX for the user · Functionality Basic functionality testing of the app in terms of business logic · Usability The usability angle is very important once we are handling a small device, a lack of attention to the UI and user e...

תכנות לאנדרואיד בקלות פרק 15 – שימוש במצלמה

אחד השימושים היותר נפוצים במכשיר נייד בכלל וב smartphone בפרט הינו צילום תמונות, זהו דבר שכולנו עושים ואם נוכל לכלול פונקציונאליות שכזאת באפליקציות שלנו, זה בוודאי רק יוסיף. מה אנחנו צריכים בשביל לצלם תמונה? נניח לצורך הדוגמא כי יש לנו איזשהו layout עם 2 אלמנטים: - כפתור למעבר למצב מצלמה (פתיחת צמצם) Button - Imageview אשר יציג את התמונה 1. בקובץ ה manifest שלנו יש לציין permission לגישה למצלמה, זאת ע"י <uses-feature android:name="android.hardware.camera" /> 2. הרעיון הוא פשוט מאוד, אנו נפעיל Capture Intent אשר נקרא :  android.provider.MediaStore.ACTION_IMAGE_CAPTURE מה שיעביר אותנו ל Activity של המצלמה, בonActivityResult נקבל בחזרה את התמונה כחלק מה intent שיחזור מה CameraActivity(של מערכת ההפעלה) כפי שלמדנו נקבל את הintent בחזרה דרך  onActivityResult() ומשם נגזור את התמונה עצמה שחוזרת כ Bitmap עם ה key שנקרא “data” (חלק מהתיעוד של android) ולאחר מכן נציג את התמונה באותו ImageView שיועד לכך מלכתחילה. public void onCreate(Bundle savedInstanceState...

תכנות לאנדרואיד בקלות פרק 14 – עבודה מול Sensors

מכשיר האנדרואיד שלנו מציע לנו בתור מפתחים ובתור משתמשים מגוון רחב של חיישנים, אותם אנו יכולים לרתום לשימושנו, ולשלב באפליקציות על מנת להעשיר את חוויית השימוש ולהציע שימושיות ברמה מאוד גבוהה למשתמשים. דוגמא לחיישנים : אור, תנועה, שדה מגנטי, תאוצה, אוריינטציה, סוללה.. וכו.. דוגמא: כרגיל אנו מדברים על Activity, שימו לב שישנו יישום של SensorListener ע"י: implements SensorListener המתודה onSensorChanged() הינה זאת שנותנת לנו לדגום את השינויים ולהציג על המסך , למעשה אנו דוגמים את האוריינטציה של המכשיר בכל רגע נתון, וכמובן המתודה  onAccuracyChanged()  כותבת ללוג את השינויים ברמת הדיוק. נקבל מופע של SensorManager  ע"י מערכת ההפעלה, ונרצה לדגום כמה נתונים: כאשר האפליקציה היא ב forground ולא ברקע, נרצה להרשם לשינויים בחיישנים אלה, כדאי לעשות את זה כאשר האפליקציה אינה ברקע על מנת לחסוך בסוללה, מכיוון שהאזנה לשינויים אלה צורכת משאבים לא מעטים. וכמובן נרצה לא להאזין לשינויים כאשר האפליקצייה ברקע בדיוק מאותם השיקולים. public class SensorDemo extends Activity implem...

תכנות לאנדרואיד בקלות פרק 13 – עבודה ברקע – Services

תכנות לאנדרואיד בקלות פרק 13 – עבודה ברקע – Services מהם services? למעשה ניתן לסווג אותם כתהליכים שרצים ברקע, ללא תלות באפליקציה לדוגמא: שעון הוא service, שעון מעורר, מזג אוויר, מד סוללה, כל אלה הם services שאינם "אפליקציות במלוא מובן המילה" ורוב הפעמים גם אין להם ממשק משתמש – UI מדוע אנחנו צריכים services? מכיוון שאנחנו צריכים תהליכים שירוצו ברקע ללא התערבות שלנו, לדוגמא: Facebook, במידה וישנם עדכונים רלוונטים, אני צריך לקבל עדכון, דהיינו יש update service שרץ ברקע ודוגם את השרת כל X שניות/דקות לקבלת מידע חדש, כנ"ל עבור gmail, calendar וכו.. מה אנחנו צריכים בשביל לפתח service? 1. להצהיר עליו ב manifest שלנו, בדיוק כמו שהיינו עושים עם Activity בצורה הבאה: <service android:enabled="true" android:name=".MyService" /> כאשר ה name שלו הוא אותו השם של ה Class אותו נכתוב בסעיף הבא. 2. לכתוב את ה Service עצמו: מדובר ב service שמתחיל לנגן קובץ מדיה כשלוחצים על play, ועוצר כשלוחצים על stop כמו כן, מציג חיווים מתאימים לגבי הstate של ה serv...

תכנות לאנדרואיד בקלות פרק 12 – מיקבול תהליכים - Threads

בכל פעם שאנו יוצרים אפליקציית אנדרואיד, Thread שנקרא "main" נוצר באופן אוטומטי. Thread זה המכונה גם UI THREAD, הוא חשוב מאוד משום שהוא אחראי על שיגור האירועים ליישומונים המתאימים וזה כולל את אירועי ה draw. זה גם Thread שמנהל אינטראקציה עם פקדים באנדרואיד. לדוגמה, אם אתה נוגע בכפתור על מסך, Thread UI משגר אירוע שבעצם נותן פקודה למערכת ההפעלה לפעול בהתאם. מודל Thread זה יכול להניב ביצועים גרועים ביישומי אנדרואיד מכיוון שכל הפעולות מתבצעות על אותו ה Thread, כגון עבודות חישוב ארוכות, גישה לשרת, הורדה של קבצים. אלה תהליכים שחייבים לפעול באופן מקביל ולא על ה UI thread , זאת על מנת לשמר מצב שבו האפליקציה מגיבה ולא קורסת. במידה וה UI THREAD תפוס למשך 5 שניות, אנו נקבל תוצאת ANR- Application not responding שיתבטא בקריסה של האפליקציה. מכאן מגיעה החשיבות הגדולה בעבודה Multi threaded , ותיכף נדגים כיצד זה מתבצע. public void onClick(View v) { new Thread(new Runnable() { public void run() { Bitmap b = loadImageFromNetwork(); mImageView.setImageBitmap(b); } }).start(); } ...

תכנות לאנדרואיד בקלות פרק 11 – בסיסי נתונים Sqlite

באפליקציית mobile יש חשיבות רבה לשמירת נתונים, החשיבות נובעת מכמה סיבות עיקריות: 1. שמירה של נתונים עבור המשתמש, העדפות שלו, הגדרות וכו. 2. במידה ואין חיבור זמין לרשת, יש לאפשר לו לעבוד במצב לא מקוון. 3. תוצאות משחקים, data של אפליקצייה – יש לשמור אותם מקומית. 4. להמעיט את הגישה לשרת לצורך אחזור של מידע קבוע, זאת על מנת לחסוך גם בסוללה וגם בעלויות של מפעיל סלולרי. ישנן כמה דרכים לשמור מידע במכשיר שלנו, בפרק זה נתרכז בבסיס נתונים רלציוני מאוד נפוץ בשם sqlite Sqlite נפוצה בהרבה פלטפורמות והיא מאוד רזה, אך מאפשרת פונקציונאליות כמעט מלאה כפי שתצפו מכל בסיס נתונים רלציוני. class DB extends SQLiteOpenHelper {     final static int DB_VERSION = 1; final static String DB_NAME = "mydb.db";     Context context;     public DB(Context context) {         super(context, DB_NAME, null, DB_VERSION);         // Store the context for later use     ...

תכנות לאנדרואיד בקלות פרק 10 – ניידות בין מסכים שונים והעברת מידע בין Activities (Implicit, Explicit)

אוקיי, עד עכשיו יצרנו כמה מסכים עם כמה ממשקים נחמדים וצבעוניים, יש תפריט שהגדרנו שמאפשר לגשת להגדרות וכו.. איך אנחנו מחברים את הכל? 1. קריאה לactivity מתוך activity קיים (מעבר למסך חדש) 2. העברה של מידע ל activity 3. קבלת תשובה מactivity ל activity שקרא לו (onActivityResult) 4. implicit intent בפרק זה נלמד: הגדרה: Intent הינו אחד מהרכיבים המרכזיים במערכת ההפעלה ומהווה אבן בניין מאוד חשובה בבנית אפליקציה. Intent למעשה הוא מסר, שמכיל מספר מאפיינים וניתן להעמיס עליו data. 1. קריאה ל activity מתוך activity קיים (מעבר למסך חדש) על מנת להתחיל activity מתוך activity נוכחי יש ליצור Intent חדש, הבנאי של Intent מקבל שני פרמטרים (יש לו עוד כמה overloads אבל נתרכז כרגע באחד מהם).   Intent intent = new Intent(getApplicationContext(),NewActvitity.class); הפרמטר הראשון הוא ה context של ה activity הנוכחי, והפרמטר השני הוא השם של ה class של ה Activity השני .שאליו רוצים לעבור. לאחר הגדרת ה Intent יש לקרוא למתודה startActivity באופן הבא: startActivity(intent); ...