adding an image to listview


Last Updated:

  1. kiros88

    kiros88 Member This Topic's Starter

    Joined:
    Oct 25, 2010
    Messages:
    5
    Likes Received:
    0
    Hi im wondering if anyone knows if this is possible im trying to create an app that iis a list of stuff and basically i want the user to be able to select one of the listview items and across from the items an image would appear. is this possible if so how do i do it?
     

    Advertisement
  2. JamesBurnstone

    JamesBurnstone Member

    Joined:
    Nov 22, 2010
    Messages:
    9
    Likes Received:
    0
    Your class with have to extend ListActivity, then create a ListView object which allows you to create a list in your parent view.

    ListView lv = getListView();

    To add images to the list use an ListAdapter and set it to the ListView, this handles adding items to the list. You can then add images from code, such as file paths in an array, or better practice is to be all the images in the res/drawable folder and then access each image via the generated R folder using the images ids.

    There is a tutorial on the android developer website List View | Android Developers

    Once you have the list you can implement an onOnItemClickListener() which is handles an individually item in the list being pressed. You can run a new Activity and display the full image on another screen or create a toast notification to pop up, though I'm not you can display an image in a Toast.

    Hope this is helpful though checking out the tutorial above is your best bet.
     
  3. echo3hotel

    echo3hotel New Member

    Joined:
    Nov 9, 2010
    Messages:
    3
    Likes Received:
    0
    the simplest way is to have your javacode as:

    Code (Text):
    1. public class JAVAFILENAME extends Activity {
    2.    
    3.     @Override
    4.     public void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.        
    7.         // Inflate our UI from its XML layout description.
    8.         setContentView(R.layout.YOURXMLFILE);
    9.        
    10.     }
    11. }
    12.  
    and your xml as

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2.  
    3.  
    4. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    5. android:layout_width="fill_parent"
    6. android:layout_height="wrap_content"
    7. android:scrollbars="none">
    8.  
    9.  
    10. <ImageView
    11.   xmlns:android="http://schemas.android.com/apk/res/android"
    12.   android:id="@+id/imageview"
    13.       android:background="@drawable/PICTURE"
    14.   android:layout_width="wrap_content"
    15.   android:layout_height="wrap_content">
    16. </ImageView>
    17.  
    18. </ScrollView>

    This is the simplest way if there is nothing special you want with that image. Don't forget to add the javafile to your manifest.
     

Share This Page

Loading...