Thursday, December 2, 2010

Android phone app listview with ( icon, title, description ) simple

Бүрэн гүйцэт ажиллана

download or save into res/drawable/icon.png,  res/drawable/menu.png

res/drawable/gradient_background_grey.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#424242"
        android:endColor="#222222"
        android:angle="0"
     />
    <corners android:radius="0dp" />
</shape>


res/layout/listview.xml

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <ImageView
                android:id="@+id/ImageView01"
                android:src="@drawable/menu"
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"></ImageView>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical"
                      android:layout_height="fill_parent"
                      android:layout_width="fill_parent">
            <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFF00"></TextView>
            <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:textColor="#0099CC"></TextView>
        </LinearLayout>
    </TableRow>
</TableLayout>


res/layout/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">
    <TextView
            android:id="@+id/TextView01"
            android:layout_height="wrap_content"
            android:background="@drawable/gradient_background_grey"
            android:text="List of Country &amp; their denotation"
            android:textStyle="normal|bold"
            android:gravity="center_vertical|left"
            android:layout_width="fill_parent"></TextView>
    <ListView
            android:id="@+id/ListView01"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:duplicateParentState="false">
    </ListView>
</LinearLayout>


src/package/MainActivity.java

import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return country.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listview, null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.TextView01);
                holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(curr[position]);
            holder.text2.setText(country[position]);

            return convertView;
        }

        static class ViewHolder {
            TextView text;
            TextView text2;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView l1 = (ListView) findViewById(R.id.ListView01);
        l1.setAdapter(new EfficientAdapter(this));
    }

    private static final String[] country = { "Iceland", "India", "Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia",
"Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Iceland", "India", "Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia",
"Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
"Luxembourg"};
    private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD",
"EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD",
"LTL ", "EUR", "ISK", "INR", "IDR", "IRR", "IQD",
"EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD",
"LTL ", "EUR"};

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example">
    <application android:icon="@drawable/icon" android:label="Simple Android Project">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

No comments: