Skip to content

Commit

Permalink
Fix the bug that button text overflow when the device is over lollipop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shintaro Katafuchi committed Jun 28, 2015
1 parent 45cbdad commit 38da532
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
16 changes: 7 additions & 9 deletions library/src/main/java/hotchemi/android/rate/DialogManager.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package hotchemi.android.rate;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.View;
import android.view.Window;

import static hotchemi.android.rate.IntentHelper.createIntentForGooglePlay;
import static hotchemi.android.rate.PreferenceHelper.setAgreeShowDialog;
import static hotchemi.android.rate.PreferenceHelper.setRemindInterval;
import static hotchemi.android.rate.Utils.getDialogBuilder;

final class DialogManager {

private DialogManager() {
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
static Dialog create(final Context context, boolean isShowNeutralButton,
boolean isShowTitle, final OnClickButtonListener listener, View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog.Builder builder = getDialogBuilder(context);
builder.setMessage(R.string.rate_dialog_message);
if (isShowTitle) builder.setTitle(R.string.rate_dialog_title);
if (view != null) builder.setView(view);
builder.setPositiveButton(R.string.rate_dialog_ok, new DialogInterface.OnClickListener() {
@Override
Expand All @@ -45,13 +49,7 @@ public void onClick(DialogInterface dialog, int which) {
if (listener != null) listener.onClickButton(which);
}
});
AlertDialog dialog = builder.create();
if (!isShowTitle) {
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
} else {
dialog.setTitle(R.string.rate_dialog_title);
}
return dialog;
return builder.create();
}

}
34 changes: 34 additions & 0 deletions library/src/main/java/hotchemi/android/rate/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package hotchemi.android.rate;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;

final class Utils {

private Utils() {
}

static boolean underHoneyComb() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
}

static boolean overLollipop() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}

static int getDialogTheme() {
return overLollipop() ? R.style.CustomLollipopDialogStyle : 0;
}

@SuppressLint("NewApi")
static AlertDialog.Builder getDialogBuilder(Context context) {
if (underHoneyComb()) {
return new AlertDialog.Builder(context);
} else {
return new AlertDialog.Builder(context, getDialogTheme());
}
}

}
26 changes: 26 additions & 0 deletions library/src/main/res/values-v21/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="CustomLollipopDialogStyle" parent="android:Theme.Material.Light.Dialog.Alert">
<item name="android:buttonBarButtonStyle">@style/CustomButtonBarButtonStyle</item>
<item name="android:buttonBarStyle">@style/CustomButtonBarStyle</item>
</style>

<style name="CustomButtonBarStyle" parent="@android:style/Widget.Material.Light.ButtonBar.AlertDialog">
<!-- Making sure, the button bar uses parent width and is not restricted in height -->
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:height">@null</item>
<item name="android:minHeight">@null</item>
</style>

<style name="CustomButtonBarButtonStyle" parent="@android:style/Widget.Material.Light.Button.Borderless.Colored">
<!-- Setting the weight as follows should result in equally wide buttons filling the alert dialog width,
but instead they span further out of the dialog, breaking in multiple lines though -->
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<!-- TODO: setting a fixed width as follows results in narrow buttons with line breaks, but of course this is not a solution -->
<item name="android:width">100dp</item>
</style>

</resources>

0 comments on commit 38da532

Please sign in to comment.