Search code examples
androidcardview

android set image in rounded cardview


I'm using DialogFragment to show image in CardView but I'm not getting full image as I expect. Here is screen short how I'm getting image. how can I fill this space.

Here is my code

public class dlgProfile extends DialogFragment {


MainActivity _main;
private MainActivity context;
private TextView txtBenutzerName;
private TextView txtName;
private TextView txtAdresse;
private TextView txtTaetigkeit;
private BlurredImageView ivProfile;
private final String TAG = "fragprofile";
private Question q;
private Answer a;
private TextView txtID;
private Long BenutzerID;
private String BenutzerName;
private TextView btnPhone;
private TextView btnEMail;
CircleImageView cvImg;
private ImageView btnWhatsApp;
private MainActivity.OnlineState onlineState = MainActivity.OnlineState.offline;
private TextView txtOnlineState;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (_main == null) _main = (MainActivity) getActivity();
    context = _main;
    View v = inflater.inflate(R.layout.fragprofile, container, false);
    txtID = v.findViewById(R.id.txtID);
    txtBenutzerName = v.findViewById(R.id.txtBenutzername);
    txtName = v.findViewById(R.id.txtName);
    txtAdresse = v.findViewById(R.id.txtAdresse);
    txtTaetigkeit = v.findViewById(R.id.txtTaetigkeitsschwerpunkte);
    txtOnlineState = v.findViewById(R.id.txtOnlineState);
    ivProfile = v.findViewById(R.id.imgProfile);
    cvImg = v.findViewById(R.id.imgProfile2);
    btnPhone = (TextView) v.findViewById(R.id.btnPhone);
    btnEMail = (TextView) v.findViewById(R.id.btnEMail);
    btnWhatsApp = (ImageView) v.findViewById(R.id.btnWhatsApp);

    if (this.BenutzerName != null) {
        try {
            setValues(this.BenutzerID, this.BenutzerName);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            lib.ShowException(TAG, context, throwable, false);
        }
    }


    getDialog().getWindow().setBackgroundDrawableResource(R.drawable.bg_round_corner);
    return v;
}

void setValues(final Question q, final Answer a) throws Throwable {
    JSONObject N;
    if (a == null) {
        this.onlineState = q.onlineState;
        setValues(q.BenutzerID, q.Benutzername);
       
    } else {
        this.onlineState = a.onlineState;
        setValues(a.BenutzerID, a.BenutzerName);
       
    }

}

void setValues(Long BenutzerID, String Benutzername) throws Exception {
    if (txtID == null) {
        this.BenutzerID = BenutzerID;
        this.BenutzerName = Benutzername;
        return;
    }
    JSONObject N = _main.clsHTTPS.getUser(_main.getBenutzerID(), BenutzerID);
    txtID.setText("" + BenutzerID);
    txtBenutzerName.setText(Benutzername);
    txtName.setText((N.isNull(VORNAME) ? "?" : N.getString(VORNAME)) + " " + (N.isNull(NAME) ? "?" : N.getString(NAME)));
    txtAdresse.setVisibility(BenutzerID.longValue() == _main.getBenutzerID()
            || N.getBoolean("gewerblich") ? View.VISIBLE : View.GONE);
    txtAdresse.setText((N.isNull("adresse") ? "" : N.getString("adresse")));
    txtTaetigkeit.setVisibility(BenutzerID.longValue() == _main.getBenutzerID()
            || N.getBoolean("gewerblich") ? View.VISIBLE : View.GONE);
    txtTaetigkeit.setText((N.isNull(TAETIGKEITSSCHWERPUNKTE) ? "" : N.getString(TAETIGKEITSSCHWERPUNKTE)));
    txtOnlineState.setText(this.onlineState.getStateString());
    Bitmap profile = null;
    try {
        String fname;
        fname = ".JPG";
        Bitmap in = _main.clsHTTPS.downloadProfileImage(this.getContext(), BenutzerID,
                _main.user.getLong(Constants.id), Benutzername);
        if (in != null) {
            profile = in;
        }
    } catch (Throwable e) {
        e.printStackTrace();
        Log.i(TAG, null, e);
    }

    if (profile != null) {
        ivProfile.setImageBitmap(profile);
    }
    if (profile != null) {
        cvImg.setImageBitmap(profile);
    }
    final ContactVO contactVO = new ContactVO(N);
    if (BenutzerID.longValue() != _main.getBenutzerID() && !contactVO.isBestaetigt()
            && contactVO.isLimindoUser() && (_main.clsHTTPS.AdminID == null || _main.clsHTTPS.AdminID == 0)) {
        btnEMail.setVisibility(View.GONE);
        btnPhone.setVisibility(View.GONE);
        btnWhatsApp.setVisibility(View.GONE);
    } else {
        if (contactVO.getContactEmail().length() > 3) {
            btnEMail.setTag(contactVO.getContactEmail());
            btnEMail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                    String[] aEmailList = {contactVO.getContactEmail()};
                    //String aEmailCCList[] = { "[email protected]","[email protected]"};
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
                    //emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);
                    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
                    emailIntent.setType("text/plain");
                    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
                    MainActivity.dontStop++;
                    context.startActivityForResult(emailIntent, MainActivity.RGLOBAL);
                }
            });
        } else {
            btnEMail.setVisibility(View.GONE);
        }
        if (contactVO.getContactNumber().length() > 3) {
            btnPhone.setTag(contactVO.getContactNumber());
            btnPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phone = contactVO.getContactNumber();
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
                    MainActivity.dontStop++;
                    startActivityForResult(intent, MainActivity.RGLOBAL);
                }
            });
            btnWhatsApp.setTag(contactVO.getContactNumber());
            btnWhatsApp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (contactVO.getContactNumber().length() > 0) {
                        String number = contactVO.getContactNumber();
                        number = number.replaceFirst("\\+", "");
                        String message = "";
                        try {
                            
                            String url = "https://api.whatsapp.com/send?phone=" + number;
                            Intent i = new Intent(Intent.ACTION_VIEW);
                            i.setData(Uri.parse(url));
                            MainActivity.dontStop++;
                            startActivityForResult(i, MainActivity.RGLOBAL);
                        } catch (Exception e) {

                        }

                    }
                }
            });
        } else {
            btnPhone.setVisibility(View.GONE);
            btnWhatsApp.setVisibility(View.GONE);
        }

    }


}

}

and here is drawable bg_round_corner for cardview which I'm using for showing data

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <corners android:radius="30dp" />
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

here is my xml of imageview

<ru.egslava.blurredview.BlurredImageView
    android:id="@+id/imgProfile"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:adjustViewBounds="true"
   android:scaleType="centerCrop"
    android:src="@drawable/vn_logo_c02"
    app:downSampling="2"
    app:keepOriginal="true"
    app:layout_constraintTop_toTopOf="parent"
    app:radius="0.9" />

Solution

  • You have two issues preventing your image from going to the extreme edges of the card.

    1. Padding around all edges

    Since your background shape has padding on all four sides, it will inset all of the content by that padding. You will have a gap between your image and the edges of the card. Removing the padding from the drawable XML (or setting padding attributes to 0dp on the view that has this as its background drawable) will remove this gap. Then, you will need to add margins to your TextViews and other content that you still want to have a gap.

    2. Image is gravitating to the left side

    • Set android:scaleType="centerCrop" and android:adjustViewBounds="false". This causes the image to scale up uniformly in order to leave no gaps between the image and the bounds of the ImageView, and without distorting the aspect ratio. It will center the image and crop the pixels that "hang off" the edges.
    • Set android:layout_width="match_parent" on the ImageView. This will ask it to take the full width of the card.
    • Set android:layout_height on the ImageView. You have two options here, depending on what your goal is:
      • wrap_content will cause it to set its height to the height of the bitmap or drawable. This probably won't give you what you want on its own, so use android:adjustViewBounds="true" to cause it to scale the height to maintain the image's aspect ratio in relation to the width.
      • 150dp or some other fixed height. This is probably what you want, assuming the images that will go here have the same aspect ratio.
    • You probably don't ever want to combine scaleType="centerCrop" and "android:adjustViewBounds="true" because these two modes conflict with each other conceptually. centerCrop means "I accept that my bounds are determined by someone else and will scale my content to fit it, and crop the rest," while adjustViewBounds means "I will change my bounds in order to match the size of my content."