简体   繁体   中英

Android Flat Buttons not showing in Dialogs

I have been trying to design a dialog to rate my game. I have designed a XML file that looks like this -

Rate App XML Design

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F5F5F5">

<LinearLayout
    android:id="@+id/LL1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="24dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"
        android:text="Rate My game"
        android:textColor="#000000"
        android:textSize="24sp" />

</LinearLayout>

<View
    android:id="@+id/singleLine"
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_below="@id/LL1"
    android:layout_marginBottom="8dp"
    android:background="#2196F3" />


<TextView
    android:id="@+id/Description_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/singleLine"
    android:layout_marginBottom="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="12dp"
    android:text="If you enjoy Playing My Game take a moment to rate 
    it.   Thanks for your Support!"
    android:textColor="#000000"
    android:textSize="16sp" />


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/Description_text"

    android:layout_margin="8dp">

    <Button
        android:id="@+id/rate_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="RATE"
        android:textAllCaps="true"
        android:textColor="#424242"
        android:textSize="14sp"

        />

    <Button
        android:id="@+id/remind_me_later_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:layout_weight="4"
        android:text="REMIND ME LATER"
        android:textAllCaps="true"
        android:textColor="#424242"
        android:textSize="14sp"

        />

    <Button
        android:id="@+id/never_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:layout_weight="2"
        android:text="NEVER"
        android:textAllCaps="true"
        android:textColor="#424242"
        android:textSize="14sp"

        />


</LinearLayout>

But When using it as dialog the flat button somehow converts to regular button like this-

Flat button in XML

Java code for displaying it -

public class AppRater extends AppCompatActivity {
private final static String APP_TITLE = "My Game";
private final static String APP_PNAME = "com.alala.blabla";

private final static int DAYS_UNTIL_PROMPT = 0;
private final static int LAUNCHES_UNTIL_PROMPT = 1;

public static void app_launched(Context mContext) {
    SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
    if (prefs.getBoolean("dontshowagain", false)) {
        return;
    }

    SharedPreferences.Editor editor = prefs.edit();

    // Increment launch counter
    long launch_count = prefs.getLong("launch_count", 0) + 1;
    editor.putLong("launch_count", launch_count);

    // Get date of first launch
    Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
    if (date_firstLaunch == 0) {
        date_firstLaunch = System.currentTimeMillis();
        editor.putLong("date_firstlaunch", date_firstLaunch);
    }

    // Wait at least n days before opening
    if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
        if (System.currentTimeMillis() >= date_firstLaunch +
                (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
            showRateDialog(mContext, editor);
        }
    }

    editor.commit();
}

public static void showRateDialog(final Context mContext, 
final SharedPreferences.Editor editor) {


    final Dialog dialog = new Dialog(mContext, R.style.FullHeightDialog);
    dialog.setContentView(R.layout.customapprater);
    Button b1 = (Button) dialog.findViewById(R.id.rate_button);
    b1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (editor != null) {
                editor.putBoolean("dontshowagain", true);
                editor.commit();
            }
            mContext.startActivity(new Intent(
 Intent.ACTION_VIEW,     Uri.parse("market://details?id=" + APP_PNAME)));
            dialog.dismiss();
        }
    });


    Button b2 = (Button) dialog.findViewById(R.id.remind_me_later_button);
    b2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (editor != null) {
                editor.clear().commit();
            }
            dialog.dismiss();
        }
    });


    Button b3 = (Button) dialog.findViewById(R.id.never_button);

    b3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (editor != null) {
                editor.putBoolean("dontshowagain", true);
                editor.commit();
            }
            dialog.dismiss();
        }
    });
    dialog.show();
}
}

What I am doing wrong?

Please, change your dialog to AlertDialog. You can read about them here .

public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
    new AlertDialog.Builder(mContext)
            .setTitle(R.string.rate_dialog_title)
            .setMessage(R.string.rate_dialog_message)
            .setPositiveButton(R.string.rate, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (editor != null) {
                        editor.putBoolean("dontshowagain", true);
                        editor.commit();
                    }

                    mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
                }
            })
            .setNegativeButton(R.string.never, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (editor != null) {
                        editor.putBoolean("dontshowagain", true);
                        editor.commit();
                    }
                }
            })
            .setNeutralButton(R.string.remind_later, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (editor != null) {
                        editor.clear().commit();
                    }
                }
            })
            .show();
}

More info about AlertDialog.Builder .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM