I want to display icons in Snackbar. I add the icon to the snackbar, but I can't change the icon size.
How can I determine the size of the icon is added to the snackbar?
My code;
public void altbildirim(Activity context,String yazi,int sure) {
View parentLayout = context.findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(parentLayout, yazi, sure);
View snackbarLayout = snackbar.getView();
TextView textView = (TextView)snackbarLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.erroricon, 0, 0, 0);
textView.setCompoundDrawablePadding(10);
snackbar.show();
}
snackbar image I get is
Instead of providing the hard coded padding it would be better if we get it from value .
Hope this work :
textView.setCompoundDrawablePadding(getResources().getDimensionPixelOffset(R.dimen.icon_padding));
If we are using lots of Snackbars, would it be better to create a class that controls the snackbar creation or would it even slow performance? Because we would be creating one more object for the garbage collector(instance of that creator class).
I'm using such code a lot:
final Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Added to favourites", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
#Override
public void onClick(View view) {
snackbar.dismiss();
}
});
snackbar.show();
or the same but with color options too:
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(ContextCompat.getColor(MyApplication.getAppContext(), R.color.accentColor));
snackbar.show();
Is it a good idea to create such a class for making those Snackbars or would it just put more weight on the performance?
Now with a helper class, code looks much more readable:
SnackBarHelper snackBarHelper = new SnackBarHelper(coordinatorLayout, "You already signed up for this one!", Snackbar.LENGTH_LONG);
snackBarHelper.addColor(R.color.accentColor);
snackBarHelper.getMySnackbar().show();
This is without the onclick code though.
How to change the Snackbar text alignment to center ? bellow code is not working
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), intent.getStringExtra(KEY_ERROR_MESSAGE), Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red_EC1C24));
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
snack.show();
tv.setGravity(Gravity.CENTER_HORIZONTAL);
EDIT
The appearance of Snackbar was changed in Support library v23 so the correct answer now is:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
Try this:
// make snackbar
Snackbar mSnackbar = Snackbar.make(view, R.string.intro_snackbar, Snackbar.LENGTH_LONG);
// get snackbar view
View mView = mSnackbar.getView();
// get textview inside snackbar view
TextView mTextView = (TextView) mView.findViewById(android.support.design.R.id.snackbar_text);
// set text to center
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
else
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
// show the snackbar
mSnackbar.show();
The above conditional setTextAlignment() OR setGravity() solution didn't work for me.
For bulletproof centered text:
Always apply setGravity()
Apply setTextAlignment() for API >= 17
And note the support library change in AndroidX. If using AndroidX, lookup the TextView by com.google.android.material.R.id.snackbar_text instead of android.support.design.R.id.snackbar_text.
Code:
TextView sbTextView = (TextView) findViewById(com.google.android.material.R.id.snackbar_text);
sbTextView.setGravity(Gravity.CENTER_HORIZONTAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sbTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
The code below does the trick for me:
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
if(tv!=null) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
Another solution, using XML:
Your main theme has to inherit from Theme.MaterialComponents, and has to delcare its own styling for Snackbar and its TextView as follows:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="snackbarStyle">#style/MySnackbar</item>
<item name="snackbarTextViewStyle">#style/MySnackbarTextView</item>
</style>
Theme.MaterialComponents adds a margin to the snackbar, and it changes its animation. If you want the AppCompat style, do as follows:
<style name="MySnackbar" parent="#style/Widget.MaterialComponents.Snackbar">
...
<item name="android:layout_margin">0dp</item>
<item name="animationMode">slide</item>
</style>
Finally, we can edit our TextView:
<style name="MySnackbarTextView" parent="#style/Widget.MaterialComponents.Snackbar.TextView">
...
<item name="android:gravity">center_horizontal</item>
<item name="android:textAlignment">center</item>
</style>
Use "com.google.android.material" instead of "android.support.design.R" as it is no more supported after androidx migration.
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar mSnackbar = Snackbar.make(view, R.string.copyright, Snackbar.LENGTH_SHORT);
View mView = mSnackbar.getView();
TextView mTextView = (TextView) mView.findViewById(com.google.android.material.R.id.snackbar_text);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
else
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mSnackbar.show();
}
});
androidX solution for H/V center:
public class SnackBarCentered{
private Snackbar snackbar;
public void show(String message){
if (snackbar != null)
snackbar.dismiss();//Dismiss the previous snackbar if any
snackbar = Snackbar.make(this.binding.getRoot(), message, Snackbar.LENGTH_SHORT);
View view = snackbar.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.CENTER;
params.width=FrameLayout.LayoutParams.WRAP_CONTENT;
TextView tv = (TextView) view.findViewById(com.google.android.material.R.id.snackbar_text);
//Replace with android.support.design.R.id.snackbar_text if you are not using androidX
if(tv!=null) {
tv.setGravity(Gravity.CENTER);
tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
}
view.setLayoutParams(params);
snackbar.show();
}
}
Result:
Try following code it's work for set snackbar text in center:
Snackbar snackbar = Snackbar.make(main_layout, "This is snack", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
layout.setGravity(Gravity.CENTER);
TextView mTextView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
snackbar.show();
I want to change the action text color for my snackbar, but it is not working for some reason.
I use the following code to display a snackbar:
Snackbar.make(findViewById(R.id.root), "text", Snackbar.LENGTH_LONG).setActionTextColor(R.color.yellow).setAction("OK", new View.OnClickListener() {
#Override
public void onClick(View view) {
}
}).show();
The argument of setActionTextColor is the int that represents the color, not the resource ID.
Instead of this:
.setActionTextColor(R.color.yellow)
try:
.setActionTextColor(Color.YELLOW)
If you want to use resources anyway, try:
.setActionTextColor(ContextCompat.getColor(context, R.color.color_name));
Note: To use ContextCompat, I assume you have included Support library to your build.gradle file (It is optional if you have already appcompat (v7) library too).
Use
.setActionTextColor(getResources().getColor(R.color.red))
instead of just
.setActionTextColor(R.color.red)
None of above answers helped me.
I found this solution, and it works by changing manually the TextView's text color
Snackbar snack = Snackbar.make(v, "Snackbar message", Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
snack.show();
If you want to change action button text color..
snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));
If you want to change action button background color..
View sbView = snackbar.getView();
Button button=
(Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
button.setBackgroundColor(getResources().getColor(R.color.white));
Try this,
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Permission required!", 3000 /*Snackbar.LENGTH_INDEFINITE*/);
snackbar.setAction("OK", new View.OnClickListener() {
#Override
public void onClick(View v) {
// perform any action when the button on the snackbar is clicked
Toast.makeText(MainActivity.this, "Permission granted.", Toast.LENGTH_SHORT).show();
}
});
snackbar.setBackgroundTint(getResources().getColor(R.color.black)); // set the background tint color for the snackbar
snackbar.setActionTextColor(getResources().getColor(R.color.purple_500)); // set the action button text color
snackbar.show();
I'm trying to leverage new Snackbar from Android Design Support Library to display multiline snackbar, as shown in http://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-specs:
import android.support.design.widget.Snackbar;
final String snack = "First line\nSecond line\nThird line";
Snackbar.make(mView, snack, Snackbar.LENGTH_LONG).show();
It displays only First line... on my Nexus 7. How to make it display all lines?
PS: I tried Toast and it displayed all lines.
Just set the maxLines attribute of Snackbars Textview
View snackbarView = snackbar.getView();
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setMaxLines(5); // show multiple line
If you're using the more recent "com.google.android.material:material:1.0.0"dependency, then you will use this: com.google.android.material.R.id.snackbar_text to access the Snackbar's TextView.
You can use even R.id.snackbar_text as well. it's work for me.
One can override the predefined value used for that in values.xml of the app
<integer name="design_snackbar_text_max_lines">5</integer>
This value is used by Snackbar by default.
With the Material Components Library you can define it using with the snackbarTextViewStyle attribute in the app theme:
<style name="AppTheme" parent="Theme.MaterialComponents.*">
...
<item name="snackbarTextViewStyle">#style/snackbar_text</item>
</style>
<style name="snackbar_text" parent="#style/Widget.MaterialComponents.Snackbar.TextView">
...
<item name="android:maxLines">5</item>
</style>
Note: it requires the version 1.2.0 of the library.
Snackbar snackbar = Snackbar.make(view, "Text",Snackbar.LENGTH_LONG).setDuration(Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView tv= (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(3);
snackbar.show();
Here is my finding on this :
Android does support multiline snackbars but it has a max limit of 2 lines which matches the design guideline where it says that the height of multiline snack bar should be 80dp (almost 2 lines)
To verify this, i used the cheesesquare android sample project. If i use following string:
Snackbar.make(view, "Random Text \n When a second snackbar is triggered while the first is displayed", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
In this case, i can see the multiline snack bar with the text of 2nd line, i.e. "When a second snackbar is triggered" but if i change this code to following implementation:
Snackbar.make(view, "Random Text \n When \n a second snackbar is triggered while the first is displayed", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
I can only see the "Random Text\nWhen ...". This means that design library is intentionally forcing the textview to be of max 2 lines.
In kotlin you can use extensions.
// SnackbarExtensions.kt
fun Snackbar.allowInfiniteLines(): Snackbar {
return apply { (view.findViewById<View?>(R.id.snackbar_text) as? TextView?)?.isSingleLine = false }
}
Usage:
Snackbar.make(view, message, Snackbar.LENGTH_LONG)
.allowInfiniteLines()
.show()
For Material Design, the reference is com.google.android.material.R.id.snackbar_text
val snack = Snackbar.make(myView, R.string.myLongText, Snackbar.LENGTH_INDEFINITE).apply {
view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text).maxLines = 10
}
snack.show()
In Kotlin, you can just do
Snackbar.make(root_view, "Yo\nYo\nYo!", Snackbar.LENGTH_SHORT).apply {
view.snackbar_text.setSingleLine(false)
show()
}
You could also replace setSingleLine(false) with maxLines = 3.
Android Studio should prompt you to add
import kotlinx.android.synthetic.main.design_layout_snackbar_include.view.*
EDIT
I haven't been able to get this to work again, so I'll just share what I think is the cleanest way to write in Kotlin what a few others have already shared:
import com.google.android.material.R as MaterialR
Snackbar.make(root_view, "Yo\nYo\nYo!", Snackbar.LENGTH_SHORT).apply {
val textView = view.findViewById<TextView>(MaterialR.id.snackbar_text)
textView.setSingleLine(false)
show()
}
2021 Answer in Kotlin for com.google.android.material:material:1.4.0
isSingleLine = false is required as well as maxLines = 5
Snackbar.make(view, "line 1\nline 2", BaseTransientBottomBar.LENGTH_INDEFINITE)
.apply {
this.view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)?.apply {
maxLines = 5
isSingleLine = false
}
}
.show()
An alternative to the suggestions that involve hardcoding the resource ID for the textview contained by the snackbar is to iterate to find the TextView. It's safer long-term and lets you update the support library with minimal fear of the ID changing.
Example:
public static Snackbar getSnackbar(View rootView, String message, int duration) {
Snackbar snackbar = Snackbar.make(rootView, message, duration);
ViewGroup snackbarLayout = (ViewGroup) snackbar.getView();
TextView text = null;
for (int i = 0; i < snackbarLayout.getChildCount(); i++) {
View child = snackbarLayout.getChildAt(i);
// Since action is a button, and Button extends TextView,
// Need to make sure this is the message TextView, not the
// action Button view.
if(child instanceof TextView && !(child instanceof Button)) {
text = (TextView) child;
}
}
if (text != null) {
text.setMaxLines(3);
}
return snackbar;
}
Instead of using setMaxLines, i use setSingleLine to make the textview wrap to its content.
String yourText = "First line\nSecond line\nThird line";
Snackbar snackbar = Snackbar.make(mView, yourText, Snackbar.LENGTH_SHORT);
TextView textView =
(TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
textView.setSingleLine(false);
snackbar.show();
this works for me
Snackbar snackbar = Snackbar.make(mView, "Your text string", Snackbar.LENGTH_INDEFINITE);
((TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text)).setSingleLine(false);
snackbar.show();
Late, but might be helpful to someone:
public void showSnackBar(String txt, View view){
final Snackbar snackbar = Snackbar.make(view,txt,Snackbar.LENGTH_INDEFINITE)
.setAction("OK", new View.OnClickListener() {
#Override
public void onClick(View view) {
//do something
}
});
View view = snackbar.getView();
TextView textView = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
textView.setMaxLines(5);
snackbar.show();
}
May i suggest you to use com.google.android.material.snackbar.Snackbar. This is the recommanded way by google. First you have to add your snackbar.
final Snackbar snackbar = Snackbar.make(
findViewById(R.id.activity_layout),
"snackbar explanation text \n multilines \n\n here",
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.action_settings, new View.OnClickListener() {
#Override
public void onClick(View view) {
// your action here
}
});
Then to add multilines support
TextView messageView = snackbar.getView().findViewById(R.id.snackbar_text);
messageView.setMaxLines(4);
Finally show the snackbar.
snackbar.show();
A way to do it which won't crash in case things change on newer versions of the library :
Snackbar.make(...).setAction(...) {
...
}.apply {
(view.findViewById<View?>(R.id.snackbar_text) as? TextView?)?.setSingleLine(false)
}.show()
And a way to do it without having ids being used, setting all TextViews in the Snackbar to have unlimited multi-lines :
#UiThread
fun setAllTextViewsToHaveInfiniteLinesCount(view: View) {
when (view) {
is TextView -> view.setSingleLine(false)
is ViewGroup -> for (child in view.children)
setAllTextViewsToHaveInfiniteLinesCount(child)
}
}
Snackbar.make(...).setAction(...) {
...
}.apply {
setAllTextViewsToHaveInfiniteLinesCount(view)
}.show()
The same function in Java:
#UiThread
public static void setAllTextViewsToHaveInfiniteLines(#Nullable final View view) {
if (view == null)
return;
if (view instanceof TextView)
((TextView) view).setSingleLine(false);
else if (view instanceof ViewGroup)
for (Iterator<View> iterator = ViewGroupKt.getChildren((ViewGroup) view).iterator(); iterator.hasNext(); )
setAllTextViewsToHaveInfiniteLines(iterator.next());
}
Just a quick comment, if you are using com.google.android.material:material the prefix or package for R.id should be com.google.android.material
val snackbarView = snackbar.view
val textView = snackbarView.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
textView.maxLines = 3
so as i am using latest material design library from google, com.google.android.material:material:1.1.0 and i used simple following code snipet below, to resolve allow to more lines in snackbar. hope it will help to new developers as well.
TextView messageView = snackbar.getView().findViewById(R.id.snackbar_text);
messageView.setMaxLines(5);
To avoid flakiness of other answers can use updateMaxLine, this solution is less likely to break if Google decide to change the id of a text view)
val snackBar = Snackbar.make(view, message, duration)
snackBar.view.allViews.updateMaxLine(5)
snackBar.show()
just note, this option will update the max line for all the text views in the Snakbar view (which tbh I do not think it matters)
add this as extension
private fun <T> Sequence<T>.updateMaxLine(maxLine : Int) {
for (view in this) {
if (view is TextView) {
view.maxLines = maxLine
}
}
}
Snackbar height adjustment:
val sMsg = "Msg\n\n"
val sOk = getString(R.string.ok)
val sMoreLines = StringBuilder()
for (iCtr in 1..6) {
sMoreLines.append("\n")
}
Snackbar
.make(
this.requireActivity().findViewById(android.R.id.content),
sMsg,
Snackbar.LENGTH_INDEFINITE)
.setAction("$sMoreLines$sOk\n$sMoreLines") {
// ...
}
.show()