I am trying to launch a snackbar
with a close icon to dismiss it at the end. This is my custom snackbar
built with some changes over the default snackbar
. It's working fine in landscape mode when there is enough space on the device to show the text and icons.
But in portrait mode, through layout inspector I can see that the view is there on the screen. But it's not taking the minimum width that it should take. Although in the XML
file, I have defined the minimum height and width it should take.
What could be the possible reason for this and how can I fix it?
My layout is LinearLayout
And my xml file for the layout is:-
<?xml version="1.0" encoding="utf-8"?>
<view
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
class="com.example.testtransitentviews.CustomSnackbarContentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
<ImageView
android:id="@+id/snackbar_image"
android:src="@drawable/pw_diamond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:minHeight="24dp"
android:minWidth="24dp"
app:tint="@color/white" />
<TextView
android:id="@+id/snackbar_text"
style="?attr/snackbarTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/color_snackbar_text" />
<ImageView
android:id="@+id/snackbar_action_image"
android:src="@drawable/ic_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:contentDescription="Close"
android:layout_gravity="center_vertical|end"/>
</view>
This is my class CustomSnackbarContentLayout
which is extending LinearLayout
public class CustomSnackbarContentLayout extends LinearLayout implements ContentViewCallback {
private ImageView iconImageView;
private TextView messageView;
private ImageView closeActionView;
public CustomSnackbarContentLayout(@NonNull Context context) {
this(context, null);
}
public CustomSnackbarContentLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
iconImageView = findViewById(R.id.snackbar_image);
messageView = findViewById(R.id.snackbar_text);
closeActionView = findViewById(R.id.snackbar_action_image);
}
public ImageView getIconImageView() {
return iconImageView;
}
public TextView getMessageView() {
return messageView;
}
public ImageView getCloseActionView() {
return closeActionView;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public void animateContentIn(int delay, int duration) {
messageView.setAlpha(0f);
messageView.animate().alpha(1f).setDuration(duration).setStartDelay(delay).start();
if (closeActionView.getVisibility() == VISIBLE) {
closeActionView.setAlpha(0f);
closeActionView.animate().alpha(1f).setDuration(duration).setStartDelay(delay).start();
}
}
@Override
public void animateContentOut(int delay, int duration) {
messageView.setAlpha(1f);
messageView.animate().alpha(0f).setDuration(duration).setStartDelay(delay).start();
if (closeActionView.getVisibility() == VISIBLE) {
closeActionView.setAlpha(1f);
closeActionView.animate().alpha(0f).setDuration(duration).setStartDelay(delay).start();
}
}
}
This is my XML
file for the drawable "close" button that I am using.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"><path
android:pathData="M19,6.4L17.6,5L12,10.6L6.4,5L5,6.4l5.6,5.6L5,17.6L6.4,19l5.6,-5.6l5.6,5.6l1.4,-1.4L13.4,12L19,6.4z"
android:fillColor="#FFFFFF"/>
</vector>
As per this also, even if I give wrap content as attribute to my ImageView
, it should take at least 24dp height and width.
If you are able to figure out why my close button on snackbar
is not taking the required min width, then please answer. It will be a great help!
Thanks in advance.
You need to hardcode the horizontal weight of the TextView
to allow the close button to show:
<TextView
android:id="@+id/snackbar_text"
style="?attr/snackbarTextViewStyle"
android:layout_width="0dp"
android:layout_weight="1"
..../>