Skip to content

Commit

Permalink
Added:
Browse files Browse the repository at this point in the history
- The ability to change layout gravity
- The ability to change progress bar gravity (does not change with layout gravity)
- The ability to add an image to the dialog

Changed:
- Dialog default gravity has been changed to center
- UI Components ids changed
  • Loading branch information
Ido-Barnea committed Apr 2, 2022
1 parent 37deec5 commit 3aa1040
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 62 additions & 8 deletions Dialoger/src/main/java/com/barnea/dialoger/Dialoger.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.barnea.dialoger

import android.app.ActionBar
import android.app.AlertDialog
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.*
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.ContextCompat

class Dialoger(
context_: Context,
Expand Down Expand Up @@ -305,6 +306,52 @@ class Dialoger(
return this
}

/**
* @param drawable the drawable used for the imageView
* @return Dialoger object
*/
fun setDrawable(drawable: Drawable): Dialoger {
getDialogImageView().setImageDrawable(drawable)
return this
}

/**
* @param drawableInt the drawable int used for the imageView
* @return Dialoger object
*/
fun setDrawable(drawableInt: Int): Dialoger {
getDialogImageView().setImageDrawable(ContextCompat.getDrawable(context, drawableInt))
return this
}

/**
* Default gravity is center
* ProgressBar gravity stays as center, to change that use setProgressBarGravity
* @param gravity the gravity of all the children in the dialog
* @return Dialoger object
*/
fun setGravity(gravity: Int): Dialoger {
getDialogBackground().gravity = gravity
return this
}

/**
* Default gravity is center
* @param gravity the gravity of all the children in the dialog
* @return Dialoger object
*/
fun setProgressBarGravity(gravity: Int): Dialoger {
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
this.gravity = gravity
}

getDialogProgressBar().layoutParams = params
return this
}

/**
* @param isCanceledOnTouchOutside whether the dialog will be dismissed on touch outside of it or not
* @return Dialoger object
Expand All @@ -318,35 +365,42 @@ class Dialoger(
* @return the dialog title textView
*/
private fun getDialogTitle(): TextView {
return dialogView.findViewById(R.id.dialog_title)
return dialogView.findViewById(R.id.dialogTitle)
}

/**
* @return the dialog text textView
*/
private fun getDialogText(): TextView {
return dialogView.findViewById(R.id.dialog_text)
return dialogView.findViewById(R.id.dialogText)
}

/**
* @return the dialog image imageView
*/
private fun getDialogImageView(): ImageView {
return dialogView.findViewById(R.id.dialogImageView)
}

/**
* @return the dialog button
*/
private fun getDialogButton(): TextView {
return dialogView.findViewById(R.id.dialog_button)
return dialogView.findViewById(R.id.dialogButton)
}

/**
* @return the dialog progress bar
*/
private fun getDialogProgressBar(): ProgressBar {
return dialogView.findViewById(R.id.dialog_progressBar)
return dialogView.findViewById(R.id.dialogProgressBar)
}

/**
* @return the dialog parent view
*/
private fun getDialogBackground(): LinearLayout {
return dialogView.findViewById(R.id.dialog_background)
return dialogView.findViewById(R.id.dialogBackground)
}

}
24 changes: 17 additions & 7 deletions Dialoger/src/main/res/layout/layout_dialog.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_background"
android:id="@+id/dialogBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/rounded_background"
android:orientation="vertical"
android:padding="24dp">

<ImageView
android:id="@+id/dialogImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="100dp"
android:scaleType="centerCrop"
android:paddingBottom="16dp"
android:contentDescription="@string/default_image_content_description" />

<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:id="@+id/dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_title"
android:textStyle="bold"
Expand All @@ -19,8 +29,8 @@
android:textColor="@color/black" />

<TextView
android:id="@+id/dialog_text"
android:layout_width="match_parent"
android:id="@+id/dialogText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_text"
android:textAlignment="inherit"
Expand All @@ -29,7 +39,7 @@
android:textColor="@color/black" />

<ProgressBar
android:id="@+id/dialog_progressBar"
android:id="@+id/dialogProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
Expand All @@ -38,7 +48,7 @@
android:layout_marginTop="24dp" />

<Button
android:id="@+id/dialog_button"
android:id="@+id/dialogButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
Expand Down
1 change: 1 addition & 0 deletions Dialoger/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>
<string name="default_title">Default Title</string>
<string name="default_text">Default text default text default text default text</string>
<string name="default_image_content_description">Dialog Image</string>
<string name="default_button_text">OK</string>
<string name="loading">Loading…</string>
<string name="success">Success!</string>
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/barnea/dialoger/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.barnea.dialoger

import android.os.Bundle
import android.os.Handler
import android.view.Gravity
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

Expand All @@ -14,6 +15,8 @@ class MainActivity : AppCompatActivity() {
.setTitle("This is a loading dialog...")
.setText("This might take a while...")
.setProgressBarColor("#7acf19")
.setDrawable(R.drawable.ic_launcher_background)
.setGravity(Gravity.START)
.show()

// dismiss the dialog after 3 seconds
Expand All @@ -29,6 +32,6 @@ class MainActivity : AppCompatActivity() {
Toast.makeText(this, "dialog button clicked", Toast.LENGTH_SHORT).show()
}
.show()
}, 3000)
}, 10000)
}
}

0 comments on commit 3aa1040

Please sign in to comment.