Archive

Archive for June, 2011

Android Lists: ListActivity and ListView I – Basic Usage

June 16, 2011 1 comment

The Android ListActivity makes it simple to display a list of items in your application. If the main purpose of a screen (Activity) in your mobile application is to present a list of choices or content to the user, use ListActivity instead of Activity. If all you want to do is display a list to the user and nothing more, you don’t even need to create a layout definition for the activity. The ListActivity has a default layout that provides a ListView for you. However, if you wish to customize the way your ListActivity looks, the single required element in your layout definition is an object with an id of @android:id/list.

<!–more–>

If your layout doesn’t have an object with an id of @android:id/list when you go to launch your application, you’ll be greeted with a “The application … has stopped unexpectedly. Please try again.”. Looking at the debug output, you can see that the exception raised states “Your content must have a ListView whose id attribute is ‘android.R.id.list’”.

Let’s say you wanted to create an application that would list all of the stock ticker symbols that you have invested in. You could create a simple layout definition file that looks something like this:

res/layout/main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<ListView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@android:id/list”
/>
</LinearLayout>

src/com/austinrasmussen/stockviewer/StockList.java

package com.austinrasmussen.stockviewer;

import android.app.ListActivity;
import android.os.Bundle;

public class StockList extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

Now, that’s not very exciting, since all it will give us is a black screen when you try to run it. In order to provide the ListView with content to display, you need to associate a ListAdapter with the ListView. A ListAdapter is essentially the data source object that the ListView pulls its rows from. For now, let’s just display a static list of stock ticker symbols from an array. The Android framework provides you with the ArrayAdapter class that makes it straightforward to assign an array to a ListView.

You can add the static list stock ticker symbols to the ListView by placing the following two lines of code after the setContentView call.

String[] tickerSymbols = new String[] { “MSFT”, “ORCL”, “AMZN”, “ERTS” };
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
tickerSymbols));

It was extremely straightforward to add the ticker symbols to the list mostly because this example used the Android framework’s ArrayAdapter and simple_list_item_1 TextView resource. The reference to android.R.layout.simple_list_item_1 is simply a single TextView, so using this resource limits you to only displaying a string of text in the list item.

What if you wanted to display the ticker symbol and the latest quote?

One option would be to simply append the quote on to the end of the ticker symbol (i.e. MSFT – 25.48); however, that means that you’re stuck with the same formatting and layout for both pieces of information.

The second option, which would allow you to treat each piece of information uniquely in terms of layout and formatting requires some more work since neither ArrayAdapter nor simple_list_item_1 will be usable. This is because ArrayAdapter only takes in a TextView resource, and simple_list_item_1 only contains a single TextView. I’ll cover how you would do this in my next post 🙂

Final Code

res/layout/main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<ListView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@android:id/list”
/>
</LinearLayout>

src/com/austinrasmussen/stockviewer/StockList.java

package com.austinrasmussen.stockviewer;

import android.app.ListActivity;
import android.os.Bundle;

public class StockList extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String[] tickerSymbols = new String[] { “MSFT”, “ORCL”, “AMZN”, “ERTS” };
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
tickerSymbols));
}
}

Advertisements
Categories: ListView

Android Lists: ListActivity and ListView II – Custom Adapter and List Item View

June 16, 2011 1 comment

You’ve got a functioning list of stock ticker symbols now; however, it’s just a list of symbols and nothing more. Let’s explore how to add a static stock quote to each of the ticker symbols.

First, you can design the layout of how the stock ticker and quote should appear in the list. This is accomplished by creating a new layout file under res\layout. The following shows a simple row layout with two TextViews placed side-by-side horizontally:

Read more…

Categories: Uncategorized

How to disable Key Lock

June 16, 2011 Leave a comment

For avoiding key lock, we can use Keyguard Manager instance to get the activity of Keyguard services.

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

Categories: Uncategorized

Android Lists: ListActivity and ListView Part – III – Basic Layout

June 16, 2011 1 comment

Now, it’s a good start, but it would be even better if it was more readable. There is plenty of empty space on the screen right now, so we might as well use it. For starters, we can space out the ticker symbol and the quote, as well as adjust the font size to make everything easier to read.
If we pop open the res/layout/stock_quote_list_item.xml file, we can see that it’s quite plain right now, and it specifies very minimal layout specific attributes. The LinearLayout that the list item is currently using makes it so that all elements in the LinearLayout container are displayed one after another in a single row (or column, depending on orientation). Instead, we can switch the list item to use RelativeLayout, which allows us to specify the layout of the elements in the RelativeLayout container in relation to the parent element, which is the actual RelativeLayout container in this case.

Read more…

Categories: ListView

JSON in Android – Details

June 16, 2011 Leave a comment

Overview
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson is an open-source project hosted at http://code.google.com/p/google-gson.

Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
Read more…

Categories: Uncategorized

Custom List : List with Check box and AutoLink(WebView)

June 15, 2011 1 comment

Custom List with Check box :

Using Custom List with check box allows you to manage multiple selection in a list and manage the multiple shared data. Generally used for Setting activity and for list selection for multiple item (few or all of them) available in the list.

example also included auto link , which redirect the link url to webview directly.

Read more…

Categories: ListView

How to post a non blocking HTTP request in Android?

June 15, 2011 Leave a comment

This code fetches content from the web without blocking the UI (runs in the background in a Thread). Once finished, it posts a Handler that is picked up by the UI as soon as possible.

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

This code fetches content from the web without blocking the UI (runs in the background in a Thread). Once finished, it posts a Handler that is picked up by the UI as soon as possible.
Read more…

Categories: Uncategorized
%d bloggers like this: