![]() | ![]() |
Source for Activity - AndroidListViewLayoutActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | package com.as400samplecode; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class AndroidListViewLayoutActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); //Generate list View from ArrayList displayListView(); } private void displayListView() { //Array list of countries List<String> countryList = new ArrayList<String>(); countryList.add( "Aruba" ); countryList.add( "Anguilla" ); countryList.add( "Netherlands Antilles" ); countryList.add( "Antigua and Barbuda" ); countryList.add( "Bahamas" ); countryList.add( "Belize" ); countryList.add( "Bermuda" ); countryList.add( "Barbados" ); countryList.add( "Canada" ); countryList.add( "Costa Rica" ); countryList.add( "Cuba" ); countryList.add( "Cayman Islands" ); countryList.add( "Dominica" ); countryList.add( "Dominican Republic" ); countryList.add( "Guadeloupe" ); countryList.add( "Grenada" ); countryList.add( "Greenland" ); countryList.add( "Guatemala" ); countryList.add( "Honduras" ); countryList.add( "Haiti" ); countryList.add( "Jamaica" ); //create an ArrayAdaptar from the String Array ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>( this , R.layout.country_list, countryList); ListView listView = (ListView) findViewById(R.id.listView1); // Assign adapter to ListView listView.setAdapter(dataAdapter); //enables filtering for the contents of the given ListView 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 toast = Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP, 25 , 300 ); toast.show(); } }); } } |
Source for Main Screen Layout - main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:padding = "10dp" android:text = "@string/some_text" android:textSize = "20sp" /> < ListView android:id = "@+id/listView1" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginBottom = "40dp" /> < RelativeLayout android:layout_width = "wrap_content" android:layout_gravity = "bottom" android:id = "@+id/bottomLayout" android:layout_height = "match_parent" android:layout_marginTop = "-40dp" > < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "5dp" android:text = "Some Random Text at the Bottom" android:textAppearance = "?android:attr/textAppearanceMedium" /> </ RelativeLayout > </ LinearLayout > |
Source for List Layout - country_list.xml
1 2 3 4 5 6 7 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:padding = "10dp" android:textSize = "16sp" > </ TextView > |
Source for application variables - strings.xml
1 2 3 4 5 6 7 8 9 10 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "app_name" >Android ListView</ string > < string name = "some_text" > Just some random Text inside our view before we display a list. We are going to display some North American Countries! </ string > </ resources > |
Source for application manifest - AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <? xml version = "1.0" encoding = "utf-8" ?> package = "com.as400samplecode" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "15" /> < application android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" android:theme = "@android:style/Theme.Light" > < activity android:name = ".AndroidListViewLayoutActivity" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.