Reuse one class to avoid extra coding, for example, how we can make one class and use it multiple times in the project.
RecyclerView
import android.support.v7.widget.RecyclerView; public abstract class RecyclerViewAdapter extends RecyclerView.Adapter { public abstract void add(Object object); public abstract void clear(); }
Activity
import android.support.v7.app.AppCompatActivity; public abstract class UpasargaActivity extends AppCompatActivity { abstract protected void initialiseViews(); abstract protected void initialiseListener(); }
Toast
public class showToast {
public static Toast showToastWithMessage(String messageResourceId) {
Toast toast = Toast.makeText(UpasargaApplication.getAppContext(), messageResourceId, Toast.LENGTH_SHORT);
toast.show();
return toast;
}
public static Toast showLongToastWithMessage(String messageResourceId) {
Toast toast = Toast.makeText(UpasargaApplication.getAppContext(), messageResourceId, Toast.LENGTH_LONG);
toast.show();
return toast;
}
}
Locale Helper
import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; import android.preference.PreferenceManager; import com.upasarga.elibrary.constants.SharedPrefConstants; import java.util.Locale; public class LocaleHelper { public static void onCreate(Context context) { String lang = getPersistedData(context, Locale.getDefault().getLanguage()); setLocale(context, lang); } public static void onCreate(Context context, String defaultLanguage) { String lang = getPersistedData(context, defaultLanguage); setLocale(context, lang); } public static String getLanguage(Context context) { return getPersistedData(context, Locale.getDefault().getLanguage()); } public static void setLocale(Context context, String language) { persist(context, language); updateResources(context, language); } private static String getPersistedData(Context context, String defaultLanguage) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); return preferences.getString(SharedPrefConstants.SELECTED_LANGUAGE, defaultLanguage); } private static void persist(Context context, String language) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = preferences.edit(); editor.putString(SharedPrefConstants.SELECTED_LANGUAGE, language); editor.apply(); } private static void updateResources(Context context, String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); configuration.locale = locale; resources.updateConfiguration(configuration, resources.getDisplayMetrics()); } }
Generic Type
public class GetType {
public static <T> T getType(Object type, Class<T> classes) {
try {
return classes.cast(type);
} catch (ClassCastException ex) {
return null;
}
}
}
Thank you! It’s all about code.
No comments:
Post a Comment