简体   繁体   中英

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

For example,

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

I want this method add auto Activity.java when create new activity or java class.

I want to save different methods like this and include it in the my project quickly where it is needed.

What you should do is create a BaseActivity and make your activity extend this BaseActivity . Add all the default methods in this activity so you can use them everywhere. You can refer this Github project for reference. It uses MVP.

Here is direct link to BaseActivity .

You just need to make a Common Utilities class. Just copy and paste the class in whatever project you are using it. Just make its method access specifiers as public staic so that you can easily access it. For eg

CommonUtilities.showToastMessage(String text);

What I would do is create a config class and store all these small things in it. For example have a look at this :

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), "");
    }
}

I've placed all the constants in a single file so you need not look at them always. If your app grows in size this would be extremely useful.

For using a progress dialog I use a class like this :

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);
    }
}

This is nothing but a single class that extends ProgressDialog.So you can aquire all the functionalities of the progress dialog class.

Similarly for toast you could do the same. If you want them to appear when the activity gets created simply keep this:

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

in your activity's onCreate() method. You can do the same for toast too.

In case if it is a java class just create a constructor and keep that snippet in that constructor..

您需要阅读“文件模板” https://riggaroo.co.za/custom-file-templates-android-studio/这是一个很大的主题,但这是值得的。

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