Skip to content

Commit

Permalink
Fix theme 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SlaVcE14 committed Jul 31, 2022
1 parent eb8dcd9 commit c0e1cde
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.ContextThemeWrapper;
import android.view.DragEvent;
import android.view.View;

import android.widget.Button;
Expand All @@ -29,7 +28,11 @@ public abstract class SJDialog {
public static final String RED_BUTTON = "RedBtn";
public static final String OLD_BUTTON_COLOR = "OldBtnColor";

private @LayoutRes int Btn1Resource = R.layout.button_template;
private @LayoutRes int Btn2Resource = R.layout.button_template;

private final int defaultTheme = R.style.Theme_SJDialog;
private boolean usesDefaultTheme = true;

public Dialog dialog;
protected Button button1, button2;
Expand Down Expand Up @@ -61,8 +64,10 @@ protected SJDialog Builder(Context context, @LayoutRes int layoutResID, @StyleRe
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations = R.style.SJDialogAnimation;
setButtons();
if (theme != defaultTheme)
if (theme != defaultTheme || useAppTheme) {
usesDefaultTheme = false;
regenerateButtons();
}
return this;
}

Expand Down Expand Up @@ -182,9 +187,12 @@ protected SJDialog setRightButtonTextColor(@ColorInt int color) {
* @return current class
*/
protected SJDialog setButtonsColor(@ColorInt int color) {
checkButtonResource(0);
button1.getBackground().setTint(color);
if (twoButtons)
if (twoButtons){
checkButtonResource(1);
button2.getBackground().setTint(color);
}
return this;
}

Expand All @@ -195,6 +203,7 @@ protected SJDialog setButtonsColor(@ColorInt int color) {
* @return current class
*/
protected SJDialog setLeftButtonColor(@ColorInt int color) {
checkButtonResource(0);
button1.getBackground().setTint(color);
return this;
}
Expand All @@ -208,11 +217,14 @@ protected SJDialog setLeftButtonColor(@ColorInt int color) {
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;
}
Expand All @@ -221,10 +233,12 @@ 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);
Expand All @@ -243,7 +257,7 @@ protected SJDialog setLeftButtonColor(@ButtonColor String color) {
protected SJDialog setRightButtonColor(@ButtonColor String color) {
if (!twoButtons)
throw OneButtonException();

checkButtonResource(1);
switch (color) {
case RED_BUTTON:
setRightButtonBackgroundResource(R.drawable.ripple_button_red);
Expand All @@ -252,6 +266,7 @@ protected SJDialog setRightButtonColor(@ButtonColor String color) {
case OLD_BUTTON_COLOR:
setRightButtonBackgroundResource(R.drawable.ripple_button_old);
setRightButtonTextColor(Color.WHITE);

break;
default:
throw new IllegalArgumentException(color + " is not a valid argument");
Expand Down Expand Up @@ -383,24 +398,59 @@ protected void setButton2(@IdRes int id) {

protected abstract void setButtons();

@SuppressLint("SetTextI18n")

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());

buttons.removeView(button1);
Activity activity = (Activity) context;
button1 = (Button) activity.getLayoutInflater().inflate(R.layout.button_template, buttons, false);
button1 = (Button) activity.getLayoutInflater().inflate(Btn1Resource, buttons, false);
button1.setText("Cancel");
buttons.addView(button1);
buttons.addView(button1,0);
}

@SuppressLint("SetTextI18n")
private void regenerateRightBtn(LinearLayout buttons){
if (buttons == null)
buttons = dialog.findViewById(setButtonsRootLayoutID());

if (onlyOneButton) {
return;
}
int btn2Visibility = button2.getVisibility();
buttons.removeView(button2);
button2 = (Button) activity.getLayoutInflater().inflate(R.layout.button_template, buttons, false);
Activity activity = (Activity) context;
button2 = (Button) activity.getLayoutInflater().inflate(Btn2Resource, buttons, false);
button2.setVisibility(btn2Visibility);
button2.setText("Ok");
buttons.addView(button2);
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) {
Expand Down
8 changes: 5 additions & 3 deletions SJDialog/src/main/res/layout/button_template.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/SJDialog.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_height="45dp"
android:layout_marginHorizontal="5dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
android:autoSizeMinTextSize="5dp"
android:autoSizeStepGranularity="1dp"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="Btn" />
android:text="Btn"
/>
15 changes: 15 additions & 0 deletions SJDialog/src/main/res/layout/button_template_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/SJDialog.ButtonStyle"
android:theme="@style/Theme.SJDialog"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginHorizontal="5dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
android:autoSizeMinTextSize="5dp"
android:autoSizeStepGranularity="1dp"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="Btn"
/>
4 changes: 2 additions & 2 deletions SJDialog/src/main/res/layout/custom_view_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
android:id="@+id/btn1"
style="@style/SJDialog.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
Expand All @@ -84,7 +84,7 @@
android:id="@+id/btn2"
style="@style/SJDialog.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
android:autoSizeMinTextSize="5dp"
Expand Down
5 changes: 3 additions & 2 deletions SJDialog/src/main/res/layout/dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
android:id="@+id/btn1"
style="@style/SJDialog.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
Expand All @@ -69,8 +69,9 @@
<Button
android:id="@+id/btn2"
style="@style/SJDialog.ButtonStyle"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
android:autoSizeMinTextSize="5dp"
Expand Down
4 changes: 2 additions & 2 deletions SJDialog/src/main/res/layout/list_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
android:id="@+id/btn1"
style="@style/SJDialog.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
Expand All @@ -81,7 +81,7 @@
android:id="@+id/btn2"
style="@style/SJDialog.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
android:autoSizeMinTextSize="5dp"
Expand Down
2 changes: 1 addition & 1 deletion SJDialog/src/main/res/layout/message_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
android:id="@+id/btn"
style="@style/SJDialog.ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:autoSizeMaxTextSize="14dp"
Expand Down
5 changes: 3 additions & 2 deletions SJDialog/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- "@android:style/Widget.Button"/-->
<style name="SJDialog.ButtonStyle" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/ripple_button</item>
<item name="android:textSize">12dp</item>
<item name="android:paddingStart">15dp</item>
<item name="android:paddingEnd">15dp</item>
<item name="android:padding">14dp</item>
<item name="textAllCaps">true</item>
<!-- <item name="android:padding">14dp</item>-->
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="textAllCaps">true</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
<item name="android:textColor">?colorOnPrimary</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
Expand Down

0 comments on commit c0e1cde

Please sign in to comment.