Search code examples
androidandroid-custom-view

Remove Padding between drawableTop and text


I have CustomLayout which has TextView on which I am setting text and drawable programmatically however the problem is when I set the drawable it is adding a huge padding with what I want is drawable to be more at the center rather than at the top.

Custom ViewGroup

public class ProfileGridView extends FrameLayout {
public ProfileGridView(@NonNull Context context) {
    super(context);
    initView(context,null);
}

public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initView(context,attrs);

}

public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context,attrs);
}

@TargetApi(21)
public ProfileGridView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initView(context,attrs);
}

public void initView(Context context, AttributeSet attrs) {
    TextView gridTextView;
    View view = inflate(context, R.layout.square_grid, null);
    gridTextView = (TextView) view.findViewById(R.id.grid_description);

    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProfileGridView);
    Drawable drawable = array.getDrawable(R.styleable.ProfileGridView_drawble);
    String description = array.getString(R.styleable.ProfileGridView_grid_description);

    gridTextView.setText(description);
    gridTextView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
    addView(view);
    array.recycle();
   }
}

CustomView LayoutFile

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:gravity="center"
android:padding="0dp"
android:textSize="20sp" />

MainActivit.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

  <com.example.views.ProfileGridView
      android:layout_width="200dp"
      android:layout_height="200dp"
      app:drawble="@drawable/ic_message"
      app:grid_description="Message"/>

</RelativeLayout>

Screenshot of the View


Solution

  • Can you use a CustomView LayoutFile like this?

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
    <ImageView
        android:id="@+id/image"
        android:layout_width="52dp"
        android:layout_height="52dp" />
    <TextView
        android:id="@+id/grid_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="4dp"
        android:textSize="20sp"/>
    </LinearLayout>
    

    And set the drawable like this

        ImageView image;
        image = (ImageView) view.findViewById(R.id.image);
        image.setImageDrawable(drawable);
    

    Instead of

    gridTextView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
    

    EDIT

    As you ask, you can make ProfileGridView wrap_content instead of 200dp. With setCompoundDrawablesWithIntrinsicBounds you attach the drawable to the bounds of the TextView (200dp). [You can check the documentation for more info](https://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable))