We added a android.support.design.widget.CoordinatorLayout to the XML file for the Activity that is using a Snackbar. All the Gradle information is correct and the two other corresponding imports are imported minSdk is 19 Support Design is v25.3.1 My question is why is this import happening and have I omitted some setting somewhere?
The original XML code file has a Relative Layout plus other items and the XML for the CoordinatorLayout widget
code for snackbar
public void showSnackbar(){
//coordinatorLayout = findViewById(snackbarView);
//Snackbar snackbar = null;
final Snackbar snackbar = Snackbar
.make(findViewById(snackbarView), getText(R.string.snackbar_text),1);
snackbar.setActionTextColor(Color.RED);
snackbar.setAction("EXIT", new View.OnClickListener(){
#Override
public void onClick(View view) {
//Intent intent = new Intent(DetailsActivity.this, MainActivity.class);
//startActivity(intent);
snackbar.dismiss();
}
});
TextView snackbarActionTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_action );
snackbarActionTextView.setTextSize( 30 );
snackbarActionTextView.setTypeface(snackbarActionTextView.getTypeface(), Typeface.BOLD);
TextView snackbarTextView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
snackbarTextView.setTextSize( 30 );
snackbarTextView.setMaxLines( 3 );
snackbarTextView.setTextColor(Color.YELLOW);
snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
snackbar.show();
}
here is my import
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
James_Duh here is some code that will create a plain Snackbar no Action button and the other method creates a Snackbar with the Action button enjoy Not sure about the import?
public void onPLAIN(View view){
noActLayout = (CoordinatorLayout)findViewById(R.id.USE_ME_TWICE);
sbNoAct = Snackbar.make(noActLayout,R.string.real_csb_noaction_text,1);// any interger will make it happy
sbNoAct.setDuration(4000);// 4 sec // OR Snackbar.LENGTH_LONG matters NOT you are setting duration here
View sbView = sbNoAct.getView();
sbView.setBackgroundColor(ContextCompat.getColor(this, R.color.color_lightBlue));
TextView textViewNoAct = sbView.findViewById(android.support.design.R.id.snackbar_text);
//set text color
textViewNoAct.setTextColor(ContextCompat.getColor(this,R.color.color_White));
textViewNoAct.setMaxLines(10);
textViewNoAct.setTextSize(18);
//increase max lines of text in snackbar. default is 2.
sbNoAct.show();
}
public void onWithAct(View view){
myLayout = (CoordinatorLayout) findViewById(R.id.USE_ME_TWICE);
sb = Snackbar.make(myLayout, R.string.real_csb_text, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.real_csb_action, myOnClickListener)
.setActionTextColor(ContextCompat.getColor(context, R.color.color_Red));
View sbView = sb.getView();
sbView.setBackgroundColor(ContextCompat.getColor(this, R.color.color_White));
TextView textView = sbView.findViewById(android.support.design.R.id.snackbar_text);
//set text color
textView.setTextColor(ContextCompat.getColor(this,R.color.color_deepBlue));
textView.setTextSize(18);
//increase max lines of text in snackbar. default is 2.
textView.setMaxLines(10);
// NOTE new View
TextView textAction = sbView.findViewById(android.support.design.R.id.snackbar_action);
//set Action text color
textAction.setTextColor(ContextCompat.getColor(this,R.color.color_Red));
textAction.setTextSize(18);
sb.show();
}
View.OnClickListener myOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//sb.dismiss();
System.out.println("=== I WAS DISMISSED OR SENT TO MainActivity===");
// OR use and Intent to go somewhere have a nice trip
Intent intent = new Intent(PageThreeActivity.this, MainActivity.class);
startActivity(intent);
}
};
}
Related
So I want to change the vertical padding on Android's SnackBar in a clean maintainable way.
I tried to use:
setPadding and setMargin on the FrameLayout, LinearLayout and TextView used to create the SnackBar
includeFontPadding, setLineSpacing and setSingleLineon the TextView
But nothing seemed to work.
However, I found this answer:
In dimens.xml.
Use this:
<dimen name="design_snackbar_padding_horizontal">0dp</dimen>
But remember that this will get applied to all the snackbars in your application.
Which does work, but the ugly way, it's applied to all the SnackBars in the app and it's not safe since I'm overriding a private property that maybe renamed or removed in the future.
I'm trying to create something similar and Facebook Messenger's (previously) and Youtube's (Funny enough, Google) internet connection indicator:
Snackbar.make(container, message, duration).apply {
val snackBarView = view.apply {
setBackgroundColor(ContextCompat.getColor(this#MainActivity, connectivity.color))
}
(snackBarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView).apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
textAlignment = View.TEXT_ALIGNMENT_CENTER
else
gravity = Gravity.CENTER
}
}.show()
So this's what i'm expecting and am getting using the answer provided above:
And this's what i'm actually getting:
Thanks in advance!
Make a custom snackbar:
Snackbar snackbar = Snackbar.make(coordinatorLayout, R.string.custom_color, Snackbar.LENGTH_LONG).setAction(R.string.Ok, new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(coordinatorLayout, R.string.click_action, Snackbar.LENGTH_SHORT).show();
}
}).setActionTextColor(Color.RED);
View snackview = snackbar.getView();
TextView txtView = snackview.findViewById(R.id.snackbar_text);
txtView.setPadding(0,0,0,0);
snackbar.show();
The provided answer didn't work out for me. So after a lot of trial, I finally solved my problem (which is exactly the same as described above).
To reduce the vertical padding in snackbar, try this:
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.topMargin = -30;
llp.bottomMargin = -30;
snackText.setLayoutParams(llp);
Below is the full code I wrote to make functionality like the youtube app for checking and showing internet errors.
public void showNoInternet(){
noInternetSnack = Snackbar.make(noInternetSnackParent, "No internet connection", Snackbar.LENGTH_INDEFINITE)
.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE)
.setBackgroundTint(PaletteUtils.getSolidColor(PaletteUtils.MATERIAL_RED));
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) noInternetSnack.getView();
TextView snackText = snackbarLayout.findViewById(com.google.android.material.R.id.snackbar_text);
snackText.setTextSize(getResources().getDimension(R.dimen.newsMoreTextSize)/3);
snackText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.topMargin = -30;
llp.bottomMargin = -30;
snackText.setLayoutParams(llp);
noInternetSnack.show();
}
public void hideNoInternet(){
if (noInternetSnack != null && noInternetSnack.isShown()){
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) noInternetSnack.getView();
noInternetSnack.setBackgroundTint(PaletteUtils.getSolidColor(PaletteUtils.MATERIAL_GREEN));
TextView snackText = snackbarLayout.findViewById(com.google.android.material.R.id.snackbar_text);
snackText.setText("You are back online");
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
noInternetSnack.dismiss();
}
},
500
);
}
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
NetworkInfo currentNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (currentNetworkInfo.isConnected()) {
hideNoInternet();
} else {
showNoInternet();
}
}
};
Good afternoon,
I am working with Snackbars and I try to put an aciton (like UNDO), but the text is not aligned to the right of the screen.
The result I need :
The result I have :
As you can see, the "UNDO" action is not aligned right, as I need.
Here is my Snackbar code :
Snackbar snackbar = Snackbar.make(mMainContent, "Message deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
#Override
//Todo: Click on "UNDO"
public void onClick(View view) {
}
});
snackbar.setActionTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
snackbar.show();
Can someone help me ?
Thank you !
Create your own custom snackbar layout Custom snackbar
The core part is below
<!-- language: lang-java -->
// Create the Snackbar
Snackbar snackbar = Snackbar.make(containerLayout, "", Snackbar.LENGTH_LONG);
// Get the Snackbar's layout view
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Hide the text
TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE);
// Inflate our custom view
View snackView = mInflater.inflate(R.layout.my_snackbar, null);
// Configure the view
ImageView imageView = (ImageView) snackView.findViewById(R.id.image);
imageView.setImageBitmap(image);
TextView textViewTop = (TextView) snackView.findViewById(R.id.text);
textViewTop.setText(text);
textViewTop.setTextColor(Color.WHITE);
// Add the view to the Snackbar's layout
layout.addView(snackView, 0);
// Show the Snackbar
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();
Trying out the new Design Support Library, I added a snackbar; but unlike its main background, the text area is not colored with the default value of #323232. Instead, it looks like this. It seems to take its color from the android:background value defined in the custom theme in my styles.xml, which goes like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:background">#4f4f5e</item>
...
</style>
If I try to forcefully color it with
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.YELLOW);
it only impacts the main background, like this, and the text background still gets colored by the custom theme. Is there a way to both keep my custom theme, and have a standard snackbar? Thanks!
To change the Snackbar's background colour you can do the following from code:
Snackbar snack = Snackbar.make(...);
ViewGroup group = (ViewGroup) snack.getView();
group.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
snack.show();
Instead of red you can use the Snackbar's default colour: #323232
.setBackgroundColor allows you to change background color of snackbar
msnackBar.setBackgroundColor(Color.parseColor("#009688"));
or
msnackBar.setBackgroundColor(getResources().getColor(R.color.BLUE)););
Here is complete tutorial to use snackbar using design support library.
The snackbar contains a TextView, so you need to change the background color for both, the snackbar the way you already did, and then the TextView like this:
View snackbarView = snackbar.getView();
TextView textView = (TextView)snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setBackgroundColor(Color.YELLOW);
This effect happens when in style attribute android:background is set.
Removing that will of course affect all layouts in your application but snackbar will be fixed.
Here is a complete sample:
Snackbar snack = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null);
ViewGroup group = (ViewGroup) snack.getView();
group.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.blue));
snack.show();
replace MainActivity.this with your currently activity or getAppContext()
You can simply create your own Snackbar class and simulate Snackbar's make method. Doing this, you just have to use this class instead of android's snackbar widget.
Snackbar.class
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class Snackbar {
/** Snackbar's lengths **/
public static final int LENGTH_SHORT = android.support.design.widget.Snackbar.LENGTH_SHORT;
public static final int LENGTH_LONG = android.support.design.widget.Snackbar.LENGTH_LONG;
public static final int LENGTH_INDEFINITE = android.support.design.widget.Snackbar.LENGTH_INDEFINITE;
#NonNull
public static android.support.design.widget.Snackbar make(#NonNull View view, #NonNull CharSequence text,
#Duration int duration) {
android.support.design.widget.Snackbar snackbar = android.support.design.widget.Snackbar.make(view, text, duration);
// TODO: This is where you have to customize your snackbar
snackbar.getView().setBackgroundColor(Color.RED);
return snackbar;
}
#NonNull
public static android.support.design.widget.Snackbar make(#NonNull View view, #StringRes int resId, #Duration int duration) {
return make(view, view.getResources().getText(resId), duration);
}
// Optional
#IntDef({LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG})
#IntRange(from = 1)
#Retention(RetentionPolicy.SOURCE)
public #interface Duration {}
}
Use:
// WARNING: Make sure you're using your snackbar's package
import com.mypackage.custom_views.Snackbar;
public class MyActivity extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Snackbar.make(view, R.string.my_msg, Snackbar.LENGTH_LONG).show();
}
}
Hope this helps!
You can use this library: https://github.com/SandroMachado/restaurant
new Restaurant(MainActivity.this, "Snackbar with custom background and text color", Snackbar.LENGTH_LONG)
.setBackgroundColor(Color.GRAY)
.show();
Disclaimer: I made the library.
this is how i'm using custom snackbar
Snackbar snackbar_network = Snackbar.make(rLayout, "Your Message", Snackbar.LENGTH_SHORT)
.setAction("EXIT", new View.OnClickListener() {
#Override
public void onClick(final View v) {
finish();
}
});
Action Text Color
snackbar_network.setActionTextColor(Color.RED);
Action Message Text Color
final View sbView = snackbar_network.getView();
final TextView tv = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.YELLOW);
Set Snackbar Background
sbView.setBackgroundColor(ContextCompat.getColor(MapsActivity.this, R.color.black));
snackbar_network.show();
Works this way for me:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
Snackbar snackbar = Snackbar.make(lineatLayout, "TEXT", Snackbar.LENGTH_LONG);
ViewGroup group = (ViewGroup) snackbar.getView();
group.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.yourColor));
TextView textView = (TextView) group.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(ContextCompat.getColor(this, R.color.yor collor));
snackbar.show();
I also faced similar issue & unfortunately no solution works for me Hence I write my own solution where I set background color for parent view too.
TextView snackbarTextView = snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
snackbarTextView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
ViewParent parentView = snackbarTextView.getParent();
if (parentView instanceof View) {
((View) parentView).setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
}
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
snackbar.show();
I have a button that begins life "Unticked" with text going to a Label that says "NO". When you push the button it changes the image to "Ticked" and displays text in a Label as "YES". This all works perfectly. What I can't do or find is how to change it back to "Unticked" and "NO" if I then push it again?
Here is the code for the button:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
button9.setBackgroundResource(R.drawable.androidnearmisson);
TextView text = (TextView) findViewById(R.id.textView2);
text.setText("YES");
}
};
Any help will be greatly appreciated.
Many Thanks
You can get TextView's current text and make a comparison. If its YES, change to NO, else vice verse:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
TextView text = (TextView) findViewById(R.id.textView2);
if(text.getText().toString().equals("NO")){
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else {
button9.setBackgroundResource(R.drawable.otherimage); //Replace otherimage with proper drawable id
text.setText("NO");
}
}
};
Hope this helps.
First move this code to your onCreate :
TextView text = (TextView) findViewById(R.id.textView2);
Inside your onClick :
if(text.getText().toString().equals("NO"))
{
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else
{
//change what you want
}