I am developing an Android app. In my app, I am using snackbar with custom view. My custom view has only child view. That is an image view. Showing custom view with snack bar is OK. But the problem is with the sizing the ImageView in the snack bar. I want to set it match parent for with. But width is not match to the width of snackbar.
This is my custom view layout for snack bar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="centerCrop"
android:id="@+id/snackbar_ad_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
This is how I show snack bar in activity
private void showSnackbarAd(View view,Ad ad)
{
final Snackbar snackbar = Snackbar.make(view,"",Snackbar.LENGTH_LONG);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)snackbar.getView();
TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE);
View snackView = getLayoutInflater().inflate(R.layout.snack_bar_ad,null);
ImageView imageView = (ImageView)snackView.findViewById(R.id.snackbar_ad_image);
Picasso.with(getBaseContext()).load(ad.getImageUrl()).into(imageView);
layout.addView(snackView, 0);
snackbar.show();
}
What I want is I want ImageView same width width snack bar. But my code is not working.
This is what I got.
How can I make that ImageView as the same width with Snackbar and fit in snackbar?
Try following code,
private void showSnackbarAd(View view,Ad ad) {
final Snackbar snackbar = Snackbar.make(view,"",Snackbar.LENGTH_LONG);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)snackbar.getView();
layout.setPadding(0, 0, 0, 0);//set padding to 0
TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE);
View snackView = getLayoutInflater().inflate(R.layout.snack_bar_ad, layout);// use the parent ViewGroup instead of null
ImageView imageView = (ImageView)snackView.findViewById(R.id.snackbar_ad_image);
Picasso.with(getBaseContext()).load(ad.getImageUrl()).into(imageView);
//layout.addView(snackView, 0);
snackbar.show();
}
You can try this library. This is a wrapper for android default snackbar. https://github.com/ChathuraHettiarachchi/CSnackBar
Snackbar.with(this,null)
.type(Type.SUCCESS)
.message("Profile updated successfully!")
.duration(Duration.SHORT)
.show();
or you can even use your own view,
View view = getLayoutInflater().inflate(R.layout.custom_view, null);
Snackbar.with(this,null)
.type(Type.UPDATE)
.contentView(view, 76)
.duration(Duration.SHORT)
.show();
You will have to specify width in xml. So image will fit into that width. match_parent attribute will not set width as no specific size is specified.
Use can also try the below code,
Picasso.with(image.getContext()).load(ad.getImageUrl()).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
image.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
Whenever use a custom view, always avoid using LinearLayout as root element. It screws things up sooooooo oooooooften.
For your problem, try to replace LinearLayout in the xml with RelativeLayout.
来源:https://stackoverflow.com/questions/38030133/match-parent-for-width-of-imageview-in-snackbar-with-custom-view-in-android-is-n