diff --git a/.idea/gradle.xml b/.idea/gradle.xml index ae388c2..385c6f5 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -11,7 +11,10 @@ diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..568bea1 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/SJDialog/.gitignore b/SJDialog/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/SJDialog/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/SJDialog/BasicDialogDoc.md b/SJDialog/BasicDialogDoc.md new file mode 100644 index 0000000..170d04e --- /dev/null +++ b/SJDialog/BasicDialogDoc.md @@ -0,0 +1,147 @@ +# BasicDialog Documentation +Create dialog example +```java +BasicDialog dialog = new BasicDialog(); +dialog.Builder(context) + .setTitle("Title") + .setLeftButtonText("button1") + .setRightButtonText("button2") + .onButtonClick(() -> { + // Do something + }) + .show(); +``` +![BasicDialog example](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/BasicDialog%20example%201.png) +## Builder +Apply the default theme to a dialog +```java +dialog.Builder(context) +``` +![BasicDialog day-night](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/BasicDialog%20day-night.png) + +Apply the app theme to a dialog **(only works with material3 theme)** +```java +dialog.Builder(context,true) +``` +Apply the custom theme to a dialog **(only works with material3 theme)** +```java +dialog.Builder(context,theme) +``` +## Old Dialog theme +By default dialog colors will be set to material3 dynamic colors. With this method you can set the dialog color for the background and buttons to the older non-dynamic colors +```java +dialog.setOldTheme(); +``` +![BasicDialog oldTheme](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/BasicDialog%20oldTheme.png) +## Add onClick Listener +onClickListener for right button. The left button is for dismissing dialog +```java +dialog.onButtonClick(new DialogButtonEvent() { + @Override + public void onButtonClick() { + // Do something + } +}); + +//or + +dialog.onButtonClick(() -> { + // Do something +}); +``` +onClickListener for left and right button +```java +dialog.onButtonClick(new DialogButtonEvents() { + @Override + public void onLeftButtonClick() { + // Do something + } + + @Override + public void onRightButtonClick() { + // Do something + } +}); +``` + +## All BasicDialog Methods +```java +//Create dialog +dialog.Builder(context); + +//Usin the old dialog theme +dialog.setOldTheme(); + +//Set title +dialog.setTitle("Title"); +//Set message +dialog.setMessage("Message"); +//Set title text alignment +dialog.setTitleAlignment(TextAlignment); +//Set message text alignment +dialog.setMessageAlignment(TextAlignment); +//Set left button text +dialog.setLeftButtonText("Text"); +//Set right button text +dialog.setRightButtonText("Text"); + +//Set text color +dialog.setTextColor(color); +//Set title text color +dialog.setTitleTextColor(color); +//Set message text color +dialog.setMessageTextColor(color); + +//Set buttons color +dialog.setButtonsColor(color); +//Set left button color +dialog.setLeftButtonColor(color); +//Set right button color +dialog.setRightButtonColor(color); + +//Set buttons text color +dialog.setButtonsTextColor(color); +//Set text color for left button +dialog.setLeftButtonTextColor(color); +//Set text color for right button +dialog.setRightButtonTextColor(color); + +//Set buttons background resource +dialog.setButtonsBackgroundResource(drawable); +//Set left button background resource +dialog.setLeftButtonBackgroundResource(drawable); +//Set right button background resource +dialog.setRightButtonBackgroundResource(drawable); + +//Set dialog color +dialog.setDialogBackgroundColor(color); +//Set dialog background resource +dialog.setDialogBackgroundResource(drawable); + +//Set maximum dialog width. Default is 600dp +dialog.setMaxDialogWidth(width); + +//Get maximum dialog width +int dialogWidth = dialog.getMaxDialogWidth(); + +//Get left button +Button leftButton = dialog.getLeftButton(); +//Get right button +Button rightButton = dialog.getRightButton(); + +//Set dialog animations +dialog.setDialogAnimations(styleRes); + +//Enable or disable swipe down to dismiss dialog. +//By default is set to true +dialog.swipeToDismiss(boolean); + +//Set dialog onTouchListener. +//This method will overide swipe down to dismiss action +dialog.setOnTouchListener(onTouchListener); + +//Shew dialog +dialog.show(); +//Dismiss dialog +dialog.dismiss(); +``` diff --git a/SJDialog/CustomViewDialogDoc.md b/SJDialog/CustomViewDialogDoc.md new file mode 100644 index 0000000..040a57f --- /dev/null +++ b/SJDialog/CustomViewDialogDoc.md @@ -0,0 +1,200 @@ +# CustomViewDialog Documentation +## Examples +#### Add button example +```java +Button button1 = new Button(this); +button1.setText("Button"); + +CustomViewDialog customViewDialog = new CustomViewDialog(); +customViewDialog.Builder(this) + .setTitle("Title") + .addCustomView(button1) + .show(); +``` +![CustomViewDialog example 1](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/CustomViewDialog%20example%201.png) +#### Add EditText example +```java +EditText editText = new EditText(this); +editText.setHint("add text"); + +CustomViewDialog customViewDialog = new CustomViewDialog(); +customViewDialog.Builder(this) + .setTitle("Title") + .dialogWithTwoButtons() + .addCustomView(editText) + .onButtonClick(() -> { + String text = editText.getText().toString(); + // Do something + }) + .show(); +``` +![CustomViewDialog example 2](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/CustomViewDialog%20example%202.png) +#### Add custom xml layout example +```java +View view = LayoutInflater.from(this).inflate(R.layout.custon_layout,null); + +CustomViewDialog customViewDialog = new CustomViewDialog(); +customViewDialog.Builder(this) + .setTitle("Title") + .dialogWithTwoButtons() + .addCustomView(view) + .show(); + +``` +![CustomViewDialog example 3](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/CustomViewDialog%20example%203.png) +## Builder +Apply the default theme to a dialog +```java +customViewDialog.Builder(context) +``` +![CustomViewDialog day-night](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/CustomViewDialog%20day-night.png) +Apply the app theme to a dialog **(only works with material3 theme)** +```java +customViewDialog.Builder(context,true) +``` +Apply the custom theme to a dialog **(only works with material3 theme)** +```java +customViewDialog.Builder(context,theme) +``` +## Dialog with two buttons +```java +customViewDialog.dialogWithTwoButtons(); +``` +## Old Dialog theme +By default dialog colors will be set to material3 dynamic colors. With this method you can set the dialog color for the background and buttons to the older non-dynamic colors +```java +customViewDialog.setOldTheme(); +``` +![CustomViewDialog oldTheme](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/CustomViewDialog%20oldTheme.png) +## Add View +```java +customViewDialog.addCustomView(view); +``` + +## Add onClick Listener +onClickListener for the right button if the dialog has [two buttons](#dialog-with-two-buttons), the left button is for dismissing dialog. If the dialog has only one button, onClickListener will be set to that button. +```java +customViewDialog.onButtonClick(new DialogButtonEvent() { + @Override + public void onButtonClick() { + // Do something + } +}); + +//or + +customViewDialog.onButtonClick(() -> { + // Do something +}); +``` +onClickListener for left and right button (only works when dialog has [two buttons](#dialog-with-two-buttons)) +```java +customViewDialog.onButtonClick(new DialogButtonEvents() { + @Override + public void onLeftButtonClick() { + // Do something + } + + @Override + public void onRightButtonClick() { + // Do something + } +}); +``` +## All CustomViewDialog Methods +```java +//Create dialog +customViewDialog.Builder(context); + +//Create dialog width two buttons +customViewDialog.dialogWithTwoButtons(); + +//Usin the old dialog theme +customViewDialog.setOldTheme(); + +//Set title +customViewDialog.setTitle("Title"); +//Set message +customViewDialog.setMessage("Message"); + +//Set title text alignment +customViewDialog.setTitleAlignment(TextAlignment); +//Set message text alignment +customViewDialog.setMessageAlignment(TextAlignment); + +//Set text color +customViewDialog.setTextColor(color); +//Set title text color +customViewDialog.setTitleTextColor(color); +//Set message text color +customViewDialog.setMessageTextColor(color); + +//Set button text (one button dialog) +customViewDialog.setButtonText("Text"); +//Set left button text +customViewDialog.setLeftButtonText("Text"); +//Set right button text +customViewDialog.setRightButtonText("Text"); + +//Set buttons color +customViewDialog.setButtonsColor(color); +//Set button color (one button dialog) +customViewDialog.setButtonColor(color); +//Set left button color +customViewDialog.setLeftButtonColor(color); +//Set right button color +customViewDialog.setRightButtonColor(color); + +//Set buttons text color +customViewDialog.setButtonsTextColor(color); +//Set text color a button (one button dialog) +customViewDialog.setButtonTextColor(color); +//Set text color for left button +customViewDialog.setLeftButtonTextColor(color); +//Set text color for right button +customViewDialog.setRightButtonTextColor(color); + +//Set buttons background resource +customViewDialog.setButtonsBackgroundResource(drawable); +//Set button background resource (one button dialog) +customViewDialog.setButtonBackgroundResource(drawable); +//Set left button background resource +customViewDialog.setLeftButtonBackgroundResource(drawable); +//Set right button background resource +customViewDialog.setRightButtonBackgroundResource(drawable); + +//Set dialog color +customViewDialog.setDialogBackgroundColor(color); +//Set dialog background resource +customViewDialog.setDialogBackgroundResource(drawable); + +//Add Custom view +customViewDialog.addCustomView(view); + +//Set maximum dialog width. Default is 600dp +customViewDialog.setMaxDialogWidth(width); + +//Get maximum dialog width +int dialogWidth = customViewDialog.getMaxDialogWidth(); + +//Get left button +Button leftButton = customViewDialog.getLeftButton(); +//Get right button +Button rightButton = customViewDialog.getRightButton(); + +//Set dialog animations +customViewDialog.setDialogAnimations(styleRes); + +//Enable or disable swipe down to dismiss dialog. +//By default is set to true +dialog.swipeToDismiss(boolean); + +//Set dialog onTouchListener. +//This method will overide swipe down to dismiss action +dialog.setOnTouchListener(onTouchListener); + +//Shew dialog +customViewDialog.show(); +//Dismiss dialog +customViewDialog.dismiss(); +``` diff --git a/SJDialog/ListDialogDoc.md b/SJDialog/ListDialogDoc.md new file mode 100644 index 0000000..233a1c7 --- /dev/null +++ b/SJDialog/ListDialogDoc.md @@ -0,0 +1,428 @@ +# ListDialog Documentation +## Examples +#### List of String array +```java +String[] strings = {"item1","item2","item3"}; + +ListDialog listDialog = new ListDialog(); +listDialog.Builder(this) + .setItems(strings,(position, value) -> { + // Do something + }) + .show(); +``` +![ListDialog example 1](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/ListDialog%20example%201.png) +#### List of Objects +```java +class ExampleObject{ + String value; + + public ExampleObject(String value) { + this.value = value; + } +} +``` + +```java +ExampleObject[] objects = {new ExampleObject("object1"),new ExampleObject("object2"),new ExampleObject("object3")}; + +ListDialog listDialog = new ListDialog(); +listDialog.Builder(this) + .setItems( + objects, + obj -> obj.value, // get value from object + (position, value) -> { + // Do something + }) + .show(); +``` +![ListDialog example 2](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/ListDialog%20example%202.png) +#### ArrayList of Objects with two values +```java +class ExampleObject{ + String value1; + String value2; + + public ExampleObject(String value1, String value2) { + this.value1 = value1; + this.value2 = value2; + } +} +``` +```java +ArrayList arrayList = new ArrayList<>(); +arrayList.add(new ExampleObject("object1","value1")); +arrayList.add(new ExampleObject("object2","value2")); +arrayList.add(new ExampleObject("object3","value3")); + +ListDialog listDialog = new ListDialog(); +listDialog.Builder(this) + .setItems( + arrayList, + new ListItemValues() { + @Override + public String getValue1(ExampleObject obj) { + return obj.value1; + } + + @Override + public String getValue2(ExampleObject obj) { + return obj.value2; + } + }, + (position, value) -> { + // Do something + }) + .show(); +``` +![ListDialog example 3](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/ListDialog%20example%203.png) +#### Selecting multiple items in a list +```java +ArrayList stringArrayList = new ArrayList<>(); +stringArrayList.add("item1"); +stringArrayList.add("item2"); +stringArrayList.add("item3"); + +ListDialog listDialog = new ListDialog(); +listDialog.Builder(this) + .dialogWithTwoButtons() + .setSelectableList() + .setItems(stringArrayList,obj -> obj) + .onButtonClick(() -> { + ArrayList selectedItems = listDialog.getSelectedItems(); + // Do something + }) + .show(); +``` +![ListDialog example 4](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/ListDialog%20example%204.png) +## Builder +Apply the default theme to a dialog +```java +listDialog.Builder(context) +``` +![ListDialog day-night](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/ListDialog%20day-night.png) +Apply the app theme to a dialog **(only works with material3 theme)** +```java +listDialog.Builder(context,true) +``` +Apply the custom theme to a dialog **(only works with material3 theme)** +```java +listDialog.Builder(context,theme) +``` +## Dialog with two buttons +```java +listDialog.dialogWithTwoButtons(); +``` +## Old Dialog theme +By default dialog colors will be set to material3 dynamic colors. With this method you can set the dialog color for the background and buttons to the older non-dynamic colors +```java +listDialog.setOldTheme(); +``` +![ListDialog oldTheme](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/ListDialog%20oldTheme.png) +## Select multiple items in a list +```java +listDialog.setSelectableList(); +``` +## Add items in a list +You can add item in a list by setting [ReciclerView Adapter](#set-reciclerview-adapter), using [setItems()](#setitems) method or using [setImageItems()](#setimageitems) method for list with icons. +### Set ReciclerView Adapter +```java +listDialog.setAdapter(recyclerViewAdapter); +``` +### setItems +This method uses [DefaultListAdapter](/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java) for array of Strings or [DefaultListAdapterGeneric](/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java) for array of Objects or ArrayList. + +#### Array of Strings +You need to use [setSelectableList()](#select-multiple-items-in-a-list) firstly or add [onListItemClick](#array-of-strings-and-onlistitemclick) +```java +listDialog.setItems(strings); +``` +#### Array of Strings and onListItemClick +```java +listDialog.setItems(strings,(position, value) -> { + // Do something +}); +``` +#### Array of Objects +You need to use [setSelectableList()](#select-multiple-items-in-a-list) firstly or add [onListItemClick](#array-of-objects-and-onlistitemclick) +```java +listDialog.setItems(objects, new ListItemValue() { + @Override + public String getValue(ExampleObject obj) { + return obj.value; // get value of an Object + } +}); + +//or + +listDialog.setItems(objects,obj -> obj.value); +``` +#### Array of Objects and onListItemClick +```java +listDialog.setItems(objects,obj -> obj.value,(position, obj) -> { + // Do something +}); +``` +#### Array of Objects with two values +You need to use [setSelectableList()](#select-multiple-items-in-a-list) firstly or add [onListItemClick](#array-of-objects-with-two-values-and-onlistitemclick) +```java +listDialog.setItems(objects, new ListItemValues() { + @Override + public String getValue1(ExampleObject obj) { + return obj.value1; + } + + @Override + public String getValue2(ExampleObject obj) { + return obj.value2; + } +}); +``` +#### Array of Objects with two values and onListItemClick +```java +listDialog.setItems(objects, new ListItemValues() { + @Override + public String getValue1(ExampleObject obj) { + return obj.value1; + } + + @Override + public String getValue2(ExampleObject obj) { + return obj.value2; + } + },(position, obj) -> { + // Do something +}); +``` +#### ArrayList +You need to use [setSelectableList()](#select-multiple-items-in-a-list) firstly or add [onListItemClick](#arraylist-and-onlistitemclick) +```java +listDialog.setItems(arrayList,obj -> obj.value); +``` +#### ArrayList and onListItemClick +```java +listDialog.setItems(arrayList,obj -> obj.value,(position, obj) -> { + // Do something +}); +``` +#### ArrayList with two values +You need to use [setSelectableList()](#select-multiple-items-in-a-list) firstly or add [onListItemClick](#arraylist-with-two-values-and-onlistitemclick) +```java +listDialog.setItems(arrayList, new ListItemValues() { + @Override + public String getValue1(ExampleObject obj) { + return obj.value1; + } + + @Override + public String getValue2(ExampleObject obj) { + return obj.value2; + } +}); +``` +#### ArrayList with two values and onListItemClick +```java +listDialog.setItems(arrayList, new ListItemValues() { + @Override + public String getValue1(ExampleObject obj) { + return obj.value1; + } + + @Override + public String getValue2(ExampleObject obj) { + return obj.value2; + } + },(position, obj) -> { + // Do something +}); +``` +### setImageItems +Creating ArrayList of [ImageListItem](/SJDialog/src/main/java/com/sjapps/library/customdialog/ImageListItem.java) +```java +ArrayList listItems = new ArrayList<>(); +listItems.add(new ImageListItem("item1", drawable1)); +// or adding data of type Object +listItems.add(new ImageListItem("item2", drawable2,data)); +``` +#### ArrayList of ImageListItem +You need to use [setSelectableList()](#select-multiple-items-in-a-list) firstly or add [onListItemClick](#arraylist-of-two-imagelistitem-and-onlistitemclick) +```java +listDialog.setImageItems(listItems); +``` +#### ArrayList of ImageListItem and onListItemClick +```java +listDialog.setImageItems(listItems, (position, obj) -> { + // Do something +}); +``` +## Set RecyclerView LayoutManager +```java +listDialog.setLayoutManager(layoutManager); +``` + +## Add onClick Listener +onClickListener for the right button if the dialog has [two buttons](#dialog-with-two-buttons), the left button is for dismissing dialog. If the dialog has only one button, onClickListener will be set to that button. +```java +listDialog.onButtonClick(new DialogButtonEvent() { + @Override + public void onButtonClick() { + // Do something + } +}); + +//or + +listDialog.onButtonClick(() -> { + // Do something +}); +``` +onClickListener for left and right button (only works when dialog has [two buttons](#dialog-with-two-buttons)) +```java +listDialog.onButtonClick(new DialogButtonEvents() { + @Override + public void onLeftButtonClick() { + // Do something + } + @Override + public void onRightButtonClick() { + // Do something + } +}); +``` +## All ListDialog Methods +```java +//Create dialog +listDialog.Builder(context); + +//Create dialog width two buttons +listDialog.dialogWithTwoButtons(); + +//Usin the old dialog theme +listDialog.setOldTheme(); + +//Set title +listDialog.setTitle("Title"); + +//Set message +listDialog.setMessage("Message"); + +//Set title text alignment +listDialog.setTitleAlignment(TextAlignment); +//Set message text alignment +listDialog.setMessageAlignment(TextAlignment); + +//Set text color +listDialog.setTextColor(color); +//Set title text color +listDialog.setTitleTextColor(color); +//Set message text color +listDialog.setMessageTextColor(color); + +//Set button text (one button dialog) +listDialog.setButtonText("Text"); +//Set left button text +listDialog.setLeftButtonText("Text"); +//Set right button text +listDialog.setRightButtonText("Text"); + +//Set buttons color +listDialog.setButtonsColor(color); +//Set button color (one button dialog) +listDialog.setButtonColor(color); +//Set left button color +listDialog.setLeftButtonColor(color); +//Set right button color +listDialog.setRightButtonColor(color); + +//Set buttons text color +listDialog.setButtonsTextColor(color); +//Set text color a button (one button dialog) +listDialog.setButtonTextColor(color); +//Set text color for left button +listDialog.setLeftButtonTextColor(color); +//Set text color for right button +listDialog.setRightButtonTextColor(color); + +//Set buttons background resource +listDialog.setButtonsBackgroundResource(drawable); +//Set button background resource (one button dialog) +listDialog.setButtonBackgroundResource(drawable); +//Set left button background resource +listDialog.setLeftButtonBackgroundResource(drawable); +//Set right button background resource +listDialog.setRightButtonBackgroundResource(drawable); + +//Set dialog color +listDialog.setDialogBackgroundColor(color); +//Set dialog background resource +listDialog.setDialogBackgroundResource(drawable); + +//Selecting multiple items in a list +listDialog.setSelectableList(); + +//Set text color of an items in a list +listDialog.setListItemTextColor(color); +//Set a background resource for items in a list +listDialog.setListItemBackgroundResource(drawable); +//Set a background resource for selected items in a list +listDialog.setListItemSelectedBackgroundResource(drawable); +//Set a background resource of a list +listDialog.setListBackgroundResource(drawable); + +//Get list item background resource +int ItemBgRes = listDialog.getListItemBgRes(); +//Get list item selected background resource +int ItemBgResSelected = listDialog.getListItemBgResSelected(); + +//set RecyclerView Adapter +listDialog.setAdapter(recyclerViewAdapter); +//Set Layout Manager +listDialog.setLayoutManager(layoutManager); + +//Add items in a list +listDialog.setItems(strings); +listDialog.setItems(strings, listItemClick); +listDialog.setItems(objects, listItemValue); +listDialog.setItems(objects, listItemValue, listItemClickObj); +listDialog.setItems(objects, listItemValues); +listDialog.setItems(objects, listItemValues, listItemClickObj); +listDialog.setItems(arrayList, listItemValue); +listDialog.setItems(arrayList, listItemValue, listItemClickObj); +listDialog.setItems(arrayList, listItemValues); +listDialog.setItems(arrayList, listItemValues, listItemClickObj); +listDialog.setImageItems(listItems); +listDialog.setImageItems(listItems, listItemClickObj); + +//Hide 'List is empty' text +listDialog.hideEmptyListText(); +//Change empty list text +listDialog.setEmptyListText("text"); + +//Set maximum dialog width. Default is 600dp +listDialog.setMaxDialogWidth(width); + +//Get maximum dialog width +int dialogWidth = listDialog.getMaxDialogWidth(); + +//Get left button +Button leftButton = listDialog.getLeftButton(); + +//Get right button +Button rightButton = listDialog.getRightButton(); + +//Set dialog animations +listDialog.setDialogAnimations(styleRes); + +//Enable or disable swipe down to dismiss dialog. +//By default is set to true +dialog.swipeToDismiss(boolean); + +//Set dialog onTouchListener. +//This method will overide swipe down to dismiss action +dialog.setOnTouchListener(onTouchListener); + +//Shew dialog +listDialog.show(); +//Dismiss dialog +listDialog.dismiss(); +``` diff --git a/SJDialog/MessageDialogDoc.md b/SJDialog/MessageDialogDoc.md new file mode 100644 index 0000000..5be6a2a --- /dev/null +++ b/SJDialog/MessageDialogDoc.md @@ -0,0 +1,112 @@ +# MessageDialog Documentation +Message dialog example +```java +MessageDialog messageDialog = new MessageDialog(); +messageDialog.Builder(context) + .setTitle("Title") + .setMessage("Message") + .show(); +``` +![MessageDialog example](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/MessageDialog%20example.png) +## Builder +Apply the default theme to a dialog +```java +messageDialog.Builder(context) +``` +![MessageDialog day-night](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/MessageDialog%20day-night.png) +Apply the app theme to a dialog **(only works with material3 theme)** +```java +messageDialog.Builder(context,true) +``` +Apply the custom theme to a dialog **(only works with material3 theme)** +```java +messageDialog.Builder(context,theme) +``` +## Old Dialog theme +By default dialog colors will be set to material3 dynamic colors. With this method you can set the dialog color for the background and buttons to the older non-dynamic colors +```java +messageDialog.setOldTheme(); +``` +![MessageDialog oldTheme](https://raw.githubusercontent.com/SlaVcE14/SJ-Dialog/master/SJDialog/images/MessageDialog%20oldTheme.png) +## Add onClick Listener +```java +messageDialog.onButtonClick(new DialogButtonEvent() { + @Override + public void onButtonClick() { + // Do something + } +}); + +//or + +messageDialog.onButtonClick(() -> { + // Do something +}); +``` +## All MessageDialog Methods +```java +//Create dialog +messageDialog.Builder(context); + +//Usin the old dialog theme +messageDialog.setOldTheme(); + +//Set title +messageDialog.setTitle("Title"); +//Set message +messageDialog.setMessage("Message"); + +//Set title text alignment +messageDialog.setTitleAlignment(TextAlignment); +//Set message text alignment +messageDialog.setMessageAlignment(TextAlignment); + +//Set button text +messageDialog.setButtonText("Text"); + +//Set text color +messageDialog.setTextColor(color); +//Set title text color +messageDialog.setTitleTextColor(color); +//Set message text color +messageDialog.setMessageTextColor(color); + +//Set button color +messageDialog.setButtonColor(color); + +//Set button text color +messageDialog.setButtonTextColor(color); + +//Set button background resource +messageDialog.setButtonBackgroundResource(drawable); + +//Set dialog color +messageDialog.setDialogBackgroundColor(color); +//Set dialog background resource +messageDialog.setDialogBackgroundResource(drawable); + +//Set maximum dialog width. Default is 600dp +messageDialog.setMaxDialogWidth(width); + +//Get maximum dialog width +int dialogWidth = messageDialog.getMaxDialogWidth(); + +//Get button +Button Button = messageDialog.getButton(); + +//Set dialog animations +messageDialog.setDialogAnimations(styleRes); + +//Enable or disable swipe down to dismiss dialog. +//By default is set to true +dialog.swipeToDismiss(boolean); + +//Set dialog onTouchListener. +//This method will overide swipe down to dismiss action +dialog.setOnTouchListener(onTouchListener); + +//Shew dialog +messageDialog.show(); +//Dismiss dialog +messageDialog.dismiss(); +``` diff --git a/SJDialog/build.gradle b/SJDialog/build.gradle new file mode 100644 index 0000000..b9da45f --- /dev/null +++ b/SJDialog/build.gradle @@ -0,0 +1,39 @@ +plugins { + id 'com.android.library' +} + +android { + compileSdk 31 + + defaultConfig { + minSdk 23 + versionCode 20 + versionName "1.6" + + buildConfigField 'int', 'VERSION_CODE', "${versionCode}" + buildConfigField 'String', 'VERSION_NAME', "\"${versionName}\"" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles "consumer-rules.pro" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_9 + targetCompatibility JavaVersion.VERSION_1_9 + } +} + +dependencies { + + implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'com.google.android.material:material:1.6.1' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} diff --git a/SJDialog/consumer-rules.pro b/SJDialog/consumer-rules.pro new file mode 100644 index 0000000..e69de29 diff --git a/SJDialog/images/BasicDialog day-night.png b/SJDialog/images/BasicDialog day-night.png new file mode 100644 index 0000000..00180e3 Binary files /dev/null and b/SJDialog/images/BasicDialog day-night.png differ diff --git a/SJDialog/images/BasicDialog example 1.png b/SJDialog/images/BasicDialog example 1.png new file mode 100644 index 0000000..2de2b1b Binary files /dev/null and b/SJDialog/images/BasicDialog example 1.png differ diff --git a/SJDialog/images/BasicDialog oldTheme.png b/SJDialog/images/BasicDialog oldTheme.png new file mode 100644 index 0000000..2b75454 Binary files /dev/null and b/SJDialog/images/BasicDialog oldTheme.png differ diff --git a/SJDialog/images/CustomViewDialog day-night.png b/SJDialog/images/CustomViewDialog day-night.png new file mode 100644 index 0000000..0275af4 Binary files /dev/null and b/SJDialog/images/CustomViewDialog day-night.png differ diff --git a/SJDialog/images/CustomViewDialog example 1.png b/SJDialog/images/CustomViewDialog example 1.png new file mode 100644 index 0000000..0be8953 Binary files /dev/null and b/SJDialog/images/CustomViewDialog example 1.png differ diff --git a/SJDialog/images/CustomViewDialog example 2.png b/SJDialog/images/CustomViewDialog example 2.png new file mode 100644 index 0000000..b9bb1a8 Binary files /dev/null and b/SJDialog/images/CustomViewDialog example 2.png differ diff --git a/SJDialog/images/CustomViewDialog example 3.png b/SJDialog/images/CustomViewDialog example 3.png new file mode 100644 index 0000000..2c3140e Binary files /dev/null and b/SJDialog/images/CustomViewDialog example 3.png differ diff --git a/SJDialog/images/CustomViewDialog oldTheme.png b/SJDialog/images/CustomViewDialog oldTheme.png new file mode 100644 index 0000000..431de53 Binary files /dev/null and b/SJDialog/images/CustomViewDialog oldTheme.png differ diff --git a/SJDialog/images/ListDialog day-night.png b/SJDialog/images/ListDialog day-night.png new file mode 100644 index 0000000..4b04f17 Binary files /dev/null and b/SJDialog/images/ListDialog day-night.png differ diff --git a/SJDialog/images/ListDialog example 1.png b/SJDialog/images/ListDialog example 1.png new file mode 100644 index 0000000..14c104a Binary files /dev/null and b/SJDialog/images/ListDialog example 1.png differ diff --git a/SJDialog/images/ListDialog example 2.png b/SJDialog/images/ListDialog example 2.png new file mode 100644 index 0000000..237e20a Binary files /dev/null and b/SJDialog/images/ListDialog example 2.png differ diff --git a/SJDialog/images/ListDialog example 3.png b/SJDialog/images/ListDialog example 3.png new file mode 100644 index 0000000..2acac3d Binary files /dev/null and b/SJDialog/images/ListDialog example 3.png differ diff --git a/SJDialog/images/ListDialog example 4.png b/SJDialog/images/ListDialog example 4.png new file mode 100644 index 0000000..38a6943 Binary files /dev/null and b/SJDialog/images/ListDialog example 4.png differ diff --git a/SJDialog/images/ListDialog oldTheme.png b/SJDialog/images/ListDialog oldTheme.png new file mode 100644 index 0000000..c31707d Binary files /dev/null and b/SJDialog/images/ListDialog oldTheme.png differ diff --git a/SJDialog/images/MessageDialog day-night.png b/SJDialog/images/MessageDialog day-night.png new file mode 100644 index 0000000..5bf0194 Binary files /dev/null and b/SJDialog/images/MessageDialog day-night.png differ diff --git a/SJDialog/images/MessageDialog error_day-night.png b/SJDialog/images/MessageDialog error_day-night.png new file mode 100644 index 0000000..f775ff9 Binary files /dev/null and b/SJDialog/images/MessageDialog error_day-night.png differ diff --git a/SJDialog/images/MessageDialog error_oldTheme.png b/SJDialog/images/MessageDialog error_oldTheme.png new file mode 100644 index 0000000..dccc1d4 Binary files /dev/null and b/SJDialog/images/MessageDialog error_oldTheme.png differ diff --git a/SJDialog/images/MessageDialog example.png b/SJDialog/images/MessageDialog example.png new file mode 100644 index 0000000..76fa94b Binary files /dev/null and b/SJDialog/images/MessageDialog example.png differ diff --git a/SJDialog/images/MessageDialog oldTheme.png b/SJDialog/images/MessageDialog oldTheme.png new file mode 100644 index 0000000..ca1ebd1 Binary files /dev/null and b/SJDialog/images/MessageDialog oldTheme.png differ diff --git a/SJDialog/proguard-rules.pro b/SJDialog/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/SJDialog/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/SJDialog/src/androidTest/java/com/sjapps/library/ExampleInstrumentedTest.java b/SJDialog/src/androidTest/java/com/sjapps/library/ExampleInstrumentedTest.java new file mode 100644 index 0000000..60f6181 --- /dev/null +++ b/SJDialog/src/androidTest/java/com/sjapps/library/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.sjapps.library; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.sjapps.library.test", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/SJDialog/src/main/AndroidManifest.xml b/SJDialog/src/main/AndroidManifest.xml new file mode 100644 index 0000000..c5a6f90 --- /dev/null +++ b/SJDialog/src/main/AndroidManifest.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/BasicDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/BasicDialog.java new file mode 100644 index 0000000..717cac4 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/BasicDialog.java @@ -0,0 +1,437 @@ +package com.sjapps.library.customdialog; + +import android.content.Context; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import androidx.annotation.ColorInt; +import androidx.annotation.DrawableRes; +import androidx.annotation.StyleRes; + +import com.sjapps.library.R; +/**@since 1.6*/ +@SuppressWarnings("unused") +public class BasicDialog extends SJDialog{ + @Deprecated + public static final String LONG_TYPE = "long"; + @Deprecated + public static final String SHORT_TYPE = "short"; + + private boolean isDeleteDialog; + + public BasicDialog(){ + twoButtons = true; + } + /** @since 1.4*/ + public BasicDialog Short(Context context, String Title){ + return Short(context,Title,null); + } + /** @since 1.0*/ + public BasicDialog Short(Context context, String Title, String Btn1Txt, String Btn2Txt){ + return Builder(context,Title,null,Btn1Txt,Btn2Txt); + } + /** @since 1.0*/ + public BasicDialog Short(Context context, String Title, String Btn2Txt){ + return Short(context,Title,null,Btn2Txt); + } + /** @since 1.4*/ + public BasicDialog Long(Context context, String Title, String Message){ + return Long(context,Title,Message,null); + } + /** @since 1.0*/ + public BasicDialog Long(Context context, String Title, String Text, String Btn1Txt, String Btn2Txt){ + return Builder(context,Title,Text,Btn1Txt,Btn2Txt); + } + /** @since 1.0*/ + public BasicDialog Long(Context context, String Title, String Text, String Btn2Txt){ + return Long(context,Title,Text,null,Btn2Txt); + } + /** @since 1.0*/ + public BasicDialog Delete(Context context, String Title){ + return Builder(context,Title,null,null,null).deleteAttributes(); + } + /** @since 1.0*/ + public BasicDialog Delete(Context context){ + return Builder(context).deleteAttributes(); + } + + public BasicDialog Delete(Context context, int theme){ + return Builder(context,theme).deleteAttributes(); + } + + public BasicDialog Delete(Context context, boolean useAppTheme){ + return Builder(context,useAppTheme).deleteAttributes(); + } + + private BasicDialog deleteAttributes(){ + isDeleteDialog = true; + return setTitle("Delete?") + .setRightButtonColor(MATERIAL3_RED_BUTTON) + .setRightButtonText("Delete"); + } + /** + * @deprecated Use {@link BasicDialog#Builder(Context)} + * */ + @Deprecated(since = "1.6") + public BasicDialog DialogBuilder(Context context){ + return Builder(context); + } + /** + * @deprecated Use {@link BasicDialog#Builder(Context, boolean)} + * @since 1.6 + */ + @Deprecated + public BasicDialog DialogBuilder(Context context, boolean useAppTheme){ + return Builder(context, useAppTheme); + } + /** + * @deprecated Use {@link BasicDialog#Builder(Context, int)} + */ + @Deprecated(since = "1.6") + public BasicDialog DialogBuilder(Context context, int theme){ + return Builder(context,theme); + } + + /** + * @deprecated Dialog type is automatic. Use {@link BasicDialog#Builder(Context)} + * @since 1.3 + */ + @Deprecated(since = "1.4") + public BasicDialog DialogBuilder(Context context, String dialogType){ + return Builder(context); + } + /** + * @deprecated Use {@link BasicDialog#Builder(Context, String, String, String, String)} + * @since 1.0 + */ + @Deprecated(since = "1.6") + public BasicDialog DialogBuilder(Context context, String Title, String Text, String Btn1Txt, String Btn2Txt){ + return Builder(context,Title,Text,Btn1Txt,Btn2Txt); + } + public BasicDialog Builder(Context context){ + super.Builder(context,R.layout.basic_dialog); + return this; + } + public BasicDialog Builder(Context context, boolean useAppTheme){ + super.Builder(context,R.layout.basic_dialog,useAppTheme); + return this; + } + public BasicDialog Builder(Context context,@StyleRes int theme){ + super.Builder(context,R.layout.basic_dialog,theme,false); + return this; + } + public BasicDialog Builder(Context context, String Title, String Text, String Btn1Txt, String Btn2Txt){ + super.Builder(context,R.layout.basic_dialog); + TextView TitleTv = dialog.findViewById(R.id.titleText); + if (Btn1Txt == null) { + button1.setOnClickListener(v -> dialog.dismiss()); + }else button1.setText(Btn1Txt); + if (Btn2Txt != null) { + button2.setText(Btn2Txt); + } + if (Text != null){ + setMessage(Text); + } + if (Title == null){ + return this; + } + TitleTv.setText(Title); + return this; + } + /** + * Set a dialog type. + * @param dialogType type of a dialog. Supported types: {@link #SHORT_TYPE} and {@link #LONG_TYPE} + * @return current class + * @deprecated Old method, not in use. Remove this. + * @since 1.4 + * */ + @Deprecated + public BasicDialog setDialogType(String dialogType){ + //Nothing here!! + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public BasicDialog setOldTheme(){ + super.setOldTheme(); + if (isDeleteDialog) + super.setRightButtonColor(RED_BUTTON); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public BasicDialog setTitle(String title){ + super.setTitle(title); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public BasicDialog setMessage(String message){ + super.setMessage(message); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public BasicDialog setTitleAlignment(int textAlignment) { + super.setTitleAlignment(textAlignment); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public BasicDialog setMessageAlignment(int textAlignment) { + super.setMessageAlignment(textAlignment); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public BasicDialog setTextColor(int color) { + super.setTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public BasicDialog setTitleTextColor(int color) { + super.setTitleTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public BasicDialog setMessageTextColor(int color) { + super.setMessageTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setLeftButtonText(String text){ + super.setLeftButtonText(text); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setRightButtonText(String text){ + super.setRightButtonText(text); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setButtonsTextColor(@ColorInt int color){ + super.setButtonsTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setLeftButtonTextColor(@ColorInt int color){ + super.setLeftButtonTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setRightButtonTextColor(@ColorInt int color){ + super.setRightButtonTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setButtonsColor(@ColorInt int color){ + super.setButtonsColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setLeftButtonColor(@ColorInt int color){ + super.setLeftButtonColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setRightButtonColor(@ColorInt int color){ + super.setRightButtonColor(color); + return this; + } + + @Override + public BasicDialog setButtonsColor(@ButtonColor String color){ + super.setButtonsColor(color); + return this; + } + + @Override + public BasicDialog setLeftButtonColor(@ButtonColor String color){ + super.setLeftButtonColor(color); + return this; + } + + @Override + public BasicDialog setRightButtonColor(@ButtonColor String color){ + super.setRightButtonColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public BasicDialog setDialogBackgroundColor(int color) { + super.setDialogBackgroundColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public BasicDialog setDialogBackgroundResource(@DrawableRes int drawable){ + super.setDialogBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public BasicDialog setButtonsBackgroundResource(@DrawableRes int drawable){ + super.setButtonsBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public BasicDialog setLeftButtonBackgroundResource(@DrawableRes int drawable){ + super.setLeftButtonBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public BasicDialog setRightButtonBackgroundResource(@DrawableRes int drawable){ + super.setRightButtonBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.0*/ + @Override + public BasicDialog onButtonClick(DialogButtonEvent dialogButtonEvent){ + super.onButtonClick(dialogButtonEvent); + return this; + } + + /**{@inheritDoc} + * @since 1.0*/ + @Override + public BasicDialog onButtonClick(DialogButtonEvents dialogButtonEvents) { + super.onButtonClick(dialogButtonEvents); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public BasicDialog show() { + super.show(); + return this; + } + + @Override + protected int setButtonsRootLayoutID() { + return R.id.buttons; + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public BasicDialog setMaxDialogWidth(int maxDialogWidth) { + super.setMaxDialogWidth(maxDialogWidth); + return this; + + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public BasicDialog setOnTouchListener(View.OnTouchListener onTouchListener) { + super.setOnTouchListener(onTouchListener); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public BasicDialog swipeToDismiss(boolean isSwipeToDismiss) { + super.swipeToDismiss(isSwipeToDismiss); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public BasicDialog setDialogAnimations(int styleRes) { + super.setDialogAnimations(styleRes); + return this; + } + + @Override + protected void setButtons() { + setButton1(R.id.btn1); + setButton2(R.id.btn2); + } + + @Override + public Button getLeftButton() { + return super.getLeftButton(); + } + + @Override + public Button getRightButton() { + return super.getRightButton(); + } + + /**@since 1.6*/ + @Override + public TextView getTitleTextView() { + return super.getTitleTextView(); + } + + /**@since 1.6*/ + @Override + public TextView getMessageTextView() { + return super.getMessageTextView(); + } +} + diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/CustomViewDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/CustomViewDialog.java new file mode 100644 index 0000000..439d679 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/CustomViewDialog.java @@ -0,0 +1,393 @@ +package com.sjapps.library.customdialog; + +import android.content.Context; +import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.ColorInt; +import androidx.annotation.DrawableRes; +import androidx.annotation.StyleRes; + +import com.sjapps.library.R; +/**@since 1.5*/ +@SuppressWarnings("unused") +public class CustomViewDialog extends SJDialog{ + + private LinearLayout rootView; + + public CustomViewDialog(){ + + } + + public CustomViewDialog Builder(Context context){ + return Builder(context,false); + } + + public CustomViewDialog Builder(Context context,@StyleRes int theme){ + super.Builder(context,R.layout.custom_view_dialog,theme, false); + rootView = dialog.findViewById(R.id.customViewRoot); + onLeftButtonClick(dialog::dismiss); + return this; + } + + public CustomViewDialog Builder(Context context,boolean useAppTheme){ + super.Builder(context,R.layout.custom_view_dialog,useAppTheme); + rootView = dialog.findViewById(R.id.customViewRoot); + onLeftButtonClick(dialog::dismiss); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setOldTheme(){ + super.setOldTheme(); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setTitle(String title){ + super.setTitle(title); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setMessage(String message) { + super.setMessage(message); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public CustomViewDialog setTitleAlignment(int textAlignment) { + super.setTitleAlignment(textAlignment); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public CustomViewDialog setMessageAlignment(int textAlignment) { + super.setMessageAlignment(textAlignment); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setTextColor(int color) { + super.setTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public CustomViewDialog setTitleTextColor(int color) { + super.setTitleTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public CustomViewDialog setMessageTextColor(int color) { + super.setMessageTextColor(color); + return this; + } + + /** + * Set a button text. + * @param text button text + * @return current class + * @since 1.5 + * */ + public CustomViewDialog setButtonText(String text){ + return setLeftButtonText(text); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setLeftButtonText(String text){ + super.setLeftButtonText(text); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setRightButtonText(String text){ + super.setRightButtonText(text); + return this; + } + + /** + * Set button text color. + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.5 + * */ + public CustomViewDialog setButtonTextColor(@ColorInt int color){ + return setLeftButtonTextColor(color); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setButtonsTextColor(@ColorInt int color){ + super.setButtonsTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setLeftButtonTextColor(@ColorInt int color){ + super.setLeftButtonTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setRightButtonTextColor(@ColorInt int color){ + super.setRightButtonTextColor(color); + return this; + } + + /** + * Set button color. + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.5 + * */ + public CustomViewDialog setButtonColor(@ColorInt int color){ + return setLeftButtonColor(color); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setButtonsColor(@ColorInt int color){ + super.setButtonsColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setLeftButtonColor(@ColorInt int color){ + super.setLeftButtonColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setRightButtonColor(@ColorInt int color){ + super.setRightButtonColor(color); + return this; + } + + @Override + public CustomViewDialog setButtonColor(@ButtonColor String color) { + super.setButtonColor(color); + return this; + } + + @Override + public CustomViewDialog setButtonsColor(@ButtonColor String color){ + super.setButtonsColor(color); + return this; + } + + @Override + public CustomViewDialog setLeftButtonColor(@ButtonColor String color){ + super.setLeftButtonColor(color); + return this; + } + + @Override + public CustomViewDialog setRightButtonColor(@ButtonColor String color){ + super.setRightButtonColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public CustomViewDialog setDialogBackgroundColor(int color) { + super.setDialogBackgroundColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setDialogBackgroundResource(@DrawableRes int drawable){ + super.setDialogBackgroundResource(drawable); + return this; + } + + /** + * Set background resource for button. + * @param drawable resource id + * @return current class + * @since 1.5 + * */ + public CustomViewDialog setButtonBackgroundResource(@DrawableRes int drawable){ + return setLeftButtonBackgroundResource(drawable); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setButtonsBackgroundResource(@DrawableRes int drawable){ + super.setButtonsBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setLeftButtonBackgroundResource(@DrawableRes int drawable){ + super.setLeftButtonBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setRightButtonBackgroundResource(@DrawableRes int drawable){ + super.setRightButtonBackgroundResource(drawable); + return this; + } + + /** + * Set onClick listener for a button + * + * @param dialogButtonEvent dialog button event + * @return current class + * @since 1.5 + */ + @Override + public CustomViewDialog onButtonClick(DialogButtonEvent dialogButtonEvent){ + if (twoButtons) + super.onButtonClick(dialogButtonEvent); + else + super.onLeftButtonClick(dialogButtonEvent); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog onButtonClick(DialogButtonEvents dialogButtonEvents) { + super.onButtonClick(dialogButtonEvents); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog show() { + super.show(); + return this; + } + + @Override + protected int setButtonsRootLayoutID() { + return R.id.buttons; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setMaxDialogWidth(int maxDialogWidth) { + super.setMaxDialogWidth(maxDialogWidth); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog setDialogAnimations(int styleRes) { + super.setDialogAnimations(styleRes); + return this; + } + + @Override + protected void setButtons() { + setButton1(R.id.btn1); + setButton2(R.id.btn2); + } + + @Override + public Button getLeftButton() { + return super.getLeftButton(); + } + + @Override + public Button getRightButton() { + return super.getRightButton(); + } + + /**@since 1.6*/ + @Override + public TextView getTitleTextView() { + return super.getTitleTextView(); + } + + /**@since 1.6*/ + @Override + public TextView getMessageTextView() { + return super.getMessageTextView(); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public CustomViewDialog dialogWithTwoButtons() { + super.dialogWithTwoButtons(); + setButton2Visibility(View.VISIBLE); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public CustomViewDialog setOnTouchListener(View.OnTouchListener onTouchListener) { + super.setOnTouchListener(onTouchListener); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public CustomViewDialog swipeToDismiss(boolean isSwipeToDismiss) { + super.swipeToDismiss(isSwipeToDismiss); + return this; + } + + @Override + public void setButton2Visibility(int visibility) { + super.setButton2Visibility(visibility); + } + + public CustomViewDialog addCustomView(View view){ + rootView.addView(view); + return this; + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/DialogButtonEvent.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/DialogButtonEvent.java new file mode 100644 index 0000000..484bf56 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/DialogButtonEvent.java @@ -0,0 +1,5 @@ +package com.sjapps.library.customdialog; + +public interface DialogButtonEvent { + void onButtonClick(); +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/DialogButtonEvents.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/DialogButtonEvents.java new file mode 100644 index 0000000..b423d7e --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/DialogButtonEvents.java @@ -0,0 +1,7 @@ +package com.sjapps.library.customdialog; + +public interface DialogButtonEvents { + void onLeftButtonClick(); + void onRightButtonClick(); +} + diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/ImageListItem.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/ImageListItem.java new file mode 100644 index 0000000..4066546 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/ImageListItem.java @@ -0,0 +1,57 @@ +package com.sjapps.library.customdialog; + +import android.graphics.drawable.Drawable; + +public class ImageListItem { + + private String name; + private Drawable image; + private Object data; + + public ImageListItem() { + } + + public ImageListItem(String name, Drawable image) { + this.name = name; + this.image = image; + } + + public ImageListItem(String name, Drawable image, Object data) { + this.name = name; + this.image = image; + this.data = data; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Drawable getImage() { + return image; + } + + public void setImage(Drawable image) { + this.image = image; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + @Override + public String toString() { + return "ImageListItem{" + + "name='" + name + '\'' + + ", image=" + image + + ", data=" + data + + '}'; + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java new file mode 100644 index 0000000..0be8d42 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java @@ -0,0 +1,931 @@ +package com.sjapps.library.customdialog; + +import android.content.Context; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import androidx.annotation.ColorInt; +import androidx.annotation.DrawableRes; +import androidx.annotation.Nullable; +import androidx.annotation.StyleRes; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.sjapps.library.R; +import com.sjapps.library.customdialog.adapter.DefaultImageListAdapter; +import com.sjapps.library.customdialog.adapter.DefaultListAdapter; +import com.sjapps.library.customdialog.adapter.DefaultListAdapterGeneric; +import com.sjapps.library.customdialog.list.events.ListItemClick; +import com.sjapps.library.customdialog.list.events.ListItemClickObj; + +import java.util.ArrayList; +/**@since 1.5*/ +@SuppressWarnings({"unused", "unchecked","UnusedReturnValue"}) +public class ListDialog extends SJDialog { + + RecyclerView listRV; + boolean isSelectableList = false; + boolean hideEmptyListTxt = false; + boolean hasAdapter = false; + boolean waitForLayoutManager = false; + + private @DrawableRes int listItemBgRes = R.drawable.ripple_list; + private @DrawableRes int listItemBgResSelected = R.drawable.ripple_list_selected; + private @ColorInt int listItemBgColor = -1; + private @ColorInt int listItemBgColorSelected = -1; + private int listItemTextColor = 1; + private RecyclerView.LayoutManager layoutManager = null; + + RecyclerView.Adapter adapter; + ArrayList selectedItems = new ArrayList<>(); + TextView emptyListTxt; + + public ListDialog() { + + } + + public ListDialog Builder(Context context) { + return Builder(context, false); + } + + public ListDialog Builder(Context context, @StyleRes int theme) { + super.Builder(context, R.layout.list_dialog, theme, false); + listRV = dialog.findViewById(R.id.list); + onLeftButtonClick(dialog::dismiss); + emptyListTxt = dialog.findViewById(R.id.emptyListTxt); + return this; + } + + public ListDialog Builder(Context context, boolean useAppTheme) { + super.Builder(context, R.layout.list_dialog, useAppTheme); + listRV = dialog.findViewById(R.id.list); + onLeftButtonClick(dialog::dismiss); + emptyListTxt = dialog.findViewById(R.id.emptyListTxt); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setOldTheme() { + super.setOldTheme(); + setListOldColor(); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setTitle(String title) { + super.setTitle(title); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setMessage(String message) { + super.setMessage(message); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public ListDialog setTitleAlignment(int textAlignment) { + super.setTitleAlignment(textAlignment); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public ListDialog setMessageAlignment(int textAlignment) { + super.setMessageAlignment(textAlignment); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public ListDialog setTextColor(int color) { + super.setTextColor(color); + setListItemTextColor(color); + setEmptyListTxtColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public ListDialog setTitleTextColor(int color) { + super.setTitleTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public ListDialog setMessageTextColor(int color) { + super.setMessageTextColor(color); + return this; + } + + /** + * Set a button text. + * + * @param text button text + * @return current class + * @since 1.5 + */ + public ListDialog setButtonText(String text) { + return setLeftButtonText(text); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setLeftButtonText(String text) { + super.setLeftButtonText(text); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setRightButtonText(String text) { + super.setRightButtonText(text); + return this; + } + + /** + * Set button text color. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.5 + */ + public ListDialog setButtonTextColor(@ColorInt int color) { + return setLeftButtonTextColor(color); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setButtonsTextColor(@ColorInt int color) { + super.setButtonsTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setLeftButtonTextColor(@ColorInt int color) { + super.setLeftButtonTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setRightButtonTextColor(@ColorInt int color) { + super.setRightButtonTextColor(color); + return this; + } + + /** + * Set button color. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.5 + */ + public ListDialog setButtonColor(@ColorInt int color) { + return setLeftButtonColor(color); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setButtonsColor(@ColorInt int color) { + super.setButtonsColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setLeftButtonColor(@ColorInt int color) { + super.setLeftButtonColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setRightButtonColor(@ColorInt int color) { + super.setRightButtonColor(color); + return this; + } + + @Override + public ListDialog setButtonColor(@ButtonColor String color) { + super.setButtonColor(color); + return this; + } + + @Override + public ListDialog setButtonsColor(@ButtonColor String color) { + super.setButtonsColor(color); + return this; + } + + @Override + public ListDialog setLeftButtonColor(@ButtonColor String color) { + super.setLeftButtonColor(color); + return this; + } + + @Override + public ListDialog setRightButtonColor(@ButtonColor String color) { + super.setRightButtonColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public ListDialog setDialogBackgroundColor(int color) { + super.setDialogBackgroundColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setDialogBackgroundResource(@DrawableRes int drawable) { + super.setDialogBackgroundResource(drawable); + return this; + } + + /** + * Set background resource for button. + * + * @param drawable resource id + * @return current class + * @since 1.5 + */ + public ListDialog setButtonBackgroundResource(@DrawableRes int drawable) { + return setLeftButtonBackgroundResource(drawable); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setButtonsBackgroundResource(@DrawableRes int drawable) { + super.setButtonsBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setLeftButtonBackgroundResource(@DrawableRes int drawable) { + super.setLeftButtonBackgroundResource(drawable); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setRightButtonBackgroundResource(@DrawableRes int drawable) { + super.setRightButtonBackgroundResource(drawable); + return this; + } + + /** + * Set onClick listener for a button + * + * @param dialogButtonEvent dialog button event + * @return current class + * @since 1.5 + */ + @Override + public ListDialog onButtonClick(DialogButtonEvent dialogButtonEvent) { + if (twoButtons) + super.onButtonClick(dialogButtonEvent); + else + super.onLeftButtonClick(dialogButtonEvent); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog onButtonClick(DialogButtonEvents dialogButtonEvents) { + super.onButtonClick(dialogButtonEvents); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog show() { + if (layoutManager == null) + layoutManager = new LinearLayoutManager(context); + if (!waitForLayoutManager) + listRV.setLayoutManager(layoutManager); + + listRV.setAdapter(adapter); + if (adapter == null) + checkListsSize(0); + + super.show(); + + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setMaxDialogWidth(int maxDialogWidth) { + super.setMaxDialogWidth(maxDialogWidth); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog setDialogAnimations(int styleRes) { + super.setDialogAnimations(styleRes); + return this; + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public ListDialog dialogWithTwoButtons() { + super.dialogWithTwoButtons(); + setButton2Visibility(View.VISIBLE); + return this; + } + + @Override + public void setButton2Visibility(int visibility) { + super.setButton2Visibility(visibility); + } + + private void setListOldColor() { + listItemBgRes = R.drawable.ripple_list_old; + listItemBgResSelected = R.drawable.ripple_list_selected_old; + } + + /** + * Set background resource for a list + * + * @param drawable drawable resource + * @return current class + * @since 1.5 + */ + public ListDialog setListBackgroundResource(@DrawableRes int drawable) { + listRV.setBackgroundResource(drawable); + return this; + } + + /** + * Set background resource for a item in a list + * + * @param drawable drawable resource + * @return current class + * @since 1.5 + */ + public ListDialog setListItemBackgroundResource(@DrawableRes int drawable) { + listItemBgRes = drawable; + return this; + } + + /** + * Set background resource for a selected item in a list + * + * @param drawable drawable resource + * @return current class + * @since 1.5 + */ + public ListDialog setListItemSelectedBackgroundResource(@DrawableRes int drawable) { + listItemBgResSelected = drawable; + return this; + } + + /** + * Set background color for a item in a list + * + * @param color {@link ColorInt} + * @return current class + * @since 1.6 + */ + public ListDialog setListItemBackgroundColor(@ColorInt int color) { + listItemBgColor = color; + return this; + } + + /** + * Set background color for a selected item in a list + * + * @param color {@link ColorInt} + * @return current class + * @since 1.6 + */ + public ListDialog setListItemSelectedBackgroundColor(@ColorInt int color) { + listItemBgColorSelected = color; + return this; + } + + /** + * Set text color for item in a list + * + * @param listItemTextColor color for a text + * @return current class + * @since 1.6 + */ + public ListDialog setListItemTextColor(int listItemTextColor) { + this.listItemTextColor = listItemTextColor; + return this; + } + + private void setEmptyListTxtColor(int color) { + emptyListTxt.setTextColor(color); + } + + /** + * Hide {@link #emptyListTxt} when list is empty + * @since 1.6 + * @return current class + */ + public ListDialog hideEmptyListText() { + hideEmptyListTxt = true; + return this; + } + + /** + * Set text to display when list is empty + * @param text Text + * @since 1.6 + * @return current class + */ + public ListDialog setEmptyListText(String text){ + emptyListTxt.setText(text); + return this; + } + + /** + * Get a dialog list + * + * @return dialog list + * @since 1.5 + */ + public RecyclerView getRecycleView() { + return listRV; + } + + @Override + protected int setButtonsRootLayoutID() { + return R.id.buttons; + } + + @Override + protected void setButtons() { + setButton1(R.id.btn1); + setButton2(R.id.btn2); + } + + @Override + public Button getLeftButton() { + return super.getLeftButton(); + } + + @Override + public Button getRightButton() { + return super.getRightButton(); + } + + /**@since 1.6*/ + @Override + public TextView getTitleTextView() { + return super.getTitleTextView(); + } + + /**@since 1.6*/ + @Override + public TextView getMessageTextView() { + return super.getMessageTextView(); + } + + /** + * Set items for a list + * + * @param listOfItems Array of {@link String} + * @return current class + * @since 1.5 + */ + public ListDialog setItems(String[] listOfItems) { + return setItems(listOfItems, (ListItemClick) null); + } + + + /** + * Set items for a list and ListItemClick event + * + * @param listOfItems Array of {@link String} + * @param itemClick ListItemClick event + * @return current class + * @since 1.5 + */ + public ListDialog setItems(String[] listOfItems, @Nullable ListItemClick itemClick) { + + if (hasAdapter) + throw tooManyAdapters; + + if (!isSelectableList && itemClick == null) + throw nullListItemClick(ListItemClick.class); + + checkListsSize(listOfItems.length); + + adapter = new DefaultListAdapter(listOfItems, + isSelectableList, + itemClick, + (ArrayList) selectedItems, + listItemBgRes, + listItemBgResSelected, + listItemTextColor, + listItemBgColor, + listItemBgColorSelected); + hasAdapter = true; + return this; + } + + /** + * Set items for a list and String value of an object for use in a list + * + * @param objArray Array of {@link Object} + * @param value {@link ListItemValue} for getting String value of an object + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(T[] objArray, ListItemValue value) { + return setItems(objArray, value, null); + } + + /** + * Set items for a list and String values of an object for use in a list + * + * @param objArray Array of {@link Object} + * @param values {@link ListItemValues} for getting String values of an object. {@link ListItemValues#getValue1(Object)} + * for first value and {@link ListItemValues#getValue1(Object)} for second value + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(T[] objArray, ListItemValues values) { + return setItems(objArray, values, null); + } + + + /** + * Set items for a list and String value of an object for use in a list + * + * @param arrayList {@link ArrayList} of {@link Object} + * @param value {@link ListItemValue} for getting String value of an object + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(ArrayList arrayList, ListItemValue value) { + return setItems(arrayList, value, null); + } + + + /** + * Set items for a list and String values of an object for use in a list + * + * @param arrayList {@link ArrayList} of {@link Object} + * @param values {@link ListItemValues} for getting String values of an object. {@link ListItemValues#getValue1(Object)} + * for first value and {@link ListItemValues#getValue1(Object)} for second value + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(ArrayList arrayList, ListItemValues values) { + return setItems(arrayList, values, null); + } + + + /** + * Set items for a list, String value of an object for use in a list and ListItemClick event + * + * @param objArray Array of {@link Object} + * @param value {@link ListItemValue} for getting String value of an object + * @param itemClick ListItemClick event + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(T[] objArray, ListItemValue value, @Nullable ListItemClickObj itemClick) { + + if (hasAdapter) + throw tooManyAdapters; + + if (!isSelectableList && itemClick == null) + throw nullListItemClick(ListItemClickObj.class); + + checkListsSize(objArray.length); + + adapter = new DefaultListAdapterGeneric<>(objArray, + value, + isSelectableList, + itemClick, + (ArrayList) selectedItems, + listItemBgRes, + listItemBgResSelected, + listItemTextColor, + listItemBgColor, + listItemBgColorSelected); + hasAdapter = true; + return this; + } + + /** + * Set items for a list, String values of an object for use in a list and ListItemClick event + * + * @param objArray Array of {@link Object} + * @param values {@link ListItemValues} for getting String values of an object. {@link ListItemValues#getValue1(Object)} + * for first value and {@link ListItemValues#getValue1(Object)} for second value + * @param itemClick ListItemClick event + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(T[] objArray, ListItemValues values, @Nullable ListItemClickObj itemClick) { + + if (hasAdapter) + throw tooManyAdapters; + + if (!isSelectableList && itemClick == null) + throw nullListItemClick(ListItemClickObj.class); + + checkListsSize(objArray.length); + + adapter = new DefaultListAdapterGeneric<>(objArray, + values, + isSelectableList, + itemClick, + (ArrayList) selectedItems, + listItemBgRes, + listItemBgResSelected, + listItemTextColor, + listItemBgColor, + listItemBgColorSelected); + hasAdapter = true; + return this; + + } + + + /** + * Set items for a list, String value of an object for use in a list and ListItemClick event + * + * @param arrayList {@link ArrayList} of {@link Object} + * @param value {@link ListItemValue} for getting String value of an object + * @param itemClick ListItemClick event + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(ArrayList arrayList, ListItemValue value, @Nullable ListItemClickObj itemClick) { + + if (hasAdapter) + throw tooManyAdapters; + + if (!isSelectableList && itemClick == null) + throw nullListItemClick(ListItemClickObj.class); + + checkListsSize(arrayList.size()); + + adapter = new DefaultListAdapterGeneric<>(arrayList, + value, + isSelectableList, + itemClick, + (ArrayList) selectedItems, + listItemBgRes, + listItemBgResSelected, + listItemTextColor, + listItemBgColor, + listItemBgColorSelected); + hasAdapter = true; + return this; + } + + /** + * Set items for a list, String values of an object for use in a list and ListItemClick event + * + * @param arrayList {@link ArrayList} of {@link Object} + * @param values {@link ListItemValues} for getting String values of an object. {@link ListItemValues#getValue1(Object)} + * for first value and {@link ListItemValues#getValue1(Object)} for second value + * @param itemClick ListItemClick event + * @param Type of an Object + * @return current class + * @since 1.5 + */ + public ListDialog setItems(ArrayList arrayList, ListItemValues values, @Nullable ListItemClickObj itemClick) { + + if (hasAdapter) + throw tooManyAdapters; + + if (!isSelectableList && itemClick == null) + throw nullListItemClick(ListItemClickObj.class); + + checkListsSize(arrayList.size()); + + adapter = new DefaultListAdapterGeneric<>(arrayList, + values, + isSelectableList, + itemClick, + (ArrayList) selectedItems, + listItemBgRes, + listItemBgResSelected, + listItemTextColor, + listItemBgColor, + listItemBgColorSelected); + hasAdapter = true; + return this; + } + + /** + * Set image items for a list + * @param arrayList {@link ArrayList} of {@link ImageListItem} + * @return current class + * @since 1.6 + */ + public ListDialog setImageItems(ArrayList arrayList) { + return setImageItems(arrayList, null); + } + + /** + * Set image items for a list and ListItemClick event + * @param arrayList {@link ArrayList} of {@link ImageListItem} + * @param itemClick ListItemClick event + * @return current class + * @since 1.6 + */ + public ListDialog setImageItems(ArrayList arrayList, @Nullable ListItemClickObj itemClick) { + + if (hasAdapter) + throw tooManyAdapters; + + if (!isSelectableList && itemClick == null) + throw nullListItemClick(ListItemClickObj.class); + + checkListsSize(arrayList.size()); + + adapter = new DefaultImageListAdapter(arrayList, + isSelectableList, + itemClick, + (ArrayList) selectedItems, + listItemBgRes, + listItemBgResSelected, + listItemTextColor, + listItemBgColor, + listItemBgColorSelected); + + hasAdapter = true; + waitForLayoutManager = true; + listRV.post(() -> { + int maxItemCount = listRV.getWidth() / functions.dpToPixels(context,80); + if (maxItemCount == 0) + maxItemCount = 1; + setLayoutManager(new GridLayoutManager(context, arrayList.size()!=0 ? Math.min(maxItemCount, arrayList.size()):1)); + listRV.setLayoutManager(layoutManager); + waitForLayoutManager = false; + }); + + return this; + } + + /** + * Set a list adapter + * + * @param adapter RecycleView {@link RecyclerView.Adapter adapter} + * @return current class + * @since 1.5 + */ + public ListDialog setAdapter(RecyclerView.Adapter adapter) { + if (hasAdapter) + throw tooManyAdapters; + this.adapter = adapter; + hasAdapter = true; + return this; + } + + /** + * @param layoutManager {@link RecyclerView.LayoutManager LayoutManager } + * @return current class + * @since 1.6 + */ + public ListDialog setLayoutManager(RecyclerView.LayoutManager layoutManager) { + this.layoutManager = layoutManager; + return this; + } + + /** + * Get a list adapter + * + * @return current class + * @since 1.5 + */ + public RecyclerView.Adapter getListAdapter() { + return adapter; + } + + + /** + * @return Background resource for views in RecycleView + * @since 1.5 + */ + public int getListItemBgRes() { + return listItemBgRes; + } + + /** + * @return Background resource for selected views in RecycleView + * @since 1.5 + */ + public int getListItemBgResSelected() { + return listItemBgResSelected; + } + + /** + * Create list with multi selectable items. Use {@link #getSelectedItems()} fet getting selected items + * + * @return current class + * @since 1.5 + */ + public ListDialog setSelectableList() { + isSelectableList = true; + return this; + } + + public boolean isSelectableList() { + return isSelectableList; + } + + @Override + public ListDialog setOnTouchListener(View.OnTouchListener onTouchListener) { + super.setOnTouchListener(onTouchListener); + return this; + } + + @Override + public ListDialog swipeToDismiss(boolean isSwipeToDismiss) { + super.swipeToDismiss(isSwipeToDismiss); + return this; + } + + /** + * Get selected items in a list + * + * @return ArrayList of selected items in a list + * @since 1.5 + */ + public ArrayList getSelectedItems() { + return (ArrayList) selectedItems; + } + + void checkListsSize(int size) { + + if (hideEmptyListTxt) { + emptyListTxt.setVisibility(View.GONE); + return; + } + emptyListTxt.setVisibility((size > 0) ? View.GONE : View.VISIBLE); + + + } + + @SuppressWarnings("rawtypes") + private NullPointerException nullListItemClick(Class className) { + return new NullPointerException( + String.format("%s is null. Set %s event or use setSelectableList() for selecting multiple item in a list", + className.getSimpleName(), + className.getSimpleName()) + ); + + } + + private final UnsupportedOperationException tooManyAdapters = new UnsupportedOperationException("Too many Adapters for RecyclerView"); +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemImageValue.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemImageValue.java new file mode 100644 index 0000000..79e121f --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemImageValue.java @@ -0,0 +1,19 @@ +package com.sjapps.library.customdialog; + +import android.graphics.drawable.Drawable; + +public interface ListItemImageValue { + /** + * Get String value of an Object for using in a list + * @param obj Current Object for getting first String value of it + * @return String value + */ + String getTitle(T obj); + + /** + * Get Drawable value of an Object for use in a list item + * @param obj Current Object for getting second String value of it + * @return Drawable + */ + Drawable getImage(T obj); +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemValue.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemValue.java new file mode 100644 index 0000000..5d437ef --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemValue.java @@ -0,0 +1,10 @@ +package com.sjapps.library.customdialog; + +public interface ListItemValue { + /** + * Get String value of an Object for using in a list + * @param obj Current Object for getting String value of it + * @return String value + */ + String getValue(T obj); +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemValues.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemValues.java new file mode 100644 index 0000000..74780bf --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListItemValues.java @@ -0,0 +1,17 @@ +package com.sjapps.library.customdialog; + +public interface ListItemValues { + /** + * Get String value of an Object for use in the first value of a list item + * @param obj Current Object for getting first String value of it + * @return String value + */ + String getValue1(T obj); + + /** + * Get String value of an Object for use in the second value of a list item + * @param obj Current Object for getting second String value of it + * @return String value + */ + String getValue2(T obj); +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/MessageDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/MessageDialog.java new file mode 100644 index 0000000..8dc7c2f --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/MessageDialog.java @@ -0,0 +1,270 @@ +package com.sjapps.library.customdialog; + +import android.content.Context; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import androidx.annotation.ColorInt; +import androidx.annotation.DrawableRes; +import androidx.annotation.StyleRes; + +import com.sjapps.library.R; +/**@since 1.3*/ +@SuppressWarnings("unused") +public class MessageDialog extends SJDialog{ + + private boolean isErrorDialog; + + public MessageDialog(){ + onlyOneButton = true; + } + + public MessageDialog Builder(Context context){ + super.Builder(context,R.layout.message_dialog); + onButtonClick(() -> dialog.dismiss()); + return this; + } + public MessageDialog Builder(Context context,@StyleRes int theme){ + super.Builder(context,R.layout.message_dialog,theme, false); + onButtonClick(() -> dialog.dismiss()); + return this; + } + public MessageDialog Builder(Context context,boolean useAppTheme){ + super.Builder(context,R.layout.message_dialog,useAppTheme); + onButtonClick(() -> dialog.dismiss()); + return this; + } + public MessageDialog ErrorDialogBuilder(Context context){ + return Builder(context).errorDialogAttributes(); + } + public MessageDialog ErrorDialogBuilder(Context context,@StyleRes int theme){ + return Builder(context,theme).errorDialogAttributes(); + } + public MessageDialog ErrorDialogBuilder(Context context,boolean useAppTheme){ + return Builder(context,useAppTheme).errorDialogAttributes(); + } + + private MessageDialog errorDialogAttributes(){ + isErrorDialog = true; + return setDialogBackgroundResource(R.drawable.dialog_background_material3_red) + .setTextColor(context.getResources().getColor(R.color.SJDialog_ErrorTextColor, context.getTheme())) + .setButtonColor(MATERIAL3_RED_BUTTON) + .setTitle("Error"); + } + + /**{@inheritDoc} + * @since 1.5*/ + @Override + public MessageDialog setOldTheme(){ + super.setOldTheme(); + if (isErrorDialog) { + setDialogBackgroundResource(R.drawable.dialog_background_red); + setTextColor(defaultOldColorWhite); + setButtonColor(defaultOldColorWhite); + setButtonTextColor(defaultOldColorBlack); + } + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public MessageDialog setTitle(String title){ + super.setTitle(title); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public MessageDialog setMessage(String message) { + super.setMessage(message); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public MessageDialog setTitleAlignment(int textAlignment) { + super.setTitleAlignment(textAlignment); + return this; + } + + /** + * {@inheritDoc} + * @since 1.6 + * */ + @Override + public MessageDialog setMessageAlignment(int textAlignment) { + super.setMessageAlignment(textAlignment); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public MessageDialog setTextColor(int color) { + super.setTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public MessageDialog setTitleTextColor(int color) { + super.setTitleTextColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public MessageDialog setMessageTextColor(int color) { + super.setMessageTextColor(color); + return this; + } + + /** + * Set a button text. + * @param text button text + * @return current class + * @since 1.4 + * */ + public MessageDialog setButtonText(String text){ + super.setLeftButtonText(text); + return this; + } + + /** + * Set button text color. + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.4 + * */ + public MessageDialog setButtonTextColor(@ColorInt int color){ + super.setLeftButtonTextColor(color); + return this; + } + + /** + * Set button color. + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.4 + * */ + public MessageDialog setButtonColor(@ColorInt int color){ + super.setLeftButtonColor(color); + return this; + } + + @Override + public MessageDialog setButtonColor(@ButtonColor String color){ + super.setButtonColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public MessageDialog setDialogBackgroundColor(int color) { + super.setDialogBackgroundColor(color); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public MessageDialog setDialogBackgroundResource(@DrawableRes int drawable){ + super.setDialogBackgroundResource(drawable); + return this; + } + + /** + * Set background resource for button. + * @param drawable resource id + * @return current class + * @since 1.3 + * */ + public MessageDialog setButtonBackgroundResource(@DrawableRes int drawable){ + super.setLeftButtonBackgroundResource(drawable); + return this; + } + + /** + * Set onClick listener for a button + * @param dialogButtonEvent dialog button events + * @return current class + * */ + @Override + public MessageDialog onButtonClick(DialogButtonEvent dialogButtonEvent){ + super.onLeftButtonClick(dialogButtonEvent); + return this; + } + + @Override + protected void setButtons() { + setButton1(R.id.btn); + } + + public Button getButton() { + return super.getLeftButton(); + } + + /**@since 1.6*/ + @Override + public TextView getTitleTextView() { + return super.getTitleTextView(); + } + + /**@since 1.6*/ + @Override + public TextView getMessageTextView() { + return super.getMessageTextView(); + } + + /**{@inheritDoc} + * @since 1.4*/ + @Override + public MessageDialog setMaxDialogWidth(int maxDialogWidth){ + super.setMaxDialogWidth(maxDialogWidth); + return this; + } + + @Override + public MessageDialog setDialogAnimations(int styleRes) { + super.setDialogAnimations(styleRes); + return this; + } + + /**{@inheritDoc} + * @since 1.3*/ + @Override + public MessageDialog show(){ + super.show(); + return this; + } + + @Override + protected int setButtonsRootLayoutID() { + return R.id.buttonRoot; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public MessageDialog setOnTouchListener(View.OnTouchListener onTouchListener) { + super.setOnTouchListener(onTouchListener); + return this; + } + + /**{@inheritDoc} + * @since 1.6*/ + @Override + public MessageDialog swipeToDismiss(boolean isSwipeToDismiss) { + super.swipeToDismiss(isSwipeToDismiss); + return this; + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/SJDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/SJDialog.java new file mode 100644 index 0000000..947e33c --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/SJDialog.java @@ -0,0 +1,744 @@ +package com.sjapps.library.customdialog; + +import android.annotation.SuppressLint; +import android.app.Dialog; +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.view.ContextThemeWrapper; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; + +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.ColorInt; +import androidx.annotation.DrawableRes; +import androidx.annotation.IdRes; +import androidx.annotation.IntDef; +import androidx.annotation.LayoutRes; +import androidx.annotation.StringDef; +import androidx.annotation.StyleRes; + +import com.sjapps.library.R; + +@SuppressWarnings({"unused", "UnusedReturnValue"}) +public abstract class SJDialog { + + public static final String RED_BUTTON = "RedBtn"; + public static final String MATERIAL3_RED_BUTTON = "Material3RedBtn"; + public static final String OLD_BUTTON_COLOR = "OldBtnColor"; + /** Sets text alignment to {@link View#TEXT_ALIGNMENT_VIEW_START} + * @since 1.6 + * */ + public static final int TEXT_ALIGNMENT_LEFT = View.TEXT_ALIGNMENT_VIEW_START; + /** Sets text alignment to {@link View#TEXT_ALIGNMENT_VIEW_END} + * @since 1.6 + * */ + public static final int TEXT_ALIGNMENT_RIGHT = View.TEXT_ALIGNMENT_VIEW_END; + /** Sets text alignment to {@link View#TEXT_ALIGNMENT_CENTER} + * @since 1.6 + * */ + public static final int TEXT_ALIGNMENT_CENTER = View.TEXT_ALIGNMENT_CENTER; + + private @LayoutRes int Btn1Resource = R.layout.button_template; + private @LayoutRes int Btn2Resource = R.layout.button_template; + + @ColorInt int defaultOldThemeTextColor; + @ColorInt int defaultOldColorWhite = 0xFFE5E5E5; + @ColorInt int defaultOldColorBlack = 0xFF333333; + + private final int defaultTheme = R.style.Theme_SJDialog; + private int newTheme = -1; + private boolean usesDefaultTheme = true; + private boolean isSwipeToDismiss = true; + private boolean isDefaultOnTouchListener = true; + private View.OnTouchListener dialogOnTouchListener = null; + + public Dialog dialog; + protected Button button1, button2; + private LinearLayout background; + private int maxDialogWidth = 600; + Context context; + + DialogButtonEvent dialogButtonEvent; + DialogButtonEvents dialogButtonEvents; + + @StringDef({RED_BUTTON, MATERIAL3_RED_BUTTON, OLD_BUTTON_COLOR}) + public @interface ButtonColor { + } + + @IntDef({TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER}) + public @interface TextAlignment { + } + + protected boolean onlyOneButton = false; + protected boolean twoButtons = false; + private boolean leftBtnOnClick = false; + + protected SJDialog Builder(Context context, @LayoutRes int layoutResID) { + Builder(context, layoutResID, defaultTheme, false); + return this; + } + + protected SJDialog Builder(Context context, @LayoutRes int layoutResID, boolean useAppTheme) { + Builder(context, layoutResID, defaultTheme, useAppTheme); + return this; + } + + protected SJDialog Builder(Context context, @LayoutRes int layoutResID, @StyleRes int theme, boolean useAppTheme) { + this.context = context; + defaultOldThemeTextColor = context.getResources().getColor(R.color.SJDialog_OldThemeTextColor, context.getTheme()); + dialog = useAppTheme ? + new Dialog(new ContextThemeWrapper(context, context.getTheme())) : + new Dialog(new ContextThemeWrapper(context, theme)); + setContentView(layoutResID); + setDialogSize(); + dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM; + dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + dialog.getWindow().getAttributes().windowAnimations = R.style.SJDialogAnimation; + background = dialog.findViewById(R.id.dialogBackground); + setButtons(); + if (theme != defaultTheme || useAppTheme) { + usesDefaultTheme = false; + newTheme = !useAppTheme ? theme : -1; + regenerateButtons(); + } + return this; + } + + protected void setDialogSize() { + if (context == null) + throw new NullPointerException("context is null"); + if (dialog == null) { + throw new NullPointerException("dialog is null"); + } + functions.SetDialogSize(context, dialog, maxDialogWidth); + } + + + /** + * By default dialog colors will be set to material3 dynamic colors. + * With this method you can set the dialog color for the background + * and buttons to the older non-dynamic colors + * + * @return current class + * @since 1.5 + */ + protected SJDialog setOldTheme() { + setButtonsColor(OLD_BUTTON_COLOR); + setDialogBackgroundResource(R.drawable.dialog_background_old); + setTextColor(defaultOldThemeTextColor); + return this; + } + + /** + * Set a dialog title. + * + * @param title title of a dialog + * @return current class + * @since 1.3 + */ + protected SJDialog setTitle(String title) { + TextView TitleTv = getTitleTextView(); + TitleTv.setText(title); + return this; + } + + /** + * Set a dialog message. + * + * @param message message of a dialog + * @return current class + * @since 1.3 + */ + protected SJDialog setMessage(String message) { + TextView msg = getMessageTextView(); + msg.setText(message); + msg.setVisibility(View.VISIBLE); + return this; + } + + /** + * Set the text alignment for Title TextView. Default is set to {@link SJDialog#TEXT_ALIGNMENT_CENTER} + * @param textAlignment The text alignment to set. Supported types: {@link SJDialog#TEXT_ALIGNMENT_LEFT}, {@link SJDialog#TEXT_ALIGNMENT_RIGHT} and {@link SJDialog#TEXT_ALIGNMENT_CENTER} + * @return current class + * @since 1.6 + * */ + protected SJDialog setTitleAlignment(@TextAlignment int textAlignment){ + TextView title = getTitleTextView(); + title.setGravity(Gravity.RIGHT); + title.setTextAlignment(textAlignment); + + return this; + } + + /** + * Set the text alignment for Message TextView. Default is set to {@link SJDialog#TEXT_ALIGNMENT_LEFT} + * @param textAlignment The text alignment to set. Supported types: {@link SJDialog#TEXT_ALIGNMENT_LEFT}, {@link SJDialog#TEXT_ALIGNMENT_RIGHT} and {@link SJDialog#TEXT_ALIGNMENT_CENTER} + * @return current class + * @since 1.6 + * */ + protected SJDialog setMessageAlignment(@TextAlignment int textAlignment){ + TextView msg = getMessageTextView(); + msg.setTextAlignment(textAlignment); + return this; + } + + /** + * Set text color. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.6 + */ + protected SJDialog setTextColor(@ColorInt int color) { + setTitleTextColor(color); + setMessageTextColor(color); + return this; + } + + /** + * Set title text color. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.6 + */ + protected SJDialog setTitleTextColor(@ColorInt int color) { + getTitleTextView().setTextColor(color); + return this; + } + + /** + * Set message text color. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.6 + */ + protected SJDialog setMessageTextColor(@ColorInt int color) { + getMessageTextView().setTextColor(color); + return this; + } + + /** + * Set a text to left button. + * + * @param text button text + * @return current class + * @since 1.4 + */ + protected SJDialog setLeftButtonText(String text) { + button1.setText(text); + return this; + } + + /** + * Set a text to right button. + * + * @param text button text + * @return current class + * @since 1.4 + */ + protected SJDialog setRightButtonText(String text) { + if (!twoButtons) + throw OneButtonException(); + button2.setText(text); + return this; + } + + /** + * Set text color for all buttons. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.4 + */ + protected SJDialog setButtonsTextColor(@ColorInt int color) { + button1.setTextColor(color); + if (twoButtons) + button2.setTextColor(color); + return this; + } + + /** + * Set text color for left button. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.4 + */ + protected SJDialog setLeftButtonTextColor(@ColorInt int color) { + button1.setTextColor(color); + return this; + } + + /** + * Set text color for right button. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.4 + */ + protected SJDialog setRightButtonTextColor(@ColorInt int color) { + if (!twoButtons) + throw OneButtonException(); + button2.setTextColor(color); + return this; + } + + /** + * Set background color for all buttons. + * + * @param color Color to use for tinting buttons + * @return current class + * @since 1.4 + */ + protected SJDialog setButtonsColor(@ColorInt int color) { + checkButtonResource(0); + button1.getBackground().setTint(color); + if (twoButtons) { + checkButtonResource(1); + button2.getBackground().setTint(color); + } + return this; + } + + /** + * Set background color for left button. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.4 + */ + protected SJDialog setLeftButtonColor(@ColorInt int color) { + checkButtonResource(0); + button1.getBackground().setTint(color); + return this; + } + + /** + * Set background color for right button. + * + * @param color Color to use for tinting this drawable + * @return current class + * @since 1.4 + */ + protected SJDialog setRightButtonColor(@ColorInt int color) { + if (!twoButtons) + throw OneButtonException(); + + checkButtonResource(1); + button2.getBackground().setTint(color); + return this; + } + + protected SJDialog setButtonColor(@ButtonColor String color) { + checkButtonResource(0); + setLeftButtonColor(color); + return this; + } + + protected SJDialog setButtonsColor(@ButtonColor String color) { + setLeftButtonColor(color); + if (twoButtons) + setRightButtonColor(color); + + return this; + } + + protected SJDialog setLeftButtonColor(@ButtonColor String color) { + checkButtonResource(0); + switch (color) { + case RED_BUTTON: + setLeftButtonBackgroundResource(R.drawable.ripple_button_red); + setLeftButtonTextColor(Color.WHITE); + break; + case MATERIAL3_RED_BUTTON: + setLeftButtonBackgroundResource(R.drawable.ripple_button_material3_red); + setLeftButtonTextColor(context.getResources().getColor(R.color.md_theme_onError, context.getTheme())); + break; + case OLD_BUTTON_COLOR: + setLeftButtonBackgroundResource(R.drawable.ripple_button_old); + setLeftButtonTextColor(Color.WHITE); + break; + default: + throw new IllegalArgumentException(color + " is not a valid argument"); + } + return this; + } + + protected SJDialog setRightButtonColor(@ButtonColor String color) { + if (!twoButtons) + throw OneButtonException(); + checkButtonResource(1); + switch (color) { + case RED_BUTTON: + setRightButtonBackgroundResource(R.drawable.ripple_button_red); + setRightButtonTextColor(Color.WHITE); + break; + case MATERIAL3_RED_BUTTON: + setRightButtonBackgroundResource(R.drawable.ripple_button_material3_red); + setRightButtonTextColor(context.getResources().getColor(R.color.md_theme_onError, context.getTheme())); + break; + case OLD_BUTTON_COLOR: + setRightButtonBackgroundResource(R.drawable.ripple_button_old); + setRightButtonTextColor(Color.WHITE); + + break; + default: + throw new IllegalArgumentException(color + " is not a valid argument"); + } + return this; + } + + + /** + * Change dialog color + * @param color {@link ColorInt} + * @return current class + * @since 1.6 + */ + protected SJDialog setDialogBackgroundColor(@ColorInt int color){ + background.getBackground().mutate().setTint(color); + return this; + } + + /** + * Set background resource for dialog. + * + * @param drawable resource id + * @return current class + * @since 1.3 + */ + protected SJDialog setDialogBackgroundResource(@DrawableRes int drawable) { + background.setBackgroundResource(drawable); + return this; + } + + /** + * Set background resource for all buttons. + * + * @param drawable resource id + * @return current class + * @since 1.3 + */ + protected SJDialog setButtonsBackgroundResource(@DrawableRes int drawable) { + checkButtonResource(0); + button1.setBackgroundResource(drawable); + if (twoButtons) { + checkButtonResource(1); + button2.setBackgroundResource(drawable); + } + return this; + } + + /** + * Set background resource for left button. + * + * @param drawable resource id + * @return current class + * @since 1.3 + */ + protected SJDialog setLeftButtonBackgroundResource(@DrawableRes int drawable) { + checkButtonResource(0); + button1.setBackgroundResource(drawable); + return this; + } + + /** + * Set background resource for right button. + * + * @param drawable resource id + * @return current class + * @since 1.3 + */ + protected SJDialog setRightButtonBackgroundResource(@DrawableRes int drawable) { + if (!twoButtons) + throw OneButtonException(); + + checkButtonResource(1); + button2.setBackgroundResource(drawable); + return this; + } + + /** + * Set onClick listener for right button + * + * @param dialogButtonEvent dialog button events + * @return current class + * @since 1.0 + */ + protected SJDialog onButtonClick(DialogButtonEvent dialogButtonEvent) { + leftBtnOnClick = false; + this.dialogButtonEvent = dialogButtonEvent; + + return this; + } + + /** + * Set onClick listener for both buttons + * + * @param dialogButtonEvents dialog button events + * @return current class + * @since 1.0 + */ + protected SJDialog onButtonClick(DialogButtonEvents dialogButtonEvents) { + this.dialogButtonEvents = dialogButtonEvents; + + return this; + } + + /** + * Set onClick listener for left button + * + * @param dialogButtonEvent dialog button event + * @return current class + */ + protected SJDialog onLeftButtonClick(DialogButtonEvent dialogButtonEvent) { + leftBtnOnClick = true; + this.dialogButtonEvent = dialogButtonEvent; + + return this; + } + + /** + * Creating dialog with two buttons. Use this method before changing buttons attributes + * @since 1.5 + */ + protected SJDialog dialogWithTwoButtons() { + twoButtons = true; + return this; + } + + /** + * show dialog + * @since 1.3 + */ + protected SJDialog show() { + setDialogTouchListener(); + addOnClickListener(); + dialog.show(); + return this; + } + + private void addOnClickListener() { + if (dialogButtonEvents == null && dialogButtonEvent == null) + return; + + if (dialogButtonEvents != null) { + button1.setOnClickListener(v -> dialogButtonEvents.onLeftButtonClick()); + button2.setOnClickListener(v -> dialogButtonEvents.onRightButtonClick()); + return; + } + + if (!twoButtons) { + button1.setOnClickListener(v -> dialogButtonEvent.onButtonClick()); + return; + } + + if (leftBtnOnClick) { + button1.setOnClickListener(v -> dialogButtonEvent.onButtonClick()); + return; + } + + button1.setOnClickListener(v -> dismiss()); + button2.setOnClickListener(v -> dialogButtonEvent.onButtonClick()); + } + + /** + * dismiss dialog + * @since 1.3 + */ + public void dismiss() { + dialog.dismiss(); + } + + private void setContentView(@LayoutRes int layoutResID) { + dialog.setContentView(layoutResID); + } + + protected void setButton1(@IdRes int id) { + this.button1 = dialog.findViewById(id); + } + + protected void setButton2(@IdRes int id) { + this.button2 = dialog.findViewById(id); + } + + protected abstract @IdRes int setButtonsRootLayoutID(); + + protected abstract void setButtons(); + + + private void regenerateButtons() { + LinearLayout buttons = dialog.findViewById(setButtonsRootLayoutID()); + regenerateLeftBtn(buttons); + if (onlyOneButton) { + return; + } + regenerateRightBtn(buttons); + } + + @SuppressLint("SetTextI18n") + private void regenerateLeftBtn(LinearLayout buttons) { + if (buttons == null) + buttons = dialog.findViewById(setButtonsRootLayoutID()); + + String txt = button1.getText().toString(); + buttons.removeView(button1); + button1 = (Button) LayoutInflater + .from(newTheme != -1 ? new ContextThemeWrapper(context, newTheme) : context) + .inflate(Btn1Resource,buttons,false); + button1.setText(txt); + buttons.addView(button1, 0); + } + + @SuppressLint("SetTextI18n") + private void regenerateRightBtn(LinearLayout buttons) { + if (buttons == null) + buttons = dialog.findViewById(setButtonsRootLayoutID()); + + int btn2Visibility = button2.getVisibility(); + String txt = button2.getText().toString(); + buttons.removeView(button2); + button2 = (Button) LayoutInflater + .from(newTheme != -1 ? new ContextThemeWrapper(context, newTheme) : context) + .inflate(Btn2Resource,buttons,false); + button2.setVisibility(btn2Visibility); + LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button2.getLayoutParams(); + params.setMarginStart(functions.dpToPixels(context,10)); + button2.setLayoutParams(params); + button2.setText(txt); + buttons.addView(button2, 1); + } + + private void checkButtonResource(int i) { + if (usesDefaultTheme) + return; + + if (i == 0) { + if (Btn1Resource == R.layout.button_template) { + Btn1Resource = R.layout.button_template_1; + regenerateLeftBtn(null); + } + return; + } + if (i == 1) { + if (Btn2Resource == R.layout.button_template) { + Btn2Resource = R.layout.button_template_1; + regenerateRightBtn(null); + } + } + } + + protected void setButton2Visibility(int visibility) { + button2.setVisibility(visibility); + } + + protected TextView getTitleTextView() { + return dialog.findViewById(R.id.titleText); + } + + protected TextView getMessageTextView() { + return dialog.findViewById(R.id.messageTxt); + } + + protected Button getLeftButton() { + return button1; + } + + protected Button getRightButton() { + return button2; + } + + public int getMaxDialogWidth() { + return this.maxDialogWidth; + } + + /** + * Set the maximum width for dialog + * + * @param maxDialogWidth set value for {@link #maxDialogWidth}. Default is 600dp + * @return current class + * @since 1.4 + */ + protected SJDialog setMaxDialogWidth(int maxDialogWidth) { + this.maxDialogWidth = maxDialogWidth; + setDialogSize(); + return this; + } + + + /** + * Set animation for a dialog + * + * @param styleRes style resource + * @return current class + * @since 1.5 + */ + protected SJDialog setDialogAnimations(@StyleRes int styleRes) { + dialog.getWindow().getAttributes().windowAnimations = styleRes; + return this; + } + + /** + * Enable or disable swipe down to dismiss dialog. Default is set to true + * + * @param isSwipeToDismiss Enable or disable swipe action + * @return current class + * @since 1.6 + */ + protected SJDialog swipeToDismiss(boolean isSwipeToDismiss){ + this.isSwipeToDismiss = isSwipeToDismiss; + return this; + } + + /** + * Set dialog OnTouchListener. by default is set to {@link SwipeDismissTouchListener}. + * If this method is used, swipe to dismiss will not work. + * + * @param onTouchListener onTouchListener for dialog view + * @return current class + * @since 1.6 + */ + protected SJDialog setOnTouchListener(View.OnTouchListener onTouchListener) { + dialogOnTouchListener = onTouchListener; + isDefaultOnTouchListener = false; + return this; + } + + private void setDialogTouchListener() { + + if (isDefaultOnTouchListener) + dialogOnTouchListener = new SwipeDismissTouchListener( + dialog.getWindow().getDecorView(), + new SwipeDismissTouchListener.DismissCallbacks() { + @Override + public boolean canDismiss() { + return isSwipeToDismiss; + } + + @Override + public void onDismiss(View view) { + dialog.dismiss(); + } + } + ); + + dialog.getWindow().getDecorView().setOnTouchListener(dialogOnTouchListener); + } + + private OneButtonException OneButtonException() { + return new OneButtonException("Trying to access right button when dialog has only one button. Use 'dialogWithTwoButtons()' to fix the problem."); + } +} + +class OneButtonException extends RuntimeException { + public OneButtonException(String message) { + super(message); + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/SetupDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/SetupDialog.java new file mode 100644 index 0000000..c0fe490 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/SetupDialog.java @@ -0,0 +1,9 @@ +package com.sjapps.library.customdialog; +/** + * by SlaVcE + * Setting up custom dialog. + * @deprecated SetupDialog is renamed to BasicDialog. Use {@link BasicDialog} instead. + * @since 1.0 + */ +@Deprecated(since = "1.6") +public class SetupDialog extends BasicDialog{ } diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/SwipeDismissTouchListener.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/SwipeDismissTouchListener.java new file mode 100644 index 0000000..af14862 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/SwipeDismissTouchListener.java @@ -0,0 +1,216 @@ +package com.sjapps.library.customdialog; + +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.animation.ValueAnimator; +import android.annotation.SuppressLint; +import android.view.MotionEvent; +import android.view.VelocityTracker; +import android.view.View; +import android.view.ViewConfiguration; +import android.view.ViewGroup; + + +public class SwipeDismissTouchListener implements View.OnTouchListener { + private final int mSlop; + private final long mAnimationTime; + + // Fixed properties + private final View mView; + private final DismissCallbacks mCallbacks; + private int mViewHeight = 1; // 1 and not 0 to prevent dividing by zero + + // Transient properties + private float mDownX; + private float mDownY; + private boolean mSwiping; + private int mSwipingSlop; + private VelocityTracker mVelocityTracker; + private float mTranslationY; + + /** + * The callback interface used by {@link SwipeDismissTouchListener} to inform its client + * about a successful dismissal of the view for which it was created. + */ + public interface DismissCallbacks { + /** + * Called to determine whether the view can be dismissed. + */ + boolean canDismiss(); + + /** + * Called when the user has indicated they she would like to dismiss the view. + * + * @param view The originating {@link android.view.View} to be dismissed. + */ + void onDismiss(View view); + } + + /** + * Constructs a new swipe-to-dismiss touch listener for the given view. + * + * @param view The view to make dismissable. + * @param callbacks The callback to trigger when the user has indicated that would like to + * dismiss this view. + */ + public SwipeDismissTouchListener(View view, DismissCallbacks callbacks) { + ViewConfiguration vc = ViewConfiguration.get(view.getContext()); + mSlop = vc.getScaledTouchSlop(); + mAnimationTime = view.getContext().getResources().getInteger(android.R.integer.config_shortAnimTime); + mView = view; + mCallbacks = callbacks; + } + @SuppressLint("ClickableViewAccessibility") + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + // offset because the view is translated during swipe + motionEvent.offsetLocation(0, mTranslationY); + + boolean returnTrue = false; + + if (mViewHeight < 2) { + mViewHeight = mView.getHeight(); + } + + switch (motionEvent.getActionMasked()) { + case MotionEvent.ACTION_DOWN: { + // TODO: ensure this is a finger, and set a flag + mDownX = motionEvent.getRawX(); + mDownY = motionEvent.getRawY(); + if (mCallbacks.canDismiss()) { + mVelocityTracker = VelocityTracker.obtain(); + mVelocityTracker.addMovement(motionEvent); + } + return true; + } + + case MotionEvent.ACTION_UP: { + if (mVelocityTracker == null) { + break; + } + + float deltaY = motionEvent.getRawY() - mDownY; + mVelocityTracker.addMovement(motionEvent); + mVelocityTracker.computeCurrentVelocity(1000); + + boolean dismiss = false; + boolean dismissDown = false; + if (deltaY > (float) mViewHeight / 2 && mSwiping) { + dismiss = true; + dismissDown = deltaY > 0; + returnTrue = true; + } else if (Math.abs(deltaY) > 0){ + returnTrue = true; + } + if (dismiss) { + // dismiss + mView.animate() + .translationY(dismissDown ? mViewHeight : 0) + .alpha(0) + .setDuration(mAnimationTime) + .setListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + performDismiss(); + } + }); + + } else if (mSwiping) { + // cancel + + mView.animate() + .translationY(0) + .alpha(1) + .setDuration(mAnimationTime) + .setListener(null); + } + mVelocityTracker.recycle(); + mVelocityTracker = null; + mTranslationY = 0; + mDownX = 0; + mDownY = 0; + mSwiping = false; + break; + } + + case MotionEvent.ACTION_CANCEL: { + if (mVelocityTracker == null) { + break; + } + mView.animate() + .translationY(0) + .alpha(1) + .setDuration(mAnimationTime) + .setListener(null); + mVelocityTracker.recycle(); + mVelocityTracker = null; + mTranslationY = 0; + mDownX = 0; + mDownY = 0; + mSwiping = false; + break; + } + + case MotionEvent.ACTION_MOVE: { + if (mVelocityTracker == null) { + break; + } + mVelocityTracker.addMovement(motionEvent); + float deltaX = motionEvent.getRawX() - mDownX; + float deltaY = motionEvent.getRawY() - mDownY; + if (Math.abs(deltaY) > mSlop && Math.abs(deltaX) < Math.abs(deltaY) / 2) { + mSwiping = true; + mSwipingSlop = (deltaY > 0 ? mSlop : 0); + mView.getParent().requestDisallowInterceptTouchEvent(true); + + + MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); + cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); + mView.onTouchEvent(cancelEvent); + cancelEvent.recycle(); + } + + if (mSwiping) { + mTranslationY = deltaY; + if (deltaY - mSwipingSlop < 0) + mView.setTranslationY(0); + else mView.setTranslationY(deltaY - mSwipingSlop); + return true; + } + break; + } + + } + return returnTrue; + } + + private void performDismiss() { + // Animate the dismissed view to zero-height and then fire the dismiss callback. + // This triggers layout on each animation frame; in the future we may want to do something + // smarter and more performant. + + final ViewGroup.LayoutParams lp = mView.getLayoutParams(); + final int originalHeight = mView.getHeight(); + + ValueAnimator animator = ValueAnimator.ofInt(originalHeight, 1).setDuration(mAnimationTime); + + animator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + mCallbacks.onDismiss(mView); + // Reset view presentation + mView.setAlpha(1f); + mView.setTranslationX(0); + lp.height = originalHeight; + mView.setLayoutParams(lp); + } + }); + + animator.addUpdateListener(valueAnimator -> { + lp.height = (Integer) valueAnimator.getAnimatedValue(); + mView.setLayoutParams(lp); + }); + + animator.start(); + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java new file mode 100644 index 0000000..0456f1a --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java @@ -0,0 +1,162 @@ +package com.sjapps.library.customdialog.adapter; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.RecyclerView; + +import com.sjapps.library.R; +import com.sjapps.library.customdialog.ImageListItem; +import com.sjapps.library.customdialog.list.events.ListItemClickObj; + +import java.util.ArrayList; + +public class DefaultImageListAdapter extends RecyclerView.Adapter { + + public ArrayList arrayListItems; + + boolean isSelectable; + + int itemBgRes; + int itemBgResSelected; + int listItemBgColor; + int listItemBgColorSelected; + int textColor; + + ListItemClickObj itemClick; + ArrayList selectedItems; + + + public static class ViewHolder extends RecyclerView.ViewHolder { + TextView valTxt; + ImageView imageView; + + public ViewHolder(@NonNull View itemView) { + super(itemView); + valTxt = itemView.findViewById(R.id.value1Txt); + imageView = itemView.findViewById(R.id.imageView); + } + + public TextView getTxt() { + return valTxt; + } + + public ImageView getImageView() { + return imageView; + } + + public View getView() { + return itemView.findViewById(R.id.layoutItem); + } + + } + + public DefaultImageListAdapter(ArrayList arrayList, + boolean isSelectable, + @Nullable ListItemClickObj itemClick, + ArrayList selectedItems, + int itemBgRes, + int itemBgResSelected, + int textColor, + int listItemBgColor, + int listItemBgColorSelected) { + this.itemClick = itemClick; + this.isSelectable = isSelectable; + this.selectedItems = selectedItems; + this.itemBgRes = itemBgRes; + this.itemBgResSelected = itemBgResSelected; + this.textColor = textColor; + this.arrayListItems = arrayList; + this.listItemBgColor = listItemBgColor; + this.listItemBgColorSelected = listItemBgColorSelected; + } + + + @NonNull + @Override + public DefaultImageListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.default_image_list_item, parent, false); + return new DefaultImageListAdapter.ViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + setValues(holder, position); + setBackground(holder, position); + + if (textColor != 1) { + holder.getTxt().setTextColor(textColor); + } + + if (isSelectable) { + holder.getView().setOnClickListener(v -> + checkSelected(holder, arrayListItems.get(position)) + ); + + return; + } + + holder.getView().setOnClickListener(v -> itemClick.onClick(position, arrayListItems.get(position))); + + } + + private void setBackground(@NonNull ViewHolder holder, int position) { + if (!isSelectable) { + setItemResource(holder, itemBgRes); + return; + } + + if (selectedItems.contains(arrayListItems.get(position))) { + setItemResource(holder, itemBgResSelected); + setItemColor(holder,listItemBgColorSelected); + } + else { + setItemResource(holder, itemBgRes); + setItemColor(holder,listItemBgColor); + } + } + + private void setValues(@NonNull ViewHolder holder, int position) { + holder.getTxt().setText(arrayListItems.get(position).getName()); + holder.getImageView().setImageDrawable(arrayListItems.get(position).getImage()); + + } + + private void checkSelected(@NonNull ViewHolder holder,ImageListItem obj) { + if (!selectedItems.contains(obj)) + selectItem(holder, obj); + else deselectItem(holder, obj); + } + + private void selectItem(@NonNull ViewHolder holder,ImageListItem obj) { + selectedItems.add(obj); + setItemResource(holder, itemBgResSelected); + setItemColor(holder,listItemBgColorSelected); + } + + private void deselectItem(@NonNull ViewHolder holder,ImageListItem obj) { + selectedItems.remove(obj); + setItemResource(holder, itemBgRes); + setItemColor(holder,listItemBgColor); + } + + private void setItemResource(@NonNull ViewHolder holder, int drawable) { + holder.getView().setBackgroundResource(drawable); + } + + private void setItemColor(@NonNull ViewHolder holder, int color){ + if (color == -1) + return; + holder.getView().getBackground().mutate().setTint(color); + } + + @Override + public int getItemCount() { + return arrayListItems.size(); + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java new file mode 100644 index 0000000..c0526b6 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java @@ -0,0 +1,112 @@ +package com.sjapps.library.customdialog.adapter; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.sjapps.library.R; +import com.sjapps.library.customdialog.list.events.ListItemClick; + +import java.util.ArrayList; + +public class DefaultListAdapter extends RecyclerView.Adapter { + + String[] items; + boolean isSelectable; + ListItemClick itemClick; + ArrayList selectedItems; + int itemBgRes; + int itemBgResSelected; + int listItemBgColor; + int listItemBgColorSelected; + int textColor; + + + public DefaultListAdapter(String[] items, + boolean isSelectable, + ListItemClick itemClick, + ArrayList selectedItems, + int itemBgRes, + int itemBgResSelected, + int textColor, + int listItemBgColor, + int listItemBgColorSelected) { + this.items = items; + this.isSelectable = isSelectable; + this.itemClick = itemClick; + this.selectedItems = selectedItems; + this.itemBgRes = itemBgRes; + this.itemBgResSelected = itemBgResSelected; + this.textColor = textColor; + this.listItemBgColor = listItemBgColor; + this.listItemBgColorSelected = listItemBgColorSelected; + + } + + @NonNull + @Override + public DefaultListAdapterGeneric.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.default_list_item,parent,false); + return new DefaultListAdapterGeneric.ViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position) { + holder.getVal1Txt().setText(items[position]); + + if (selectedItems.contains(items[position])) { + setItemResource(holder, itemBgResSelected); + setItemColor(holder,listItemBgColorSelected); + } + else { + setItemResource(holder, itemBgRes); + setItemColor(holder,listItemBgColor); + } + if (textColor != 1) + holder.getVal1Txt().setTextColor(textColor); + + if (isSelectable) { + holder.getView().setOnClickListener(v -> { + if (!selectedItems.contains(items[position])) + selectItem(holder,position); + else deselectItem(holder, position); + }); + return; + } + + holder.getView().setOnClickListener(v -> itemClick.onClick(position,items[position])); + + } + + private void selectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position){ + selectedItems.add(items[position]); + setItemResource(holder,itemBgResSelected); + setItemColor(holder,listItemBgColorSelected); + + } + + private void deselectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position){ + selectedItems.remove(items[position]); + setItemResource(holder,itemBgRes); + setItemColor(holder,listItemBgColor); + } + + private void setItemResource(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int drawable){ + holder.getView().setBackgroundResource(drawable); + } + + private void setItemColor(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int color){ + if (color == -1) + return; + holder.getView().getBackground().mutate().setTint(color); + } + + @Override + public int getItemCount() { + return items.length; + } + +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java new file mode 100644 index 0000000..fd9c9da --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java @@ -0,0 +1,290 @@ +package com.sjapps.library.customdialog.adapter; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.RecyclerView; + +import com.sjapps.library.R; +import com.sjapps.library.customdialog.ListItemValue; +import com.sjapps.library.customdialog.ListItemValues; +import com.sjapps.library.customdialog.list.events.ListItemClickObj; + +import java.util.ArrayList; + +public class DefaultListAdapterGeneric extends RecyclerView.Adapter { + + public ArrayList arrayListItems; + public T[] listObj; + + private ListItemValue value; + private ListItemValues values; + + boolean isArrList; + boolean isObjArr; + boolean hasTwoVal; + boolean isSelectable; + + int itemBgRes; + int itemBgResSelected; + int listItemBgColor; + int listItemBgColorSelected; + int textColor; + + ListItemClickObj itemClick; + ArrayList selectedItems; + + + public static class ViewHolder extends RecyclerView.ViewHolder{ + TextView val1Txt; + TextView val2Txt; + + public ViewHolder(@NonNull View itemView) { + super(itemView); + val1Txt = itemView.findViewById(R.id.value1Txt); + val2Txt = itemView.findViewById(R.id.value2Txt); + } + + public TextView getVal1Txt() { + return val1Txt; + } + + public TextView getVal2Txt() { + return val2Txt; + } + + public View getView(){ + return itemView.findViewById(R.id.layoutItem); + } + + } + + public DefaultListAdapterGeneric(T[] objArray, + ListItemValue value, + boolean isSelectable, + @Nullable ListItemClickObj itemClick, + ArrayList selectedItems, + int itemBgRes, + int itemBgResSelected, + int textColor, + int listItemBgColor, + int listItemBgColorSelected){ + this.value = value; + this.itemClick = itemClick; + this.isSelectable = isSelectable; + this.selectedItems = selectedItems; + this.itemBgRes = itemBgRes; + this.itemBgResSelected = itemBgResSelected; + this.textColor = textColor; + this.listItemBgColor = listItemBgColor; + this.listItemBgColorSelected = listItemBgColorSelected; + ObjArrayAdapter(objArray); + } + + public DefaultListAdapterGeneric(T[] objArray, + ListItemValues values, + boolean isSelectable, + @Nullable ListItemClickObj itemClick, + ArrayList selectedItems, + int itemBgRes, + int itemBgResSelected, + int textColor, + int listItemBgColor, + int listItemBgColorSelected){ + this.values = values; + this.itemClick = itemClick; + hasTwoVal = true; + this.isSelectable = isSelectable; + this.selectedItems = selectedItems; + this.itemBgRes = itemBgRes; + this.itemBgResSelected = itemBgResSelected; + this.textColor = textColor; + this.listItemBgColor = listItemBgColor; + this.listItemBgColorSelected = listItemBgColorSelected; + ObjArrayAdapter(objArray); + } + + public DefaultListAdapterGeneric(ArrayList arrayList, + ListItemValue value, + boolean isSelectable, + @Nullable ListItemClickObj itemClick, + ArrayList selectedItems, + int itemBgRes, + int itemBgResSelected, + int textColor, + int listItemBgColor, + int listItemBgColorSelected){ + this.value = value; + this.itemClick = itemClick; + this.isSelectable = isSelectable; + this.selectedItems = selectedItems; + this.itemBgRes = itemBgRes; + this.itemBgResSelected = itemBgResSelected; + this.textColor = textColor; + this.listItemBgColor = listItemBgColor; + this.listItemBgColorSelected = listItemBgColorSelected; + ArrayListAdapter(arrayList); + } + + public DefaultListAdapterGeneric(ArrayList arrayList, + ListItemValues values, + boolean isSelectable, + @Nullable ListItemClickObj itemClick, + ArrayList selectedItems, + int itemBgRes, + int itemBgResSelected, + int textColor, + int listItemBgColor, + int listItemBgColorSelected){ + this.values = values; + this.itemClick = itemClick; + hasTwoVal = true; + this.isSelectable = isSelectable; + this.selectedItems = selectedItems; + this.itemBgRes = itemBgRes; + this.itemBgResSelected = itemBgResSelected; + this.textColor = textColor; + this.listItemBgColor = listItemBgColor; + this.listItemBgColorSelected = listItemBgColorSelected; + ArrayListAdapter(arrayList); + } + + private void ObjArrayAdapter(T[] objArray){ + this.listObj = objArray; + isObjArr = true; + + } + + private void ArrayListAdapter(ArrayList arrayList){ + this.arrayListItems = arrayList; + isArrList = true; + } + + @NonNull + @Override + public DefaultListAdapterGeneric.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.default_list_item,parent,false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position) { + setValues(holder,position); + setBackground(holder,position); + + if (textColor != 1){ + holder.getVal1Txt().setTextColor(textColor); + holder.getVal2Txt().setTextColor(textColor); + } + + if (isSelectable){ + holder.getView().setOnClickListener(v -> { + if (isArrList) + checkSelected(holder, arrayListItems.get(position)); + else if (isObjArr) + checkSelected(holder, listObj[position]); + }); + + return; + } + + if (isArrList) + holder.getView().setOnClickListener(v -> itemClick.onClick(position,arrayListItems.get(position))); + + if (isObjArr) + holder.getView().setOnClickListener(v -> itemClick.onClick(position,listObj[position])); + + } + + private void setBackground(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position) { + if (!isSelectable) { + setItemResource(holder,itemBgRes); + return; + } + if (isArrList) { + if (selectedItems.contains(arrayListItems.get(position))) { + setItemResource(holder, itemBgResSelected); + setItemColor(holder,listItemBgColorSelected); + } + else { + setItemResource(holder, itemBgRes); + setItemColor(holder,listItemBgColor); + } + return; + } + if (isObjArr) + if (selectedItems.contains(listObj[position])) { + setItemResource(holder, itemBgResSelected); + setItemColor(holder,listItemBgColorSelected); + } + else { + setItemResource(holder, itemBgRes); + setItemColor(holder,listItemBgColor); + } + } + + private void setValues(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position){ + if (isArrList){ + if (!hasTwoVal) { + holder.getVal1Txt().setText(value.getValue(arrayListItems.get(position))); + return; + } + holder.getVal1Txt().setText(values.getValue1(arrayListItems.get(position))); + holder.getVal2Txt().setText(values.getValue2(arrayListItems.get(position))); + holder.getVal2Txt().setVisibility(View.VISIBLE); + return; + } + if (isObjArr) { + if (!hasTwoVal) { + holder.getVal1Txt().setText(value.getValue(listObj[position])); + return; + } + holder.getVal1Txt().setText(values.getValue1(listObj[position])); + holder.getVal2Txt().setText(values.getValue2(listObj[position])); + holder.getVal2Txt().setVisibility(View.VISIBLE); + } + } + + private void checkSelected(@NonNull DefaultListAdapterGeneric.ViewHolder holder, T obj){ + if (!selectedItems.contains(obj)) + selectItem(holder,obj); + else deselectItem(holder,obj); + } + + private void selectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, T obj){ + selectedItems.add(obj); + setItemResource(holder, itemBgResSelected); + setItemColor(holder,listItemBgColorSelected); + + } + + private void deselectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, T obj){ + selectedItems.remove(obj); + setItemResource(holder,itemBgRes); + setItemColor(holder,listItemBgColor); + + } + + private void setItemResource(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int drawable){ + holder.getView().setBackgroundResource(drawable); + } + + private void setItemColor(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int color){ + if (color == -1) + return; + holder.getView().getBackground().mutate().setTint(color); + } + + @Override + public int getItemCount() { + if (isArrList) + return arrayListItems.size(); + if (isObjArr) + return listObj.length; + return 0; + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/functions.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/functions.java new file mode 100644 index 0000000..ceb278a --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/functions.java @@ -0,0 +1,31 @@ +package com.sjapps.library.customdialog; + +import android.app.Dialog; +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.util.Log; +import android.util.TypedValue; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; + +import com.sjapps.library.R; + +public class functions { + public static void SetDialogSize(@NonNull Context context, Dialog dialog, int maxWidth){ + Configuration configuration = context.getResources().getConfiguration(); + int width = ViewGroup.LayoutParams.MATCH_PARENT; + int height = ViewGroup.LayoutParams.WRAP_CONTENT; + + if (configuration.screenWidthDp > maxWidth) + width = dpToPixels(context,maxWidth); + + dialog.getWindow().setLayout(width, height); + + } + public static int dpToPixels(@NonNull Context context, float dp) { + Resources r = context.getResources(); + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); + } +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/list/events/ListItemClick.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/list/events/ListItemClick.java new file mode 100644 index 0000000..43f2c47 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/list/events/ListItemClick.java @@ -0,0 +1,5 @@ +package com.sjapps.library.customdialog.list.events; + +public interface ListItemClick { + void onClick(int position, String value); +} diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/list/events/ListItemClickObj.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/list/events/ListItemClickObj.java new file mode 100644 index 0000000..5c38355 --- /dev/null +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/list/events/ListItemClickObj.java @@ -0,0 +1,5 @@ +package com.sjapps.library.customdialog.list.events; + +public interface ListItemClickObj { + void onClick(int position, T obj); +} diff --git a/SJDialog/src/main/res/anim/slide_in.xml b/SJDialog/src/main/res/anim/slide_in.xml new file mode 100644 index 0000000..97f2515 --- /dev/null +++ b/SJDialog/src/main/res/anim/slide_in.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/anim/slide_out.xml b/SJDialog/src/main/res/anim/slide_out.xml new file mode 100644 index 0000000..dc2c40d --- /dev/null +++ b/SJDialog/src/main/res/anim/slide_out.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/dialog_background.xml b/SJDialog/src/main/res/drawable/dialog_background.xml new file mode 100644 index 0000000..aa579df --- /dev/null +++ b/SJDialog/src/main/res/drawable/dialog_background.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/dialog_background_material3_red.xml b/SJDialog/src/main/res/drawable/dialog_background_material3_red.xml new file mode 100644 index 0000000..a82d7b0 --- /dev/null +++ b/SJDialog/src/main/res/drawable/dialog_background_material3_red.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/dialog_background_old.xml b/SJDialog/src/main/res/drawable/dialog_background_old.xml new file mode 100644 index 0000000..0812011 --- /dev/null +++ b/SJDialog/src/main/res/drawable/dialog_background_old.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/dialog_background_red.xml b/SJDialog/src/main/res/drawable/dialog_background_red.xml new file mode 100644 index 0000000..c3b617d --- /dev/null +++ b/SJDialog/src/main/res/drawable/dialog_background_red.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_button.xml b/SJDialog/src/main/res/drawable/ripple_button.xml new file mode 100644 index 0000000..535fe85 --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_button.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_button_material3_red.xml b/SJDialog/src/main/res/drawable/ripple_button_material3_red.xml new file mode 100644 index 0000000..846ed5a --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_button_material3_red.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_button_old.xml b/SJDialog/src/main/res/drawable/ripple_button_old.xml new file mode 100644 index 0000000..6ecd2a7 --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_button_old.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_button_red.xml b/SJDialog/src/main/res/drawable/ripple_button_red.xml new file mode 100644 index 0000000..e799e3b --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_button_red.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_list.xml b/SJDialog/src/main/res/drawable/ripple_list.xml new file mode 100644 index 0000000..18b9d7e --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_list.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_list_old.xml b/SJDialog/src/main/res/drawable/ripple_list_old.xml new file mode 100644 index 0000000..5ae90de --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_list_old.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_list_selected.xml b/SJDialog/src/main/res/drawable/ripple_list_selected.xml new file mode 100644 index 0000000..b87b794 --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_list_selected.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/drawable/ripple_list_selected_old.xml b/SJDialog/src/main/res/drawable/ripple_list_selected_old.xml new file mode 100644 index 0000000..affa5af --- /dev/null +++ b/SJDialog/src/main/res/drawable/ripple_list_selected_old.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/SJDialog/src/main/res/font/koho.ttf b/SJDialog/src/main/res/font/koho.ttf new file mode 100644 index 0000000..78e04aa Binary files /dev/null and b/SJDialog/src/main/res/font/koho.ttf differ diff --git a/SJDialog/src/main/res/font/koho_bold.ttf b/SJDialog/src/main/res/font/koho_bold.ttf new file mode 100644 index 0000000..a0123f5 Binary files /dev/null and b/SJDialog/src/main/res/font/koho_bold.ttf differ diff --git a/SJDialog/src/main/res/font/koho_italic.ttf b/SJDialog/src/main/res/font/koho_italic.ttf new file mode 100644 index 0000000..e6cf346 Binary files /dev/null and b/SJDialog/src/main/res/font/koho_italic.ttf differ diff --git a/SJDialog/src/main/res/layout/basic_dialog.xml b/SJDialog/src/main/res/layout/basic_dialog.xml new file mode 100644 index 0000000..b581a7f --- /dev/null +++ b/SJDialog/src/main/res/layout/basic_dialog.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + +