Friday, February 18, 2011

ListView of Data from SQLiteDatabase - Android

This is a next level tutorial where I am mixing two concepts – The ListView concept that is explicitly explained in the ListView Tutorial and the SQLiteDB concept in into own tutorial.

Here I am intending to query a database using a SQLiteDatabase API. The results I obtain are in a Cursor object that I iterate and create an ArrayList that is passed to the ListView. Let us see the steps involved in this exercise:

In the onCreate(…) method I have 2 methods corresponding to the two steps described above:

openAndQueryDatabase();
       
      displayResultList();

Let us see what each of them does:

Here is the first step:

private void openAndQueryDatabase() {
            try {
                  DBHelper dbHelper = new DBHelper(this.getApplicationContext());
                  newDB = dbHelper.getWritableDatabase();
                  Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " +
                        tableName +
                        " where Age > 10 LIMIT 4", null);

            if (c != null ) {
                  if  (c.moveToFirst()) {
                        do {
                              String firstName = c.getString(c.getColumnIndex("FirstName"));
                              int age = c.getInt(c.getColumnIndex("Age"));
                              results.add("Name: " + firstName + ",Age: " + age);
                        }while (c.moveToNext());
                  }
            }                
            } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (newDB != null)
                  newDB.execSQL("DELETE FROM " + tableName);
                  newDB.close();
        }

      }       

Now, DBHelper is a class I have written extending the SQLiteOpenHelper class. All that it does is create a database by name “sample”, create a table within it and insert values into the table. It does this if the database does not already exist.  The table name is “Resource” and the columns in the table are Lastname, Firstname, Country, Age.

The code for this example can be downloaded here and you can look into the DBHelper code as well (which I do not want to elaborate here)

So, from the DBHelper class we get an open database which we call the newDB. Using this handle, we query the table for values. To keep it simple, while retrieving values I have hard-coded the column names which need not be the case. So I run a rawQuery and get the results into a Cursor.

Next I iterate through the Cursorand populate the results into an ArrayList of Strings “result”.  In the finally block, I not only close the database but before that I delete all the entries inserted into the database by the DBHelper class just to clean up.

Next how do I display the results in a ListView. If you know ListView– how it works it is rather simple. Else you could look at the ListViewTutorial. Here is the method:

      private void displayResultList() {
            TextView tView = new TextView(this);
        tView.setText("This data is retrieved from the database and only 4 " +
                  "of the results are displayed");
        getListView().addHeaderView(tView);
       
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);
           
      }

The only additional bit I have done here is to add a HeaderView (which is using the concept of programming UI explained in another tutorial).

I have written this tutorial based on some of the requests in my blog. Hope this helps.

Tuesday, February 1, 2011

Android Developers Blog: Android 3.0 Platform Preview and Updated SDK Tools

Android Developers Blog: Android 3.0 Platform Preview and Updated SDK Tools

Being in the domain of developing applications for large enterprises, what looks to me as interesting features of Android 3.0:
1. Enhancements for the Enterprise such as encrypted storage and password expiration!
2. And a UI Framework to suit larger screen devices.

Currently any enterprise that develops mobile applications has to duplicate its efforts towards developing and testing on each of the platforms like Android, Windows, Blackberry, iPhone etc. (Though I know there are a host of "write-once, deply on multi-platforms' options like AppMobi, Monotouch, Corona from Anscamobile, they need to mature a lot more). I hope this will help in consolidating development effort towards: "write once, run anywhere" for mobile devices.

I am looking at the Android market picking up rapidly and becoming the de-facto JVM equivalent in the mobile space...

How easy could life get with writing an app for one mobile and bingo! it works on all possible mobile devices that exist!! or at least majority of them! :)


Friday, January 14, 2011

Android 2.3 is here and what does it bring?

Android 2.3 (Gingerbread) has been released on the 6th of December 2010. While it is still a minor release, Android 3.0 (Honeycomb) would probably bring greater surprises. Atleast I am hoping that there would be the "Wow" factor with it.

Here are a few features of the Gingerbread version:
  • Updated user interface design
  • Support for extra-large screen sizes and resolutions (WXGA and higher)
  • Native support for SIP VoIP telephony
  • Support for WebM/VP8 video playback, and AAC audio encoding
  • New audio effects such as reverb, equalization, headphone virtualization, and bass boost
  • Support for Near Field Communication
  • System-wide copy–paste functionalities
  • Redesigned multi-touch software keyboard
  • Enhanced support for native code development
  • Audio, graphical, and input enhancements for game developers
  • Concurrent garbage collection for increased performance
  • Native support for more sensors (such as gyroscopes and barometers)
  • A download manager for long running downloads
  • Improved power management and application control
  • Native support for multiple cameras
  • Switched from YAFFS to the ext4 filesystem
  • An Integrated task Manager
Alongside the new platform, Google is also releasing updates to the Software Development Kit (SDK) tools (r8), Native Development Kit (NDK) and Android Development Tools (ADT) plug-in for Eclipse (8.0.0).
    The new features are simplified debug builds, integrated ProGaurd support, HierarchyViewer improvements and a preview of new UI Builder.  For a few of the reviews you can go here:

    Friday, December 3, 2010

    Android UI – Inflate from XML (Dynamic UI Creation)


    We have seen that we can declare User Interface in Android through XML or we can even create the UI Programmatically. Now, we will see how we can mix the two and use it for building dynamic User Interfaces. One example of such a case would be when on a particular action of the user on one of the UI elements, you need more UI elements to appear on the same screen. How do we achieve this?
    You can do it in two ways:
    1.       The dynamic part of the UI can be created programmatically. However we saw in my earlier tutorial that it is not a very good way to mix UI and code.  So, we can
    2.       Define the dynamic UI too as an XML and use XML inflation to include it into the existing UI.
    We will see how to do the 2ndway, which probably is a good practice too.
    As always, again a very simple example. Assume I have a very simple linear layout. In that I want to include a button. I can do it as part of the mail XML itself. However, assume that this button is supposed to be reused in many activities and hence I have defined it as a separate XML. How do I include it into the main XML?
    So, here is the main.xml
    <?xml version="1.0"encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/layout1"
        >
    </LinearLayout>
    Here is the buttons.xml that is also created in the res/layout folder:
    <?xml version="1.0"encoding="utf-8"?>

    <Button xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/button_small_left"
     style="?android:attr/buttonStyleSmall"
            android:text="Press to close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    And here is the Activity’s onCreate(…) method of the InflateView class:
        public voidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            final LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            Button b = (Button)inflater.inflate(R.layout.buttons,
                    null);
           
            lLayout = (LinearLayout)findViewById(R.id.layout1);
            lLayout.addView(b);
    First 3 lines must be familiar. In the 4thline, I am getting a handle to the LayoutInflater through the getSystemService(…) method.  This inflater has a method inflate to which I pass the buttons.xml by passing the parameter R.layout.buttons. Then, I try to append this button to the Linear Layout that already exists and is set as the view in line 2 setContentView(R.layout.main). How to append? I get a handle to the LinearLayoutlLayout and add the new button to it in the last line!
    That simple to inflate an XML and append it to an existing view!
    However, I have gone ahead and added another bit to this program as shown below:
            b.setOnClickListener(new OnClickListener() {
               
                public void onClick(View v) {
                      //restrict to adding only 1 textview child element
                      if (lLayout.getChildAt(2) == null)
                      {
                      TextView tv = (TextView)inflater.inflate(R.layout.text, null);
                      lLayout.addView(tv);
                      }
                }
            });
    On the click of this dynamically added button, I am showing how we can add more to the UI dynamically through inflation. Assume, on the click of the button, you want to show some new text. This TextView is defined in another XML called text.xml which is also in the res/layout folder.
    So, I am inflating from this XML and appending it to the LinearLayout view. So, a lot can be achieved for dynamic UI through inflation.

    You can download the complete sample code here.

    Thursday, December 2, 2010

    Creating Android UI Programmatically


    So far, in all my examples, I have been using the declarative way of creating an Android UI using XML. However, there could arise certain situations when you may have to create UI programmatically. Sincere advice would be to avoid such a design since android has a wonderful architecture where the UI and the program are well separated. However, for those few exceptional cases where we may need too… here is how we do it.
    Every single view or viewgroupelement has an equivalent java class in the SDK. The structure and naming of the classes and methods is very similar to the XML vocabulary that we are used to so far.
    Let us start with a LinearLayout. How would we declare it in an XML?
    <?xml version="1.0"encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
    </LinearLayout>
    This just contains a TextViewembedded in a LinearLayout. A very trivial example. But serves the purpose intended. Let me show how almost every single element here corresponds to a class or a method call in the class.  So the equivalent code in the onCreate(…)  method of an activity would be like this:
             super.onCreate(savedInstanceState);
          
            lLayout = newLinearLayout(this);
            lLayout.setOrientation(LinearLayout.VERTICAL);
            //-1(LayoutParams.MATCH_PARENT) is fill_parent or match_parent since API level 8
            //-2(LayoutParams.WRAP_CONTENT) is wrap_content
            lLayout.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.MATCH_PARENT));
            tView = newTextView(this);
            tView.setText("Hello, This is a view created programmatically! " +
                      "You CANNOT change me that easily :-)");
            tView.setLayoutParams(new LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT));
            lLayout.addView(tView);
            setContentView(lLayout);

    Like this any layout view can be created. But from this small example you can notice two outstanding things – very tedious to code for every attribute of the view. And any simple change in the view, you need to change the code, compile, deploy and only then you see the effect of the change – unlike in a layout editor. 


    You can download the sample code here.

    Wednesday, December 1, 2010

    Android Eclipse link Error - New Project

    I set up a new environment on my new laptop with Windows7 and jdk1.6.0_21 version. Did the initial setup with eclipse installation, android SDK installation, installing the ADT plugin on eclipse etc. I then imported all the android projects I had created so far. It worked perfectly fine.
    Next I created a simple project without any custom stuff, it failed to create with a link error as shown in the image below:

    After much trial and error, setting up everything afresh, changing or creating a new workspace, nothing seemed to work. I noticed that the Android package was not visible in the Java build path that you get to see in the project properties. So, finally I found a manual workaround to overcome this problem if you mandatorily want to continue working in the same workspace
    Solution: Same workspace:
    The manual resolution I found is to make a classpath entry in the .classpath file outside the eclipse environment.
    The .classpath file may have such an entry before you edit it:
    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
         <classpathentry kind="src" path="src"/>
         <classpathentry kind="src" path="gen"/>
         <classpathentry kind="output" path="bin"/>
    </classpath>

    So add an entry for the android framework to be included as shown below:
    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
         <classpathentry kind="src" path="src"/>
         <classpathentry kind="src" path="gen"/>
         <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
         <classpathentry kind="output" path="bin"/>
    </classpath>

    What actually corrupted my workspace, i have not yet figured out. However, while searching on the web for all possible solutions, I came across the announcement on the eclipse forums that Eclipse 3.3 to 3.6 on windows platform is not stable with the JDK version -  jdk1.6.0_21. Hence we need to either roll back to patch 20 or upgrade to 22. I have upgraded and see that eclipse is far more stable now.
    Please see this link for more details on the windows and jdk problem mentioned above:
    Solution: Different or new workspace:
    Just upgrade your JDK to jdk1.6.0_22 and point eclipse to use this as the default JDK. Create a new workspace and you are done. It fixes the problem.
    Hope this helps many not to waste time on searching a solution that is not to do with the way you are coding but with the environment set up itself. I had to spend quite a bit of my precious time before figuring this out. 

    Sunday, September 19, 2010

    Sample Code Download Link

    Hi,

    I got a lot of requests for alternate download sites (for the Sample Code). So, I have put all the sample code into one page and here is the link to the same. Hope this helps you.

    https://sites.google.com/site/saigeethamn/home/android-tutorial-code-for-download