Skip to content

Commit

Permalink
Merge pull request #2 from SandroMachado/feature/add-append-text-support
Browse files Browse the repository at this point in the history
Add append text support
  • Loading branch information
SandroMachado committed Nov 28, 2015
2 parents 29097d3 + 8c8cd58 commit 7bce690
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Restaurant
An extension to the Snackbar view available at the Android Design Support Library.

Restaurant wraps the `Snackbar` implementation to easily allow more customization. With Restaurant you can change the text and background color of the `Snackbar` just calling a method. Restaurant can also detect the correct view to attach the Snackbar just receiving the activity.
Restaurant wraps the `Snackbar` implementation to easily allow more customization. With Restaurant you can change the text and background color of the `Snackbar` just calling a method. Restaurant can also detect the correct view to attach the Snackbar just receiving the activity.

# Gradle Dependency

Expand All @@ -24,7 +24,7 @@ dependencies {
// ... other dependencies here.
// Set the transitive = false if you already have the Design Support Library dependency.
compile('com.github.SandroMachado.restaurant:0.1.0@aar') {
compile('com.github.SandroMachado.restaurant:0.2.0@aar') {
transitive = true
}
}
Expand Down Expand Up @@ -52,6 +52,19 @@ new Restaurant(MainActivity.this, "Snackbar with custom text color", Snackbar.LE
.show();
```

Show a Snackbar with a custom text colors:

![Snackbar with custom text colors](screenshots/custom_text_colors.png)

```Java
new Restaurant(MainActivity.this, "", Snackbar.LENGTH_LONG)
.appendText("RED", Color.RED)
.appendText("GREEN", Color.GREEN)
.appendText("BLUE", Color.BLUE)
.appendText("WHITE", Color.WHITE)
.show();
```

Show a custom Snackbar:

![Snackbar with custom](screenshots/custom.png)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,18 @@ public void onClick(View v) {
.show();
}
});

Button snackbarMultipleCustomTextColor = (Button) findViewById(R.id.button_snackbar_multiple_custom_text_color);
snackbarMultipleCustomTextColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Restaurant(MainActivity.this, "", Snackbar.LENGTH_LONG)
.appendText("RED", Color.RED)
.appendText("GREEN", Color.GREEN)
.appendText("BLUE", Color.BLUE)
.appendText("WHITE", Color.WHITE)
.show();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
android:layout_height="wrap_content"
android:text="Show a Snackbar with custom text color and background"/>

<Button
android:id="@+id/button_snackbar_multiple_custom_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show a Snackbar with multiple custom text color"/>

</LinearLayout>
42 changes: 39 additions & 3 deletions app/src/main/java/com/sandro/restaurant/Restaurant.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,41 @@
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

/**
* The Restaurant is an extension to the Snackbar view available at the Android Design Support Library.
*
* Restaurant wraps the Snackbar implementation to easily allow more customization. With Restaurant
* you can change the text and background color of the Snackbar just calling a method. Restaurant
* can also detect the correct view to attach the Snackbar just receiving the activity.
*/
public class Restaurant {

private Snackbar snackbar;
private final Snackbar snackbar;

/**
* Constructor.
*
* @param activity The activity.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @param duration How long to display the message.
*/
public Restaurant(Activity activity, @StringRes int resId, @Snackbar.Duration int duration) {
this.snackbar = Snackbar.make(activity.getWindow().getDecorView().findViewById(android.R.id.content), resId, duration);
}

/**
* Constructor.
*
* @param activity The activity.
* @param text The string to use. Can be formatted text.
* @param duration How long to display the message.
*/
public Restaurant(Activity activity, @NonNull CharSequence text, @Snackbar.Duration int duration) {
this.snackbar = Snackbar.make(activity.getWindow().getDecorView().findViewById(android.R.id.content), text, duration);
}
Expand All @@ -30,6 +54,18 @@ public Snackbar getSnackBar() {
return snackbar;
}

public Restaurant appendText(String text, @ColorInt int color) {
Spannable spannable = new SpannableString(text);
View view = this.getSnackBar().getView();
TextView textView = (TextView) view.findViewById(R.id.snackbar_text);

spannable.setSpan(new ForegroundColorSpan(color), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.append(spannable);

return this;
}

/**
* Sets the text color of the action specified in
* {@link #setAction(CharSequence, View.OnClickListener)}.
Expand Down Expand Up @@ -123,9 +159,9 @@ public Restaurant setText(@StringRes int resId) {
@NonNull
public Restaurant setTextColor(ColorStateList colors) {
View view = this.getSnackBar().getView();
TextView tv = (TextView) view.findViewById(R.id.snackbar_text);
TextView textView = (TextView) view.findViewById(R.id.snackbar_text);

tv.setTextColor(colors);
textView.setTextColor(colors);

return this;
}
Expand Down
Binary file added screenshots/custom_text_colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7bce690

Please sign in to comment.