Skip to content

Commit

Permalink
update SetupDialog and MessageDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
SlaVcE14 committed Jun 20, 2022
1 parent 1821ba5 commit d0b7ca6
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 168 deletions.
4 changes: 2 additions & 2 deletions SJDialog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {

defaultConfig {
minSdk 23
versionCode 9
versionName "1.3.2"
versionCode 10
versionName "1.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
Expand All @@ -22,7 +18,7 @@ public class MessageDialog {
public Dialog dialog;
private Button button;
Context context;
private int maxWidth = 600;
private int maxDialogWidth = 600;

public MessageDialog(){

Expand All @@ -33,7 +29,7 @@ public MessageDialog Builder(Context context){
dialog = new Dialog(context);
dialog.setContentView(R.layout.message_dialog);
button = dialog.findViewById(R.id.btn);
SetDialogSize(context);
setDialogSize();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
button.setOnClickListener(v -> dialog.dismiss());
return this;
Expand Down Expand Up @@ -145,14 +141,28 @@ public Button getButton() {
return button;
}

private void SetDialogSize(Context context){
Configuration configuration = context.getResources().getConfiguration();
if (configuration.screenWidthDp > maxWidth)
dialog.getWindow().setLayout(dpToPixels(context,maxWidth), ViewGroup.LayoutParams.WRAP_CONTENT);
else dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
/**
* Set a maximum width for dialog
* @param maxDialogWidth set value for {@link #maxDialogWidth}. Default is 600
* @return current class
*/
public MessageDialog setMaxDialogWidth(int maxDialogWidth){
this.maxDialogWidth = maxDialogWidth;
setDialogSize();
return this;
}

public int getMaxDialogWidth(){
return this.maxDialogWidth;
}
private int dpToPixels(Context context, float dp) {
Resources r = context.getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());

private 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
Expand All @@ -20,32 +16,37 @@
* Setting up custom dialog.
*/
public class SetupDialog {

@Deprecated
public static final String LONG_TYPE = "long";
@Deprecated
public static final String SHORT_TYPE = "short";
public static final String RED_BUTTON = "RedBtn";

public Dialog dialog;
private Button button1, button2;
private int maxWidth = 600;
private int maxDialogWidth = 600;
Context context;
private String selectedDialogType = null;

public SetupDialog (){
}

public SetupDialog Long(Context context,String Title,String Text, String Btn1Txt, String Btn2Txt){
return DialogBuilder(context,Title,Text,Btn1Txt,Btn2Txt);
}
public SetupDialog Long(Context context,String Title,String Text, String Btn2Txt){
return Long(context,Title,Text,null,Btn2Txt);
public SetupDialog Short(Context context, String Title){
return Short(context,Title,null);
}
public SetupDialog Short(Context context, String Title, String Btn1Txt, String Btn2Txt){
return DialogBuilder(context,Title,null,Btn1Txt,Btn2Txt);
}
public SetupDialog Short(Context context, String Title, String Btn2Txt){
return Short(context,Title,null,Btn2Txt);
}
public SetupDialog Long(Context context,String Title,String Message){
return Long(context,Title,Message,null);
}
public SetupDialog Long(Context context,String Title,String Text, String Btn1Txt, String Btn2Txt){
return DialogBuilder(context,Title,Text,Btn1Txt,Btn2Txt);
}
public SetupDialog Long(Context context,String Title,String Text, String Btn2Txt){
return Long(context,Title,Text,null,Btn2Txt);
}
public SetupDialog Delete(Context context,String Title){
return DialogBuilder(context,Title,null,null,null).setRightButtonColor(RED_BUTTON);
}
Expand All @@ -55,38 +56,28 @@ public SetupDialog Delete(Context context){
public SetupDialog DialogBuilder(Context context){
this.context = context;
dialog = new Dialog(context);
return this;
}

public SetupDialog DialogBuilder(Context context, String dialogType){
this.context = context;
dialog = new Dialog(context);
if (dialogType.equals(SHORT_TYPE)){
dialog.setContentView(R.layout.popup_panel);
}else if (dialogType.equals(LONG_TYPE)){
dialog.setContentView(R.layout.popup_panel_long);
}else throw new IllegalArgumentException("Invalid dialog type");

this.selectedDialogType = dialogType;
SetDialogSize(context);
dialog.setContentView(R.layout.popup_panel);
setDialogSize();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
button1 = dialog.findViewById(R.id.Btn1);
button2 = dialog.findViewById(R.id.Btn2);
return this;
}

/**
* @deprecated Dialog type is automatic. Use {@link SetupDialog#DialogBuilder(Context)}
*/
@Deprecated
public SetupDialog DialogBuilder(Context context, String dialogType){
return DialogBuilder(context);
}


public SetupDialog DialogBuilder(Context context,String Title,String Text, String Btn1Txt, String Btn2Txt){
this.context = context;
dialog = new Dialog(context);
if (Text == null){
dialog.setContentView(R.layout.popup_panel);
}else{
dialog.setContentView(R.layout.popup_panel_long);
TextView msg = dialog.findViewById(R.id.textView);
msg.setText(Text);
}
SetDialogSize(context);
dialog.setContentView(R.layout.popup_panel);
setDialogSize();
TextView TitleTv = dialog.findViewById(R.id.TitleText);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
button1 = dialog.findViewById(R.id.Btn1);
Expand All @@ -97,6 +88,9 @@ public SetupDialog DialogBuilder(Context context,String Title,String Text, Strin
if (Btn2Txt != null) {
button2.setText(Btn2Txt);
}
if (Text != null){
setMessage(Text);
}
if (Title == null){
return this;
}
Expand All @@ -107,19 +101,11 @@ public SetupDialog DialogBuilder(Context context,String Title,String Text, Strin
* Set a dialog type.
* @param dialogType type of a dialog. Supported types: {@link #SHORT_TYPE} and {@link #LONG_TYPE}
* @return current class
* @deprecated Dialog type is automatic. Remove this.
* */
@Deprecated
public SetupDialog setDialogType(String dialogType){
if (dialogType.equals(SHORT_TYPE)){
dialog.setContentView(R.layout.popup_panel);
}else if (dialogType.equals(LONG_TYPE)){
dialog.setContentView(R.layout.popup_panel_long);
}else throw new IllegalArgumentException("Invalid dialog type!");

this.selectedDialogType = dialogType;
SetDialogSize(context);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
button1 = dialog.findViewById(R.id.Btn1);
button2 = dialog.findViewById(R.id.Btn2);
//Noting here!!
return this;
}
/**
Expand All @@ -128,8 +114,6 @@ public SetupDialog setDialogType(String dialogType){
* @return current class
* */
public SetupDialog setTitle(String title){
checkDialogType();

TextView TitleTv = dialog.findViewById(R.id.TitleText);
TitleTv.setText(title);
return this;
Expand All @@ -140,22 +124,17 @@ public SetupDialog setTitle(String title){
* @return current class
* */
public SetupDialog setMessage(String message){
checkDialogType();

if (selectedDialogType.equals(LONG_TYPE)) {
TextView msg = dialog.findViewById(R.id.textView);
msg.setText(message);
return this;
}
throw new IllegalArgumentException("This view is not available in this dialog type!");
TextView msg = dialog.findViewById(R.id.messageTxt);
msg.setText(message);
msg.setVisibility(View.VISIBLE);
return this;
}
/**
* Set a text to left button.
* @param text button text
* @return current class
* */
public SetupDialog setLeftButtonText(String text){
checkDialogType();
button1.setText(text);
return this;
}
Expand All @@ -165,7 +144,6 @@ public SetupDialog setLeftButtonText(String text){
* @return current class
* */
public SetupDialog setRightButtonText(String text){
checkDialogType();
button2.setText(text);
return this;
}
Expand Down Expand Up @@ -344,21 +322,29 @@ public Button getRightButton() {
return button2;
}

private void SetDialogSize(Context context){
Configuration configuration = context.getResources().getConfiguration();
if (configuration.screenWidthDp > maxWidth)
dialog.getWindow().setLayout(dpToPixels(context,maxWidth), ViewGroup.LayoutParams.WRAP_CONTENT);
else dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

/**
* Set a maximum width for dialog
* @param maxDialogWidth set value for {@link #maxDialogWidth}. Default is 600
* @return current class
*/
public SetupDialog setMaxDialogWidth(int maxDialogWidth){
this.maxDialogWidth = maxDialogWidth;
setDialogSize();
return this;
}

private int dpToPixels(Context context, float dp) {
Resources r = context.getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
public int getMaxDialogWidth(){
return this.maxDialogWidth;
}
private void checkDialogType(){
if (selectedDialogType == null)
throw new NullPointerException("Dialog type is not selected! Use 'setDialogType(type)' to set dialog type.");

private 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);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.TypedValue;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

public class functions {
public static void SetDialogSize(@NonNull Context context, Dialog dialog, int maxWidth){
Configuration configuration = context.getResources().getConfiguration();
if (configuration.screenWidthDp > maxWidth)
dialog.getWindow().setLayout(dpToPixels(context,maxWidth), ViewGroup.LayoutParams.WRAP_CONTENT);
else dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

}
private static int dpToPixels(@NonNull Context context, float dp) {
Resources r = context.getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
}
11 changes: 11 additions & 0 deletions SJDialog/src/main/res/layout/popup_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
android:textAlignment="center"
android:textSize="25dp" />

<TextView
android:id="@+id/messageTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:visibility="gone"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Loading

0 comments on commit d0b7ca6

Please sign in to comment.