DrkStr
DrkStr

Reputation: 1932

Blank spaces appearing on top and bottom when adding image

I am adding an images using the following code:

void choosePic()
{
  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  intent.addCategory(Intent.CATEGORY_OPENABLE);
  startActivityForResult(intent, GALLERY_IMAGE_ACTIVITY_REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);



     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                                                  LinearLayout.LayoutParams.WRAP_CONTENT,
                                                  LinearLayout.LayoutParams.WRAP_CONTENT);



     if(resultCode == Activity.RESULT_OK && null != data)
     {


         case GALLERY_IMAGE_ACTIVITY_REQUEST_CODE:
              try {
                  // We need to recyle unused bitmaps
                  if (myBitmap != null) {
                      myBitmap.recycle();
                  }
                  InputStream stream = getActivity().getContentResolver().openInputStream(data.getData());
                  myBitmap = BitmapFactory.decodeStream(stream);
                  stream.close();
                  photo.setVisibility(View.VISIBLE);                      
                  photo.setImageBitmap(myBitmap);
                  photo.requestLayout();
                  photo.setLayoutParams(layoutParams);

              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
             return;

         default:
             return;

         }
     }

My problem is that I am getting blank spaces (massive ones) on the top and bottem of the image after it get added. I understand that this is because of the image view resizing, but I dont get how to get rid of the blank spaces.

The layout of the image view(and it neighbours):

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="gone"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
             />

        <EditText
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:autoLink="all"
            android:fadeScrollbars="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="top"
            android:hint="@string/noteshint"
            android:inputType="textMultiLine"
            android:linksClickable="true"
            android:scrollbars="vertical" />
    </LinearLayout>
</ScrollView>

Note: I have tried different values for the android:scaleType element

My question is how do I get rid of the blank space on top and button of the image view when I add a image?

Also, is there a better way to do this. I am also adding pics from the camera.

Upvotes: 0

Views: 903

Answers (3)

D Lad
D Lad

Reputation: 146

I managed to resolve this using

android:adjustViewBounds="true"
android:scaleType="fitCenter"

Upvotes: 0

Ahmed
Ahmed

Reputation: 125

Try this:

android:adjustViewBounds="true"

From:
https://stackoverflow.com/a/5857153/7218947

Upvotes: 1

MAC
MAC

Reputation: 15847

Set scale type to fitxy

android:scaleType="fitXY"

And if that space because of EditText remove this line

 <EditText
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"<-----------
            android:layout_weight="1"
            android:autoLink="all"
            android:fadeScrollbars="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="top"
            android:hint="@string/noteshint"
            android:inputType="textMultiLine"
            android:linksClickable="true"
            android:scrollbars="vertical" />

Upvotes: 0

Related Questions