Skip to content

Commit d48fc3f

Browse files
author
Prashant
committed
Add Icons for Error and Warning & improve design.
1 parent ed3a8c6 commit d48fc3f

File tree

10 files changed

+169
-43
lines changed

10 files changed

+169
-43
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ allprojects {
2323

2424
> Step 3. Now use it directly in your code like
2525
26-
```For Simple / Normal :
26+
```
27+
For Simple / Normal :
2728
MyToast.simpleToast(context = this, simpleMessage = "Toast Message")
2829
2930
For Error toast :
@@ -32,11 +33,10 @@ allprojects {
3233
For Warning toast :
3334
MyToast.warningToast(context =this, warningMessage = "use 'name' in upper case")
3435
35-
You can set toast position on screen by Gravity class like
36-
* By default Gravity was set to bottom
37-
* Gravity.TOP : for align Top
38-
* Gravity.BOTTOM : for align Bottom
39-
* Gravity.CENTER : for align Center
36+
You can set toast position top/center/bottom
37+
* By default Toast position was set to bottom e.g positionDefault
38+
* MyToast.positionTop : for align Top
39+
* MyToast.positionCenter : for align Center
4040
41-
example - MyToast.simpleToast(context = this, simpleMessage = "Toast Message", position = Gravity.TOP)
41+
example - MyToast.simpleToast(context = this, simpleMessage = "Toast Message", position = MyToast.positionTop)
4242
```

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies {
3636

3737
implementation 'androidx.core:core-ktx:1.9.0'
3838
implementation 'androidx.appcompat:appcompat:1.6.0'
39-
implementation 'com.google.android.material:material:1.7.0'
39+
implementation 'com.google.android.material:material:1.8.0'
4040
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4141
implementation project(path: ':mytoast')
4242
testImplementation 'junit:junit:4.13.2'

app/src/main/java/com/sumfactor/mytoast/MainActivity.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.sumfactor.mytoast
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5-
import android.view.Gravity
65
import android.widget.Button
76

87
class MainActivity : AppCompatActivity() {
@@ -11,15 +10,21 @@ class MainActivity : AppCompatActivity() {
1110
setContentView(R.layout.activity_main)
1211

1312
findViewById<Button>(R.id.button1).setOnClickListener {
13+
// use this without mention default position on toast
1414
MyToast.simpleToast(this, "Toast Message")
15+
// or use this with mentioning 'default' position of toast into bottom
16+
MyToast.simpleToast(this, "Toast Message", MyToast.positionDefault)
17+
1518
}
1619

1720
findViewById<Button>(R.id.button2).setOnClickListener {
18-
MyToast.errorToast(this, "error on uploading", Gravity.TOP)
21+
// Error toast message with 'top' position
22+
MyToast.errorToast(this, "Error on uploading", MyToast.positionTop)
1923
}
2024

2125
findViewById<Button>(R.id.button3).setOnClickListener {
22-
MyToast.warningToast(this, "use 'name' in upper case!" , Gravity.CENTER)
26+
// Warning toast message with 'center' position
27+
MyToast.warningToast(this, "Use 'name' in upper case!", MyToast.positionCenter)
2328
}
2429

2530
}

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.3.1' apply false
4-
id 'com.android.library' version '7.3.1' apply false
3+
id 'com.android.application' version '7.4.0' apply false
4+
id 'com.android.library' version '7.4.0' apply false
55
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Thu Jan 12 15:27:26 IST 2023
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
11
package com.sumfactor.mytoast
22

3-
import android.content.Context
3+
import android.app.Activity
44
import android.graphics.Color
55
import android.view.Gravity
6-
import android.view.LayoutInflater
76
import android.view.View
7+
import android.widget.ImageView
8+
import android.widget.LinearLayout
89
import android.widget.TextView
910
import android.widget.Toast
1011

1112
object MyToast {
1213

13-
fun simpleToast(context: Context, simpleMessage: String, position: Int = Gravity.BOTTOM) {
14-
val getToast = getToast(context = context, message = simpleMessage, color = "#FAFAFA")
15-
getToast.setGravity(position, 0, 0)
14+
// TODO : Define toast position
15+
const val positionDefault: Int = Gravity.BOTTOM
16+
const val positionCenter: Int = Gravity.CENTER
17+
const val positionTop: Int = Gravity.TOP
18+
19+
// TODO : Create a simple toast with default ( Bottom ) position
20+
fun simpleToast(context: Activity, simpleMessage: String, position: Int = positionDefault) {
21+
val getToast =
22+
getToast(context = context, message = simpleMessage, color = "#FAFAFA", iconIndex = 0)
23+
getToast.setGravity(isValidPosition(position), 0, 0)
1624
getToast.show()
1725
}
1826

19-
fun warningToast(context: Context, warningMessage: String, position: Int = Gravity.BOTTOM) {
20-
val getToast = getToast(context = context, message = warningMessage, color = "#FFC107")
21-
getToast.setGravity(position, 0, 0)
27+
// TODO : Create a warning toast with default ( Bottom ) position
28+
fun warningToast(context: Activity, warningMessage: String, position: Int = positionDefault) {
29+
val getToast =
30+
getToast(context = context, message = warningMessage, color = "#FFC107", iconIndex = 1)
31+
getToast.setGravity(isValidPosition(position), 0, 0)
2232
getToast.show()
2333
}
2434

25-
fun errorToast(context: Context, errorMessage: String, position: Int = Gravity.BOTTOM) {
26-
val getToast = getToast(context = context, message = errorMessage, color = "#FF5722")
27-
getToast.setGravity(position, 0, 0)
35+
// TODO : Create a Error toast with default ( Bottom ) position
36+
fun errorToast(context: Activity, errorMessage: String, position: Int = positionDefault) {
37+
val getToast =
38+
getToast(context = context, message = errorMessage, color = "#F44336", iconIndex = 2)
39+
getToast.setGravity(isValidPosition(position), 0, 0)
2840
getToast.show()
2941
}
3042

31-
private fun getToast(context: Context, message: String, color: String): Toast {
43+
// TODO : Get Toast with custom layout and textview
44+
private fun getToast(context: Activity, message: String, color: String, iconIndex: Int): Toast {
3245
val toast = Toast(context)
3346
toast.duration = Toast.LENGTH_SHORT
34-
val inflater: LayoutInflater =
35-
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
36-
val view: View = inflater.inflate(R.layout.toast_custom_layout,null)
47+
48+
// val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
49+
50+
val inflater = context.layoutInflater
51+
52+
val view: View = inflater.inflate(R.layout.toast_custom_layout, null)
3753
val textView = view.findViewById<TextView>(R.id.toast_message)
54+
val icon = view.findViewById<ImageView>(R.id.toast_logo)
55+
val toastBox = view.findViewById<LinearLayout>(R.id.toast_box)
56+
when (iconIndex) {
57+
0 -> icon.visibility = View.GONE
58+
1 -> icon.setImageResource(R.drawable.warning)
59+
else -> icon.setImageResource(R.drawable.error)
60+
}
3861
textView.text = message
39-
textView.setBackgroundColor(Color.parseColor(color))
62+
toastBox.setBackgroundColor(Color.parseColor(color))
4063
toast.view = view
4164
return toast
4265
}
4366

67+
// TODO : Checking pos value id valid or not
68+
private fun isValidPosition(pos: Int): Int {
69+
val isValid: Boolean = pos == positionDefault || pos == positionCenter || pos == positionTop
70+
return if (isValid) pos else positionDefault
71+
}
4472

4573
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:fillColor="#000"
8+
android:pathData="m22,12c0,5.523 -4.477,10 -10,10 -5.523,0 -10,-4.477 -10,-10 0,-5.523 4.477,-10 10,-10 5.523,0 10,4.477 10,10z" />
9+
<path
10+
android:fillColor="#000"
11+
android:pathData="m7.05,9.4 l3.536,3.6 -3.536,3.5 1.414,1.4 3.536,-3.5 3.536,3.5 1.414,-1.4 -3.536,-3.5 3.536,-3.6 -1.414,-1.4 -3.536,3.5 -3.536,-3.5 -1.414,1.4z" />
12+
<path
13+
android:fillColor="#ecf0f1"
14+
android:pathData="m7.05,8.4 l3.536,3.6 -3.536,3.5 1.414,1.4 3.536,-3.5 3.536,3.5 1.414,-1.4 -3.536,-3.5 3.536,-3.6 -1.414,-1.4 -3.536,3.5 -3.536,-3.5 -1.414,1.4z" />
15+
</vector>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="48dp"
3+
android:height="48dp"
4+
android:viewportWidth="48"
5+
android:viewportHeight="48">
6+
<path
7+
android:fillAlpha="0.40206185"
8+
android:fillType="nonZero"
9+
android:pathData="M8.107,36.206l30.308,0l0,9.982l-30.308,0z"
10+
android:strokeWidth="0.022"
11+
android:strokeAlpha="0.40206185"
12+
android:strokeColor="#00000000"
13+
android:strokeLineCap="round"
14+
android:strokeLineJoin="miter" />
15+
<path
16+
android:fillAlpha="0.40206185"
17+
android:fillType="nonZero"
18+
android:pathData="M38.415,36.207C38.415,36.207 38.415,46.188 38.415,46.188C41.647,46.207 46.229,43.952 46.229,41.197C46.229,38.442 42.622,36.207 38.415,36.207z"
19+
android:strokeWidth="0.022"
20+
android:strokeAlpha="0.40206185"
21+
android:strokeColor="#00000000"
22+
android:strokeLineCap="round"
23+
android:strokeLineJoin="miter" />
24+
<path
25+
android:fillAlpha="0.40206185"
26+
android:fillType="nonZero"
27+
android:pathData="M8.107,36.207C8.107,36.207 8.107,46.188 8.107,46.188C4.875,46.207 0.293,43.952 0.293,41.197C0.293,38.442 3.9,36.207 8.107,36.207z"
28+
android:strokeWidth="0.022"
29+
android:strokeAlpha="0.40206185"
30+
android:strokeColor="#00000000"
31+
android:strokeLineCap="round"
32+
android:strokeLineJoin="miter" />
33+
<path
34+
android:fillColor="#000"
35+
android:fillType="nonZero"
36+
android:pathData="M43.269,38.596L26.365,6.871C25.879,5.99 25,5.5 24.02,5.5C23.041,5.5 22.158,6.087 21.664,6.969L4.886,38.694C4.491,39.477 4.486,40.554 4.972,41.337C5.459,42.121 6.24,42.512 7.22,42.512L40.903,42.512C41.882,42.512 42.765,42.023 43.16,41.24C43.653,40.456 43.657,39.477 43.269,38.596z"
37+
android:strokeWidth="0.6382978"
38+
android:strokeColor="#000" />
39+
<path
40+
android:fillType="nonZero"
41+
android:pathData="M9.771,38.106C9.476,38.603 9.768,39 10.257,39L37.87,39C38.359,39 38.655,38.603 38.363,38.106L24.472,11.674C24.18,11.177 23.788,11.177 23.591,11.674L9.771,38.106z"
42+
android:strokeColor="#00000000" />
43+
<path
44+
android:fillAlpha="0.5"
45+
android:fillColor="#00000000"
46+
android:fillType="nonZero"
47+
android:pathData="M41.753,37.874L25.94,8.235C25.267,6.958 25.025,6.549 24.002,6.549C23.121,6.549 22.661,7.145 22.025,8.351L6.387,37.965C5.479,39.628 5.382,40.061 5.819,40.793C6.256,41.525 6.817,41.485 8.89,41.533L39.625,41.533C41.556,41.557 41.991,41.41 42.346,40.678C42.789,39.946 42.601,39.365 41.753,37.874z"
48+
android:strokeWidth="0.63829792"
49+
android:strokeAlpha="0.5" />
50+
<path
51+
android:fillColor="#fff"
52+
android:fillType="nonZero"
53+
android:pathData="M24,35.682C22.869,35.682 22,34.812 22,33.682C22,32.464 22.783,31.682 24,31.682C25.217,31.682 25.913,32.464 26,33.682C26,34.812 25.217,35.682 24,35.682L24,35.682zM22.783,30.551L22.261,20.551L25.739,20.551L25.217,30.551L22.696,30.551L22.783,30.551z"
54+
android:strokeColor="#00000000" />
55+
</vector>

mytoast/src/main/res/layout/toast_custom_layout.xml

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
6+
android:layout_height="wrap_content"
7+
android:padding="5dp">
68

79
<androidx.cardview.widget.CardView
810
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
912
app:cardCornerRadius="10dp"
10-
android:layout_height="wrap_content"
11-
android:layout_margin="10dp"
12-
app:layout_constraintEnd_toEndOf="parent"
13-
app:layout_constraintStart_toStartOf="parent"
14-
app:layout_constraintTop_toTopOf="parent">
13+
app:cardElevation="0dp">
1514

16-
<TextView
17-
android:id="@+id/toast_message"
18-
android:layout_width="wrap_content"
19-
android:layout_height="wrap_content"
20-
android:padding="8dp"
21-
android:textColor="#000"
22-
android:textSize="12sp" />
15+
<LinearLayout
16+
android:id="@+id/toast_box"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent"
19+
android:orientation="horizontal"
20+
android:padding="5dp"
21+
tools:ignore="UseCompoundDrawables">
22+
23+
<ImageView
24+
android:id="@+id/toast_logo"
25+
android:layout_width="24dp"
26+
android:layout_height="24dp"
27+
android:layout_gravity="center_vertical"
28+
app:layout_constraintBottom_toBottomOf="parent"
29+
app:layout_constraintStart_toStartOf="parent"
30+
app:layout_constraintTop_toTopOf="parent"
31+
tools:ignore="ContentDescription" />
32+
33+
<TextView
34+
android:id="@+id/toast_message"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:layout_gravity="center_vertical"
38+
android:layout_marginStart="5dp"
39+
android:inputType="textMultiLine"
40+
android:textColor="#000"
41+
android:textSize="14sp"
42+
tools:ignore="TextViewEdits" />
43+
44+
</LinearLayout>
2345

2446
</androidx.cardview.widget.CardView>
2547

0 commit comments

Comments
 (0)