How to add methods that I often use for android studio?

For instance,

public void show_message(String message){
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

I want this method to add auto Activity.java when creating a new action or java class.

I want to save various methods like this and quickly include it in my project where necessary.

+4
source share
4 answers

You just need to create the Common Utilities class . Just copy and paste the class into any project you use. Just specify access qualifiers as public staic so you can easily access it. For instance,

CommonUtilities.showToastMessage(String text);
+3
source

, BaseActivity BaseActivity. , . Github . MVP.

BaseActivity.

+7

, . , :

public class Config {
    public Context context;
    public String sharedPrefsName;
    public String carTablesName, carsTableCarColumn, databaseName;
    public int databaseNewVersion, databaseOldVersion;
    public boolean showNotificationsToCustomer;
    public String customerNotificationState;
    public String userMobile;
    public SharedPreferences preferences;
    public String customerChatTableName;
    public String customerChatMessageColumn;
    public String customerChatSentByCustomerColumn;
    public String customerChatTimeColumn;
    public String loggedInUserId;
    public String loggedInUserName;
    public String customerChatSupportNotifyingUrl;

    public Config(Context context) {
        this.context = context;
        customerChatSupportNotifyingUrl = "";
        customerChatTableName = "customerChat";
        customerChatMessageColumn = "customerMessage";
        customerChatTimeColumn = "sentOn";
        customerChatSentByCustomerColumn = "isSentByCustomer";
        sharedPrefsName = context.getString(R.string.shared_prefs_login_validator);
        preferences = context.getSharedPreferences(sharedPrefsName, Context.MODE_PRIVATE);
        customerNotificationState = context.getString(R.string.customer_notification_state);
        showNotificationsToCustomer = preferences.getBoolean(customerNotificationState, true);
        carTablesName = context.getString(R.string.user_car_table);
        carsTableCarColumn = context.getString(R.string.user_car_table_car_column);
        databaseName = context.getString(R.string.user_db);
        databaseNewVersion = 3;
        databaseOldVersion = 1;
        loggedInUserId = preferences.getString(context.getString(R.string.user_db), "");
        userMobile = preferences.getString(context.getString(R.string.user_mobile), "");
        loggedInUserName = preferences.getString(context.getString(R.string.user_name), "");
    }
}

, . , .

, :

public class MyProgressDialog extends ProgressDialog {
    String title, message;

    public MyProgressDialog(Context context, String title, String message) {
        super(context);
        if (!title.equals("")) this.setTitle(title);
        this.setMessage(message);
        this.setCancelable(false);
        this.setIndeterminate(false);
    }
}

, , ProgressDialog. , .

Similarly for toast you can do the same. If you want them to appear when creating an activity, just save this:

MyProgressDialog dialog=new MyProgressDialog(this,"title","message");
dialog.show();

in your onCreate () function. You can do the same for the toast.

If this is a java class, just create a constructor and save this fragment in this constructor.

0
source

You need to read about the “File Templates” https://riggaroo.co.za/custom-file-templates-android-studio/ this great topic, but it's worth it.

0
source

Source: https://habr.com/ru/post/1682427/


All Articles