-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a04a51c
commit 88f9a6a
Showing
28 changed files
with
775 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
SnackBar/src/main/java/com/example/snackbar/SnackBar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.example.snackbar | ||
|
||
import android.annotation.SuppressLint | ||
import android.graphics.Color | ||
import android.os.Build | ||
import android.view.Gravity | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.WindowInsets | ||
import android.widget.FrameLayout | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.updateLayoutParams | ||
import androidx.core.view.updateMargins | ||
import com.example.snackbar.databinding.LayoutDangerBinding | ||
import com.example.snackbar.databinding.LayoutInfoBinding | ||
import com.example.snackbar.databinding.LayoutNetworkErrorBinding | ||
import com.example.snackbar.databinding.LayoutSuccessBinding | ||
import com.example.snackbar.databinding.LayoutWarningBinding | ||
import com.google.android.material.snackbar.Snackbar | ||
|
||
class SnackBar { | ||
companion object { | ||
//Constant value | ||
private const val SUCCESS = 200 | ||
private const val INFO = 300 | ||
private const val WARNING = 400 | ||
private const val ERROR = 500 | ||
private const val NETWORKERROR = 600 | ||
const val LENGTH_LONG = Snackbar.LENGTH_LONG | ||
const val LENGTH_SHORT = Snackbar.LENGTH_SHORT | ||
const val LENGTH_INDEFINITE = Snackbar.LENGTH_INDEFINITE | ||
|
||
fun success(view: View, msg: String, length: Int): Snackbar { | ||
return createSanckbar(view, msg, length, SUCCESS) | ||
} | ||
|
||
fun info(view: View, msg: String, length: Int): Snackbar { | ||
return createSanckbar(view, msg, length, INFO) | ||
} | ||
|
||
fun warning(view: View, msg: String, length: Int): Snackbar { | ||
return createSanckbar(view, msg, length, WARNING) | ||
} | ||
|
||
fun error(view: View, msg: String, length: Int): Snackbar { | ||
return createSanckbar(view, msg, length, ERROR) | ||
} | ||
|
||
fun networkError(view: View, msg: String, length: Int): Snackbar { | ||
return createSanckbar(view, msg, length, NETWORKERROR) | ||
} | ||
|
||
@SuppressLint("WrongConstant") | ||
private fun createSanckbar(view: View, msg: String, length: Int, type: Int): Snackbar { | ||
|
||
val snackBar = Snackbar.make(view, "", length) | ||
snackBar.view.setBackgroundColor(Color.TRANSPARENT) | ||
|
||
snackBar.view.updateLayoutParams<FrameLayout.LayoutParams> { | ||
gravity = Gravity.TOP | ||
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)) { | ||
val systemBarTopInset: Int = ViewCompat.getRootWindowInsets(view) | ||
?.getInsets(WindowInsets.Type.statusBars()) | ||
?.bottom | ||
?: 0 | ||
updateMargins(top = topMargin + systemBarTopInset) | ||
} | ||
ViewCompat.setLayoutDirection(snackBar.view, ViewCompat.LAYOUT_DIRECTION_LOCALE) | ||
} | ||
|
||
val snackBarLayout = snackBar.view as Snackbar.SnackbarLayout | ||
snackBarLayout.setPadding(0, 0, 0, 0) | ||
|
||
when (type) { | ||
SUCCESS -> { | ||
val binding = LayoutSuccessBinding.inflate(LayoutInflater.from(view.context)) | ||
binding.messageView.text = msg | ||
binding.closeBtn.setOnClickListener { | ||
snackBar.dismiss() | ||
} | ||
snackBarLayout.addView(binding.root) | ||
} | ||
INFO -> { | ||
val binding = LayoutInfoBinding.inflate(LayoutInflater.from(view.context)) | ||
binding.messageView.text = msg | ||
binding.closeBtn.setOnClickListener { | ||
snackBar.dismiss() | ||
} | ||
snackBarLayout.addView(binding.root) | ||
} | ||
WARNING -> { | ||
val binding = LayoutWarningBinding.inflate(LayoutInflater.from(view.context)) | ||
binding.messageView.text = msg | ||
binding.closeBtn.setOnClickListener { | ||
snackBar.dismiss() | ||
} | ||
snackBarLayout.addView(binding.root) | ||
} | ||
ERROR -> { | ||
val binding = LayoutDangerBinding.inflate(LayoutInflater.from(view.context)) | ||
binding.messageView.text = msg | ||
binding.closeBtn.setOnClickListener { | ||
snackBar.dismiss() | ||
} | ||
snackBarLayout.addView(binding.root) | ||
} | ||
|
||
NETWORKERROR -> { | ||
val binding = LayoutNetworkErrorBinding.inflate(LayoutInflater.from(view.context)) | ||
binding.messageView.text = msg | ||
binding.closeBtn.setOnClickListener { | ||
snackBar.dismiss() | ||
} | ||
snackBarLayout.addView(binding.root) | ||
} | ||
} | ||
return snackBar | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="800dp" | ||
android:height="800dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:pathData="M19,5L5,19M5,5L19,19" | ||
android:strokeLineJoin="round" | ||
android:strokeWidth="1.5" | ||
android:fillColor="#00000000" | ||
android:strokeColor="#ffffff" | ||
android:strokeLineCap="round"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="@color/white" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24" | ||
android:tint="?attr/colorControlNormal"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="@color/error"/> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="@color/info"/> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="@color/success"/> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="@color/warning"/> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:tools="http://schemas.android.com/tools" | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<com.google.android.material.card.MaterialCardView | ||
android:layout_width="match_parent" | ||
android:layout_height="57dp" | ||
app:cardBackgroundColor="@color/error" | ||
android:layout_marginTop="10dp" | ||
app:cardCornerRadius="4dp" | ||
android:layout_marginBottom="10dp" | ||
app:cardElevation="3dp"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="horizontal" | ||
android:gravity="center"> | ||
|
||
|
||
<LinearLayout | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:paddingBottom="16dp" | ||
android:paddingTop="16dp" | ||
android:layout_marginStart="16dp" | ||
android:layout_marginEnd="16dp" | ||
android:orientation="vertical"> | ||
<TextView | ||
android:id="@+id/messageView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:textColor="@color/white" | ||
android:fontFamily="sans-serif-condensed" | ||
android:textSize="15sp"/> | ||
</LinearLayout> | ||
|
||
<ImageView | ||
android:id="@+id/closeBtn" | ||
android:layout_width="40dp" | ||
android:layout_height="match_parent" | ||
android:src="@drawable/cross_svgrepo_com" | ||
android:padding="5dp" | ||
android:clickable="true" | ||
android:focusable="true" | ||
android:contentDescription="@null" | ||
android:layout_marginEnd="8dp" /> | ||
</LinearLayout> | ||
</com.google.android.material.card.MaterialCardView> | ||
|
||
</LinearLayout> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
tools:ignore="MissingDefaultResource"> | ||
|
||
<com.google.android.material.card.MaterialCardView | ||
android:layout_width="match_parent" | ||
android:layout_height="55dp" | ||
app:cardBackgroundColor="@color/info" | ||
android:layout_marginTop="10dp" | ||
app:cardCornerRadius="7dp" | ||
android:layout_marginBottom="10dp" | ||
app:cardElevation="3dp"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="horizontal" | ||
android:gravity="center"> | ||
|
||
|
||
<LinearLayout | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:paddingBottom="16dp" | ||
android:paddingTop="16dp" | ||
android:layout_marginStart="16dp" | ||
android:layout_marginEnd="16dp" | ||
android:orientation="vertical"> | ||
<TextView | ||
android:id="@+id/messageView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:textColor="@color/white" | ||
android:fontFamily="sans-serif-condensed" | ||
android:textSize="15sp"/> | ||
</LinearLayout> | ||
|
||
<ImageView | ||
android:id="@+id/closeBtn" | ||
android:layout_width="40dp" | ||
android:layout_height="match_parent" | ||
android:src="@drawable/icbase_close" | ||
android:padding="5dp" | ||
android:clickable="true" | ||
android:focusable="true" | ||
android:contentDescription="@null" | ||
android:layout_marginEnd="8dp" /> | ||
</LinearLayout> | ||
</com.google.android.material.card.MaterialCardView> | ||
|
||
</LinearLayout> | ||
</layout> |
Oops, something went wrong.