Search code examples
javaandroidxmllayoutimageview

ImageView leaves unwanted top/bottom margin/padding


I am designing an Android XML layout that contains a few ImageViews representing images of checkmarks and red Xs. I have run into a few issues with these images containing extra margin/padding when presented to the screen.

Screenshot of padding issue

Notice that there appears to be no Right or Left margin/padding but there is plenty of top/bottom padding.

The images are PNG formats following the guidelines for creating Small/Context Icons described at http://developer.android.com/design/style/iconography.html

I would like to correct the issue by having the margin/padding match the effect of its horizontal margin/padding.

Here is the XML of the layout:

<!-- Event Reoccuring -->
            <LinearLayout
                android:id="@+id/event_reoccur_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="5dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                >

                <TextView 
                    android:id="@+id/kid_event_reoccur_label"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:layout_gravity="center_vertical"
                    android:text="@string/event_reoccur"
                />

                <ImageView 
                    android:id="@+id/kid_event_reoccur_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.1"
                    android:layout_gravity="center_vertical"
                    android:contentDescription="@string/indicator"
                    />

                <TextView 
                    android:id="@+id/kid_event_reoccur"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.4"
                    android:layout_gravity="center_vertical"
                    android:paddingLeft="4dp"
                    android:text="@string/not_applicable"
                />
            </LinearLayout>

<!-- Age Required -->
            <LinearLayout
                android:id="@+id/event_age_req_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                >

                <TextView 
                    android:id="@+id/kid_event_age_req_label"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:layout_gravity="center_vertical"
                    android:text="@string/event_age_req"
                />
                <ImageView 
                    android:id="@+id/kid_event_age_req_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.1"
                    android:gravity="center_vertical"
                    android:contentDescription="@string/indicator"
                    />
                <TextView 
                    android:id="@+id/kid_event_age_req"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.4"
                    android:layout_gravity="center_vertical"
                    android:paddingLeft="4dp"
                    android:text="@string/not_applicable"
                />
            </LinearLayout>

Here is the code that populates the ImageViews (Note - mReoccurIndcator & mAgeReqIndicator are ImageViews while mReoccurence & mAgeReq are TextViews):

@TargetApi(16)
private void setupEventReoccurence(View v) {
    // Get Reference
    mReoccurence = (TextView) v.findViewById(R.id.kid_event_reoccur);
    mReoccurence.setText(boolToString(mEvent.isReoccuring()));

    // Checkmark/X indicator
    mReoccurIndicator = (ImageView) v.findViewById(R.id.kid_event_reoccur_indicator);
    mReoccurIndicator.setImageResource(R.drawable.greencheck_small_context_icon);
    mReoccurIndicator.setScaleType(ScaleType.CENTER_INSIDE);
    mReoccurIndicator.setCropToPadding(true);
    //mReoccurIndicator.setMaxHeight((int) mReoccurence.getTextSize());
    //mReoccurIndicator.setMinimumHeight((int) mReoccurence.getTextSize());

    Log.d(TAG, "getTextSize() as float: " + mReoccurence.getTextSize());
    Log.d(TAG, "getTextSize() as int: " + (int) mReoccurence.getTextSize());



}
    @TargetApi(16)
private void setupEventAgeReq(View v) {
    // Get Reference
    mAgeReq = (TextView) v.findViewById(R.id.kid_event_age_req);
    mAgeReq.setText(boolToString(mEvent.isAgeReq()));

    // Checkmark/X indicator
    mAgeReqIndicator = (ImageView) v.findViewById(R.id.kid_event_age_req_indicator);
    mAgeReqIndicator.setImageResource(R.drawable.greencheck_small_context_icon);
    mAgeReqIndicator.setScaleType(ScaleType.CENTER_INSIDE);
    mAgeReqIndicator.setCropToPadding(true);
    //mAgeReqIndicator.setMaxHeight((int) mReoccurence.getTextSize());
    //mAgeReqIndicator.setMinimumHeight((int) mReoccurence.getTextSize());

            Log.d(TAG, "getTextSize() as float: " + mReoccurence.getTextSize());
            Log.d(TAG, "getTextSize() as int: " + (int) mReoccurence.getTextSize());
}

Solution

  • Just found a solution through another question: Unwanted padding around an ImageView

    android:adjustViewBounds="true" on the ImageView XML object solves issue.