0

I am getting problem with custom dialog, i created custom dialog and it works properly but im having confusion about not showing it again if user click on button. Like there is a button named rating when ever user click on that button then custom dialog will never open again. So if anyone knows how to do it then please help me

This is my code

   final Dialog dialogrul = new Dialog(MainActivity.this);
    dialogrul.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    Window window = dialogrul.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    dialogrul.setContentView(R.layout.exitlayout);
    Button rating = dialogrul.findViewById(R.id.ratingok);
    Button dialogok = dialogrul.findViewById(R.id.exityes);
    final Button dialognotok = dialogrul.findViewById(R.id.exitno);

    dialogrul.setCancelable(true);
    dialogrul.show();
    dialogok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity.this.onSuperBackPressed();
            finish();

        }
    });
    rating.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent j = new Intent(android.content.Intent.ACTION_VIEW);
            j.setData(Uri.parse("https://play.google.com/store/apps/details?"));
            startActivity(j);
        }
    });
    dialognotok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialogrul.cancel();

        }
    });

}
Vadim Kotov
  • 7,766
  • 8
  • 46
  • 61

2 Answers2

1

If you're trying to determine if the user rated your app in the playstore, then there isn't a proper way to do so, and for a good reason. If apps were able to determine the users which rated the app, they could provide rewards for high ratings and 'punishment' for poor ratings.

Instead a good rule of thumb would be to,

  1. Let the user use the app 5 times, to get a good feel of it.
  2. Prompt the user on the 6th run to rate it with options for Yes, Later and Never. Later delays it by 'n' (your choice) days.

Now, to determine if the user chose Yes or Never before, you could store the value in a SharedPreference.

Setting values in Preference:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("rating_choice", user_choice);
 editor.apply();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
String name = prefs.getString("rating_choice", "Later");//"Later" is the default value.

References: 1, 2, 3

Chrisvin Jem
  • 3,706
  • 1
  • 7
  • 24
0

You would probably have to write to a file to store a variable wich says if he clicked on the button

This is to read the file:

private String readFromFile(Context context) {

        String ret = "";

        try {
            InputStream inputStream = context.openFileInput("yourfilename.txt");

            if ( inputStream != null ) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.append(receiveString);
                }

                inputStream.close();
                ret = stringBuilder.toString();
            }
        }
        catch (FileNotFoundException e) { } catch (IOException e) { }

        return ret;
    }

and this is to write to the file:

private void writeToFile(String data,Context context) {
        String x=readFromFile(this);
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("yourfilename.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        }
        catch (IOException e) { }
    }