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

Ability to switch between top and bottom. #38

Open
wants to merge 2 commits 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
34 changes: 33 additions & 1 deletion app/src/main/java/com/androidadvance/tsnackbar/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ protected void onCreate(Bundle savedInstanceState) {
Button button_example_4 = (Button) findViewById(R.id.button_example_4);
Button button_example_5 = (Button) findViewById(R.id.button_example_5);
Button button_example_6 = (Button) findViewById(R.id.button_example_6);
Button button_example_7 = (Button) findViewById(R.id.button_example_7);
Button button_example_8 = (Button) findViewById(R.id.button_example_8);


relative_layout_main = (RelativeLayout) findViewById(R.id.relative_layout_main);

button_example_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TSnackbar.make(relative_layout_main, "Hello from VSnackBar 1", TSnackbar.LENGTH_LONG)
TSnackbar.make(relative_layout_main, "Hello from VSnackBar 1", 10000)
.show();
}
});
Expand Down Expand Up @@ -143,5 +145,35 @@ public void onClick(View v) {

}
});

button_example_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TSnackbar.make(relative_layout_main, "Hello from VSnackBar 1", TSnackbar.LENGTH_LONG, TSnackbar.POSITION_BOTTOM)
.show();
}
});

button_example_8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TSnackbar snackbar = TSnackbar
.make(relative_layout_main, "Had a snack at Snackbar", TSnackbar.LENGTH_LONG, TSnackbar.POSITION_BOTTOM)
.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Action Button", "onClick triggered");
}
});
snackbar.setActionTextColor(Color.LTGRAY);
snackbar.addIcon(R.mipmap.ic_core, 200);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#555555"));
TextView textView = (TextView) snackbarView.findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbar.show();

}
});
}
}
23 changes: 23 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@
android:id="@+id/button_example_6"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:gravity="bottom"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Bottom 1"
android:id="@+id/button_example_7"
android:layout_weight="1" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Bottom 2"
android:id="@+id/button_example_8"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

</RelativeLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.SwipeDismissBehavior;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
Expand Down Expand Up @@ -81,6 +81,7 @@ public void onShown(TSnackbar snackbar) {


@IntDef({LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG})
@IntRange(from = 1)
@Retention(RetentionPolicy.SOURCE)
public @interface Duration {
}
Expand All @@ -101,6 +102,14 @@ public void onShown(TSnackbar snackbar) {
private static final int MSG_SHOW = 0;
private static final int MSG_DISMISS = 1;

@IntDef({POSITION_TOP, POSITION_BOTTOM})
@Retention(RetentionPolicy.SOURCE)
public @interface Position {
}

public static final int POSITION_TOP = 0;
public static final int POSITION_BOTTOM = 1;

static {
sHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
@Override
Expand All @@ -123,29 +132,45 @@ public boolean handleMessage(Message message) {
private final SnackbarLayout mView;
private int mDuration;
private Callback mCallback;
private final int mPosition;

private TSnackbar(ViewGroup parent) {
private TSnackbar(ViewGroup parent, @Position int position) {
mParent = parent;
mContext = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(mContext);
mView = (SnackbarLayout) inflater.inflate(R.layout.tsnackbar_layout, mParent, false);
int layoutId = position == POSITION_TOP ? R.layout.tsnackbar_layout : R.layout.tsnackbar_layout_bottom;
mView = (SnackbarLayout) inflater.inflate(layoutId, mParent, false);
mPosition = position;
}


@NonNull
public static TSnackbar make(@NonNull View view, @NonNull CharSequence text,
@Duration int duration) {
TSnackbar snackbar = new TSnackbar(findSuitableParent(view));
return make(view, text, duration, POSITION_TOP);
}


@NonNull
public static TSnackbar make(@NonNull View view, @StringRes int resId, @Duration int duration) {
return make(view, view.getResources()
.getText(resId), duration, POSITION_TOP);
}

@NonNull
public static TSnackbar make(@NonNull View view, @NonNull CharSequence text,
@Duration int duration, @Position int position) {
TSnackbar snackbar = new TSnackbar(findSuitableParent(view), position);
snackbar.setText(text);
snackbar.setDuration(duration);
return snackbar;
}


@NonNull
public static TSnackbar make(@NonNull View view, @StringRes int resId, @Duration int duration) {
return make(view, view.getResources()
.getText(resId), duration);
public static TSnackbar make(@NonNull View view, @StringRes int resId, @Duration int duration,
@Position int position) {
return make(view, view.getResources().getText(resId), duration, position);
}

private static ViewGroup findSuitableParent(View view) {
Expand Down Expand Up @@ -484,8 +509,9 @@ public void onLayoutChange(View view, int left, int top, int right, int bottom)
}

private void animateViewIn() {
boolean top = mPosition == POSITION_TOP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
ViewCompat.setTranslationY(mView, -mView.getHeight());
ViewCompat.setTranslationY(mView, top ? -mView.getHeight() : mView.getHeight());
ViewCompat.animate(mView)
.translationY(0f)
.setInterpolator(com.androidadvance.topsnackbar.AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
Expand All @@ -509,7 +535,7 @@ public void onAnimationEnd(View view) {
.start();
} else {
Animation anim = AnimationUtils.loadAnimation(mView.getContext(),
R.anim.top_in);
top ? R.anim.top_in : R.anim.bottom_in);
anim.setInterpolator(com.androidadvance.topsnackbar.AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR);
anim.setDuration(ANIMATION_DURATION);
anim.setAnimationListener(new Animation.AnimationListener() {
Expand All @@ -535,9 +561,10 @@ public void onAnimationRepeat(Animation animation) {
}

private void animateViewOut(final int event) {
boolean top = mPosition == POSITION_TOP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
ViewCompat.animate(mView)
.translationY(-mView.getHeight())
.translationY(top ? -mView.getHeight() : mView.getHeight())
.setInterpolator(com.androidadvance.topsnackbar.AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR)
.setDuration(ANIMATION_DURATION)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
Expand All @@ -553,7 +580,7 @@ public void onAnimationEnd(View view) {
})
.start();
} else {
Animation anim = AnimationUtils.loadAnimation(mView.getContext(), R.anim.top_out);
Animation anim = AnimationUtils.loadAnimation(mView.getContext(), top ? R.anim.top_out : R.anim.bottom_out);
anim.setInterpolator(com.androidadvance.topsnackbar.AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR);
anim.setDuration(ANIMATION_DURATION);
anim.setAnimationListener(new Animation.AnimationListener() {
Expand Down
7 changes: 7 additions & 0 deletions topsnackbar/src/main/res/anim/bottom_in.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="100%"
android:interpolator="@android:interpolator/decelerate_cubic"
android:toYDelta="0%" />
7 changes: 7 additions & 0 deletions topsnackbar/src/main/res/anim/bottom_out.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="0%"
android:interpolator="@android:interpolator/accelerate_cubic"
android:toYDelta="100%" />
22 changes: 22 additions & 0 deletions topsnackbar/src/main/res/layout/tsnackbar_layout_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="com.androidadvance.topsnackbar.TSnackbar$SnackbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
style="@style/Widget.Design.Snackbar" />