Animal Pink Pig pigpng animals add new Animal Orange Tiger tigerpng animals add

Animal pink pig pigpng animals add new animal orange

This preview shows page 12 - 19 out of 41 pages.

Animal( "Pink Pig" , "pig.png" )); animals .add( new Animal( "Orange Tiger" , "tiger.png" )); animals .add( new Animal( "Green Turtle" , "turtle.png" )); ListView lv = (ListView) findViewById(R.id. listView ); lv.setAdapter( new AnimalArrayAdaptor( this , R.layout. custom_row , animals )); lv.setOnItemClickListener( this ); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Animal animal = animals .get(position); Toast. makeText (getApplicationContext(), "Animal clicked : " + animal.getName(), Toast. LENGTH_SHORT ).show(); } }
Image of page 12
COEN 268-TR, spring 2016 13 public class AnimalArrayAdaptor extends ArrayAdapter<Animal> { private final List<Animal> animals ; public AnimalArrayAdaptor(Context context, int resource, List<Animal> animals) { super (context, resource, animals); this . animals = animals; } Adaptor class
Image of page 13
COEN 268-TR, spring 2016 14 Adaptor class @Override public View getView( int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) getContext().getSystemService (Context. LAYOUT_INFLATER_SERVICE ); View row = covertView; if (convertView == null ) { row = inflater.inflate(R.layout. custom_row , parent, false ); } TextView textView = (TextView) row.findViewById(R.id. label ); textView.setText( animals .get(position).getName()); ImageView imageView = (ImageView) row.findViewById(R.id. icon ); try { String filename = animals .get(position).getFilename(); InputStream inputStream = getContext().getAssets().open(filename); Drawable drawable = Drawable. createFromStream (inputStream, null ); imageView.setImageDrawable(drawable); imageView.setScaleType(ImageView.ScaleType. CENTER_CROP ); } catch (IOException e) { e.printStackTrace(); } return row; } }
Image of page 14
COEN 268-TR, spring 2016 15 Image in assets Create assets directory and copy over images Support png, jpg, gif common formats Image size Use “natural” size; or Scale the image CENTER_CROP is a common tactic
Image of page 15
COEN 268-TR, spring 2016 16 Additional notes For some text oriented listing, one may use ListActivity Refer code sample L08-ListActivity L08-ListActivity-custom-row
Image of page 16
COEN 268-TR, spring 2016 17 Demo complex custom list view Objective Additional clickable “button” for each row Optimize performance for scrolling Strategy Customize row layout more and register click callback dynamically Re- use “scrape” view whenever possible
Image of page 17
COEN 268-TR, spring 2016 18 Row layout file <? xml version= "1.0" encoding= "utf-8" ?> < LinearLayout xmlns: android = "" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :background= "@android:color/darker_gray" android :gravity= "center_vertical" > < ImageView android :layout_width= "70dp" android :layout_height= "50dp" android :id= "@+id/icon" /> < TextView android :layout_width= "wrap_content" android :layout_height= "wrap_content" android :text= "New Text" android :id= "@+id/label" android :textSize= "30sp" /> < Space android :layout_width= "20px" android :layout_height= "20px" android :layout_weight= "1" /> < ImageView android :layout_width= "wrap_content" android :layout_height= "wrap_content" android :id= "@+id/menu" android :src= "@android:drawable/ic_menu_info_details" android :layout_marginRight= "10dp" /> </ LinearLayout > Space is taking all the gap so that “menu” can be at right
Image of page 18
Image of page 19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture