Sunday, November 23, 2014

Tutorial 12 : Sort List Example

Hi, guys! Sorry for long time, didn't update my blog because recently are busy with my personal stuff. In this tutorial, we will learn how to sort the list by ascending or descending order. This able to done by using sort() method and comparator class.

1. Sort Method

Ascending Order

By referring tutorial 9, we are creating a list view layout and put some elements in an array. Now we would like to modify the previous project in order to sort the them in ascending order. Use the Arrays.sort(items) command to sort them as shown in Figure 12.1.

public class ListMonth extends ListActivity {

    static final String[] MONTHS = new String[] { "January", "February",
             "March", "April", "May", "June", "July", "August",
             "September", "October", "November", "December" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Arrays.sort(MONTHS);

       setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_list
       _month, MONTHS));

        ListView listView = getListView();
        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                   int position, long id) {
               // When clicked, show a toast with the TextView text
               Toast.makeText(getApplicationContext(),
               ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
             }
        });

    }
Figure 12.1

Friday, November 21, 2014

Tutorial 11 : Web View Example

Hi, everyone. Do you feel fun with create your own Android application? In this tutorial, we will learn how to navigate a web pages by using web view layout in Android application. It is useful when the application need to show something from the web. It might be easy as just click a button or text to surf the web or net.

1. Create Android Layout

We need to create two Android layout for this tutorial. One is for the main activity layout and another one is for the web activity layout, they are shown in Figure 11.1 and Figure 11.2 respectively.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".webapplication">

    <Button
        android:id="@+id/btnGoogle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Google"
        android:padding="10sp"
        android:textSize="20sp" />

</RelativeLayout>
Figure 11.1

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
Figure 11.2