How to add two different layouts to Listview?

1.4k views Asked by At

I want to develop an interface like WhatsApp chat.

I implemented the whole interface with ListView. I can able to add text to ListView. But I don't know how to add an image to the same ListView as an attachment.

My main aim is to add text, images, audio files, and video files to single ListView.

Please help me.

2

There are 2 answers

0
pavaniiitn On BEST ANSWER

Finally I got the answer.

Here is my code for combining the TextView and ImageView in ListView:
adding-more-than-two-different-layouts-to-listview.html

1
narancs On

So if understand the question well, you would like to load an image from the SDcard or from the gallery and load into a listview item`s imageview, right ?

Load image from file:

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

}

or if you would like to select from gallery:

public void pickImage() {
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/*");
  startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode ==         Activity.RESULT_OK) {
    if (data == null) {
        //Display an error
        return;
    }
    InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
    //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}

You can call these methods from the listview onItemClick listener, and return with the Bitmap, when you are finished the loading of the image.