Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drop shadow support #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/main/java/es/dmoral/toastysample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.text.style.StyleSpan;
import android.view.View;

import es.dmoral.toasty.ShadowOptions;
import es.dmoral.toasty.Toasty;

import static android.graphics.Typeface.BOLD_ITALIC;
Expand Down Expand Up @@ -60,6 +61,14 @@ public void onClick(View view) {
Toasty.warning(MainActivity.this, R.string.warning_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_shadow_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.Config.getInstance().setShadow(ShadowOptions.DROP_SHADOW).apply();
Toasty.warning(MainActivity.this, R.string.shadow_message, Toasty.LENGTH_SHORT, true).show();
Toasty.Config.reset();
}
});
findViewById(R.id.button_normal_toast_wo_icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down
19 changes: 15 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,32 @@
android:layout_alignParentEnd="true" />

<Button
android:text="@string/warning_toast"
android:id="@+id/button_warning_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_info_toast_with_formatting"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="@string/warning_toast" />

<Button
android:id="@+id/button_shadow_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_warning_toast"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:id="@+id/button_warning_toast" />
android:layout_alignParentRight="true"
android:text="@string/shadow_toast" />

<Button
android:text="@string/normal_toast_without_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_warning_toast"
android:layout_below="@+id/button_shadow_toast"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button_normal_toast_wo_icon"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<string name="normal_toast_with_icon">NORMAL TOAST (WITH ICON)</string>
<string name="normal_toast_without_icon">NORMAL TOAST (WITHOUT ICON)</string>
<string name="warning_toast">WARNING TOAST</string>
<string name="shadow_toast">DROP SHADOW TOAST</string>
<string name="info_toast">INFO TOAST</string>
<string name="info_toast_with_formatting">INFO TOAST WITH FORMATTING</string>
<string name="error_toast">ERROR TOAST</string>
Expand All @@ -13,6 +14,7 @@
<string name="success_message">Success!</string>
<string name="info_message">Here is some info for you.</string>
<string name="warning_message">Beware of the dog.</string>
<string name="shadow_message">This has a drop shadow.</string>
<string name="normal_message_without_icon">Normal toast w/o icon</string>
<string name="normal_message_with_icon">Normal toast w/ icon</string>
<string name="custom_message">sudo kill -9 everyone</string>
Expand Down
56 changes: 56 additions & 0 deletions toasty/src/main/java/es/dmoral/toasty/ShadowOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package es.dmoral.toasty;

import android.graphics.Color;

import androidx.annotation.CheckResult;

/**
* Provide a simple way to configure shadows for toast texts
*/
public class ShadowOptions {

/**
* No shadow at all
*/
public static ShadowOptions NONE = new ShadowOptions(0,0,0,0);

/**
* A drop shadow suitable far from the text to aid legibility
*/
public static ShadowOptions DROP_SHADOW = new ShadowOptions(8,5,5,Color.BLACK);

/**
* An outer 'glow'
*/
public static ShadowOptions OUTER_GLOW = new ShadowOptions(3,0,0,Color.WHITE);

private int radius, dx, dy, color;

public ShadowOptions(int radius, int dx, int dy, int color) {
this.radius = radius;
this.dx = dx;
this.dy = dy;
this.color = color;
}

@CheckResult
public int getColor() {
return color;
}

@CheckResult
public int getDx() {
return dx;
}

@CheckResult
public int getDy() {
return dy;
}

@CheckResult
public int getRadius() {
return radius;
}

}
20 changes: 19 additions & 1 deletion toasty/src/main/java/es/dmoral/toasty/Toasty.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class Toasty {
private static int yOffset = -1;
private static boolean supportDarkTheme = true;
private static boolean isRTL = false;
private static ShadowOptions shadowOptions = ShadowOptions.NONE;

private static Toast lastToast = null;

Expand Down Expand Up @@ -298,6 +299,7 @@ public static Toast custom(@NonNull Context context, @StringRes int message, Dra
ToastyUtils.getColor(context, textColorRes), duration, withIcon, shouldTint);
}


@SuppressLint("ShowToast")
@CheckResult
public static Toast custom(@NonNull Context context, @NonNull CharSequence message, Drawable icon,
Expand Down Expand Up @@ -331,6 +333,11 @@ public static Toast custom(@NonNull Context context, @NonNull CharSequence messa
toastTextView.setTextColor(textColor);
toastTextView.setTypeface(currentTypeface);
toastTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
toastTextView.setShadowLayer(
shadowOptions.getRadius(),
shadowOptions.getDx(),
shadowOptions.getDy(),
shadowOptions.getColor());

currentToast.setView(toastLayout);

Expand Down Expand Up @@ -390,6 +397,8 @@ public static class Config {
private int yOffset = Toasty.yOffset;
private boolean supportDarkTheme = true;
private boolean isRTL = false;
private ShadowOptions shadowOptions = Toasty.shadowOptions;


private Config() {
// avoiding instantiation
Expand All @@ -410,6 +419,7 @@ public static void reset() {
Toasty.yOffset = -1;
Toasty.supportDarkTheme = true;
Toasty.isRTL = false;
Toasty.shadowOptions = ShadowOptions.NONE;
}

@CheckResult
Expand Down Expand Up @@ -455,7 +465,14 @@ public Config supportDarkTheme(boolean supportDarkTheme) {
this.supportDarkTheme = supportDarkTheme;
return this;
}


@CheckResult
public Config setShadow(ShadowOptions shadowOptions) {
this.shadowOptions = shadowOptions;
return this;
}

@CheckResult
public Config setRTL(boolean isRTL) {
this.isRTL = isRTL;
return this;
Expand All @@ -471,6 +488,7 @@ public void apply() {
Toasty.yOffset = yOffset;
Toasty.supportDarkTheme = supportDarkTheme;
Toasty.isRTL = isRTL;
Toasty.shadowOptions = shadowOptions;
}
}
}