O
O
Octavia Melody2015-12-02 19:28:42
Java
Octavia Melody, 2015-12-02 19:28:42

How to replace statistical text with text with Strings.xml?

I searched and searched and couldn't find an answer to my question.
Most people suggest getting text thanks to getText, but private final static doesn't want to be friends with getResources.
If you haven't understood me yet, then I want to replace the value of AppName with the value that will be displayed with Strings.xml:

private final static String APP_TITLE = "App name";

Here is the complete code:
package com.example.ps4;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import android.net.Uri;



public class AppRater {



  private final static String APP_TITLE = "App name";
  private final static String APP_PACKAGE_NAME = "com.dropbox.android";
  

  private final static int DAYS_UNTIL_PROMPT = 0;
  private final static int LAUNCH_UNTIL_PROMPT = 5;
  
  public static void app_launched(Context context) {
    SharedPreferences prefs = context.getSharedPreferences("rate_app", 0);
    if(prefs.getBoolean("dontshowagain", false)){
      return;
    }
    
    SharedPreferences.Editor editor = prefs.edit();
    
    long launch_count = prefs.getLong("launch_count", 0) + 1;
    editor.putLong("launch_count", launch_count);
    
    Long date_firstLaunch = prefs.getLong("date_first_launch", 0);
    if(date_firstLaunch == 0) {
      date_firstLaunch = System.currentTimeMillis();
      editor.putLong("date_first_launch", date_firstLaunch);
    }
    
    if(launch_count >= LAUNCH_UNTIL_PROMPT) {
      
      if(System.currentTimeMillis() >= date_firstLaunch + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)){
        showRateDialog(context, editor);
      }
    }
    editor.commit();
  }
  
  public static void showRateDialog(final Context context, final SharedPreferences.Editor editor) {
    
    Dialog dialog = new Dialog(context);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    
    String message = "If you enjoy using "
        + APP_TITLE 
        + ", please take a moment to rate the app. "
        + "Thank you for your support!";
    
    builder.setMessage(message)
      .setTitle("Rate " + APP_TITLE)
      .setIcon(context.getApplicationInfo().icon)
      .setCancelable(false)
      .setPositiveButton("Rate Now", new DialogInterface.OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
          editor.putBoolean("dontshowagain", true);
          editor.commit();
          

          try {
            context.startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse("market://details?id=" + APP_PACKAGE_NAME)));
          }catch(ActivityNotFoundException e) {

          }
          
          dialog.dismiss();
        }
      })
      .setNeutralButton("Later", new DialogInterface.OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {

          dialog.dismiss();
        }
      })
      .setNegativeButton("No, Thanks", new DialogInterface.OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
          if(editor != null) {
            editor.putBoolean("dontshowagain", true);
            editor.commit();
          }
          

          
          dialog.dismiss();
        }
      });
    
    dialog = builder.create();
    dialog.show();
  }

}

I don't send activity_main, of course, it's just empty.
Thanks in advance for any help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
itdroid, 2015-12-03
@itdroid

IMHO

private final static String APP_TITLE = "App name";
here is too much. And a more correct solution would be to transfer all strings to resources, like this:
<string name="app_name">App name</string>
<string name="rate_dialog_msg">If you enjoy using %1$s, please...</string>

And in the showRateDialog() method use this like this:
String appName = context.getString(R.string.app_name);
String message = context.getString(R.string.rate_dialog_msg, appName);

The recommendation for getting strings from strings.xml applies not only to constants, but to all strings that you intend to display to the user. This will simplify localization in the future, and the code will look cleaner.

F
FoxInSox, 2015-12-02
@FoxInSox

public static void app_launched(Context context) {
  APP_TITLE = context.getString(R.string.text_id);
  //...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question