Java
Shared Preferences
/**
* Thread'den bağımsız data senkronizasyonu için oluşturuldu
*/
public class SysPref {
private static SysPref sysPref;
private static Context instance;
private SharedPreferences sp;
public static void init(Context context) {
sysPref = new SysPref();
instance = context;
if (sysPref.sp == null) {
sysPref.sp = PreferenceManager.getDefaultSharedPreferences(context);
}
}
private static void checkfornull() {
if (sysPref == null)
throw new NullPointerException("Call init() method in application class");
}
//putString
public static void put(String key, String value) {
checkfornull();
try {
sysPref.sp.edit().putString(key, value).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//putStringSet
public static void put(String key, Set<String> value) {
checkfornull();
try {
sysPref.sp.edit().putStringSet(key, value).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//putInt
public static void put(String key, int value) {
checkfornull();
try {
sysPref.sp.edit().putInt(key, value).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//putLong
public static void put(String key, long value) {
checkfornull();
try {
sysPref.sp.edit().putLong(key, value).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//putFloat
public static void put(String key, float value) {
checkfornull();
try {
sysPref.sp.edit().putFloat(key, value).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//putBoolean
public static void put(String key, boolean value) {
checkfornull();
try {
sysPref.sp.edit().putBoolean(key, value).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//putObject or arrayList
public static void put(String key, Object value) {
checkfornull();
try {
Gson gson = new GsonBuilder().create();
sysPref.sp.edit().putString(key, gson.toJson(value).toString()).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//getString
public static String getString(String key, String defaultvalue) {
checkfornull();
try {
return sysPref.sp.getString(key, defaultvalue);
} catch (Exception e) {
e.printStackTrace();
return defaultvalue;
}
}
public static String getString(String key) {
return getString(key, "");
}
//getStringSet
public static Set<String> getStringSet(String key, Set<String> defaultvalue) {
checkfornull();
try {
return sysPref.sp.getStringSet(key, defaultvalue);
} catch (Exception e) {
e.printStackTrace();
return defaultvalue;
}
}
public static Set<String> getStringSet(String key) {
return getStringSet(key, null);
}
//getInt
public static int getInt(String key, int defaultvalue) {
checkfornull();
try {
return sysPref.sp.getInt(key, defaultvalue);
} catch (Exception e) {
e.printStackTrace();
return defaultvalue;
}
}
public static int getInt(String key) {
return getInt(key, 0);
}
//getLong
public static long getLong(String key, long defaultvalue) {
checkfornull();
try {
return sysPref.sp.getLong(key, defaultvalue);
} catch (Exception e) {
e.printStackTrace();
return defaultvalue;
}
}
public static long getLong(String key) {
return getLong(key, (long) 0);
}
//getFloat
public static float getFloat(String key, float defaultvalue) {
checkfornull();
try {
return sysPref.sp.getFloat(key, defaultvalue);
} catch (Exception e) {
e.printStackTrace();
return defaultvalue;
}
}
public static float getFloat(String key) {
return getFloat(key, 0.0f);
}
//getBoolean
public static boolean getBoolean(String key, boolean defaultvalue) {
checkfornull();
try {
return sysPref.sp.getBoolean(key, defaultvalue);
} catch (Exception e) {
e.printStackTrace();
return defaultvalue;
}
}
public static boolean getBoolean(String key) {
return getBoolean(key, false);
}
//getObject
public static <T> Object getObject(String key, Class<?> tClass) {
checkfornull();
try {
Gson gson = new GsonBuilder().create();
return gson.fromJson(sysPref.sp.getString(key, ""), tClass);
} catch (Exception e) {
Log.e("gson", e.getMessage());
return "";
}
}
//getArrayList
public static <T> ArrayList<T> getArrayList(String key, Class<?> tClass) {
Log.e("_+_++__+_+", "" + tClass.getName());
Gson gson = new Gson();
String data = sysPref.sp.getString(key, "");
if (!data.trim().isEmpty()) {
Type type = new GenericType(tClass);
return (ArrayList<T>) gson.fromJson(data, type);
}
return new ArrayList<T>();
}
//clear single value
public static void clear(String key) {
checkfornull();
try {
sysPref.sp.edit().remove(key).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
//clear all preference
public static void clearAll() {
checkfornull();
try {
sysPref.sp.edit().clear().apply();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addListener(StashListener listener){
checkfornull();
sysPref.sp.registerOnSharedPreferenceChangeListener(listener);
}
public static void removeListener(StashListener listener){
checkfornull();
sysPref.sp.unregisterOnSharedPreferenceChangeListener(listener);
}
private static class GenericType implements ParameterizedType {
private Type type;
GenericType(Type type) {
this.type = type;
}
@Override
public Type[] getActualTypeArguments() {
return new Type[]{type};
}
@Override
public Type getRawType() {
return ArrayList.class;
}
@Override
public Type getOwnerType() {
return null;
}
// implement equals method too! (as per javadoc)
}
public interface StashListener extends SharedPreferences.OnSharedPreferenceChangeListener {
}
}
Toast
public class ToastClass extends AppCompatActivity{
ImageView toast_image;
TextView toast_text;
LinearLayout custom_toast;
View toastView;
String status;
public void toastIconSuccess(final AppCompatActivity activity) {
Toast toast = new Toast(activity.getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
View custom_view = activity.getLayoutInflater().inflate(R.layout.toast_icon_text, null);
((TextView) custom_view.findViewById(R.id.message)).setText("Success!");
((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_done);
((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(activity.getResources().getColor(R.color.green_500));
toast.setView(custom_view);
toast.show();
}
public void toastIconSuccess(final AppCompatActivity activity,String message) {
Toast toast = new Toast(activity.getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
View custom_view = activity.getLayoutInflater().inflate(R.layout.toast_icon_text, null);
((TextView) custom_view.findViewById(R.id.message)).setText(message);
((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_done);
((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(activity.getResources().getColor(R.color.green_500));
toast.setView(custom_view);
toast.show();
}
public void toastIconError(final AppCompatActivity activity,String message) {
Toast toast = new Toast(activity.getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
View custom_view = activity.getLayoutInflater().inflate(R.layout.toast_icon_text, null);
((TextView) custom_view.findViewById(R.id.message)).setText(message);
((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_close );
((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(activity.getResources().getColor(R.color.red_500));
toast.setView(custom_view);
toast.show();
}
public void toastIconFail(final AppCompatActivity activity,String message) {
Toast toast = new Toast(activity.getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
View custom_view = activity.getLayoutInflater().inflate(R.layout.toast_icon_text, null);
((TextView) custom_view.findViewById(R.id.message)).setText(message);
((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_error_outline );
((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(activity.getResources().getColor(R.color.red_500));
toast.setView(custom_view);
toast.show();
}
public void toastIconFail(final Fragment fragment, String message) {
Toast toast = new Toast(fragment.getContext());
toast.setDuration(Toast.LENGTH_LONG);
View custom_view = fragment.getLayoutInflater().inflate(R.layout.toast_icon_text, null);
((TextView) custom_view.findViewById(R.id.message)).setText(message);
((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_error_outline );
((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(fragment.getResources().getColor(R.color.red_500));
toast.setView(custom_view);
toast.show();
}
public void createCustomToast(final AppCompatActivity activity, String Message) {
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
toastView = layoutInflater.inflate(R.layout.fragment_custom_toast, null);
custom_toast=(LinearLayout)toastView.findViewById(R.id.custom_toast);
toast_image = (ImageView) toastView.findViewById(R.id.toast_image);
toast_text = (TextView) toastView.findViewById(R.id.toast_text);
toast_text.setText(Message);
toast_image.setImageResource(R.drawable.user);
custom_toast.setBackgroundResource(R.color.green_500);
Toast toast = new Toast(activity.getApplicationContext());
//toast.setImageResource(toast_image);
toast.setView(toastView);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
Validation
public class ValidationHelper extends Application {
public boolean textBoxisEmpty(AutoCompleteTextView actv) {
if (actv.getText().toString().trim().isEmpty()) {
actv.setError("BOŞ BIRAKILAMAZ!");
return false;
} else {
actv.setError(null);
return true;
}
}
public boolean textBoxisEmpty(EditText et) {
if (et.getText().toString().trim().isEmpty()) {
et.setError("BOŞ BIRAKILAMAZ!");
return false;
} else {
et.setError(null);
return true;
}
}
public boolean valueisZero(AutoCompleteTextView actv) {
DecimalFormat format = new DecimalFormat("0.##");
String value = format.format(Float.valueOf(actv.getText().toString().trim()));
if (value.equals("0")) {
actv.setError("BU DEĞER SIFIR OLAMAZ!");
return false;
} else {
actv.setError(null);
return true;
}
}
public boolean valueisZero(EditText et) {
DecimalFormat format = new DecimalFormat("0.##");
String value = format.format(Float.valueOf(et.getText().toString().trim()));
if (value.equals("0")) {
et.setError("BU DEĞER SIFIR OLAMAZ!");
return false;
} else {
et.setError(null);
return true;
}
}
public boolean valueisDot(AutoCompleteTextView actv) {
if (actv.getText().toString().trim().equals(".")) {
actv.setError("BU DEĞER NOKTA OLAMAZ!");
return false;
} else {
actv.setError(null);
return true;
}
}
public boolean valueisDot(EditText et) {
if (et.getText().toString().trim().equals(".")) {
et.setError("BU DEĞER NOKTA OLAMAZ!");
return false;
} else {
et.setError(null);
return true;
}
}
public boolean emailisCorrect(AutoCompleteTextView actv) {
if (actv.getText().toString().trim().isEmpty()) {
actv.setError("BOŞ BIRAKILAMAZ!");
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(actv.getText().toString().trim()).matches()) {
actv.setError("LÜTFEN GEÇERLİ BİR E-MAİL GİRİNİZ!");
return false;
} else {
actv.setError(null);
return true;
}
}
}
Scanner
public class QRCodeScanner implements SurfaceHolder.Callback, Camera.PreviewCallback {
private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
private final Context context;
private final SurfaceView cameraPreview;
private Camera camera;
private final BarcodeScanner barcodeScanner;
private final QRCodeListener qrCodeListener;
public QRCodeScanner(Context context, SurfaceView cameraPreview, QRCodeListener qrCodeListener) {
this.context = context;
this.cameraPreview = cameraPreview;
this.qrCodeListener = qrCodeListener;
cameraPreview.getHolder().addCallback(this);
barcodeScanner = BarcodeScanning.getClient(new BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_ALL_FORMATS)
.build());
}
public void startCamera() {
if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
} else {
initCamera();
}
}
public void stopCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
private void initCamera() {
camera = Camera.open();
if (camera != null) {
Camera.Parameters parameters = camera.getParameters();
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(cameraPreview.getHolder());
camera.setPreviewCallback(this);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
startCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Do nothing
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
stopCamera();
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
InputImage image = InputImage.fromByteArray(data, camera.getParameters().getPreviewSize().width, camera.getParameters().getPreviewSize().height, Camera.CameraInfo.CAMERA_FACING_BACK, InputImage.IMAGE_FORMAT_NV21);
barcodeScanner.process(image)
.addOnSuccessListener(barcodeList -> {
if (!barcodeList.isEmpty()) {
Barcode barcode = barcodeList.get(0);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
qrCodeListener.onQRCodeDetected(barcode.getRawValue());
}
})
.addOnFailureListener(e -> {
// QR code not detected or an error occurred
});
}
public interface QRCodeListener {
void onQRCodeDetected(String qrCode);
}
}
public class QRCodeScanActivity extends Activity implements QRCodeScanner.QRCodeListener {
private QRCodeScanner qrCodeScanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qr_code_scan);
SurfaceView cameraPreview = findViewById(R.id.cameraPreview);
qrCodeScanner = new QRCodeScanner(this, cameraPreview, this);
}
@Override
protected void onResume() {
super.onResume();
qrCodeScanner.startCamera();
}
@Override
protected void onPause() {
super.onPause();
qrCodeScanner.stopCamera();
}
@Override
public void onQRCodeDetected(String qrCode) {
// QR kodu tespit edildiğinde yapılması gereken işlemleri burada gerçekleştirin.
System.out.println("################ " + qrCode);
qrCodeScanner.stopCamera();
Intent resultIntent = new Intent();
resultIntent.putExtra(SharedPrefKey.SCANNER_DATA, qrCode);
setResult(RESULT_OK, resultIntent);
finish();
}
}
Intent intent = new Intent(MainActivity.this, QRCodeScanActivity.class);
startActivityForResult(intent, REQUEST_CODE);
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
if (arg0 == REQUEST_CODE && arg1 == RESULT_OK) {
if (arg2 != null) {
String scannerData = arg2.getStringExtra(SharedPrefKey.SCANNER_DATA);
}
}
}
Network Utils
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkUtils {
private Context context;
private NetworkChangeListener listener;
private NetworkChangeReceiver networkChangeReceiver;
public NetworkUtils(Context context, NetworkChangeListener listener) {
this.context = context;
this.listener = listener;
networkChangeReceiver = new NetworkChangeReceiver();
}
public boolean hasInternetConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
}
public void startListening() {
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(networkChangeReceiver, filter);
}
public void stopListening() {
context.unregisterReceiver(networkChangeReceiver);
}
private class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
boolean isConnected = hasInternetConnection();
listener.onNetworkChanged(isConnected);
}
}
}
public interface NetworkChangeListener {
void onNetworkChanged(boolean isConnected);
}
}
Custom Banner
public class CustomBanner {
private final LinearLayout bannerLayout;
private final TextView tv_net_marquee;
public CustomBanner(Activity activity) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View bannerView = inflater.inflate(R.layout.banner_layout, null);
bannerLayout = bannerView.findViewById(R.id.bannerLayout);
tv_net_marquee = bannerView.findViewById(R.id.tv_net_marquee);
activity.addContentView(bannerView, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
));
updateBannerVisibility(false,"");
}
public void updateBannerVisibility(boolean isConnected,String bannerText) {
if (isConnected) {
bannerLayout.setVisibility(View.GONE);
} else {
bannerLayout.setVisibility(View.VISIBLE);
tv_net_marquee.setText( bannerText);
tv_net_marquee.setSelected(true);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bannerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FF0000"
android:padding="8dp"
android:visibility="gone">
<TextView
android:id="@+id/tv_net_marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center_horizontal"/>
</LinearLayout>
Observable Array List
public class ObservableArrayList<T> extends ArrayList<T> {
private ArrayListChangeListener listener;
public void setListener(ArrayListChangeListener listener) {
this.listener = listener;
}
@Override
public boolean add(T item) {
boolean result = super.add(item);
if (listener != null) {
listener.onArrayListChanged();
}
return result;
}
@Override
public boolean remove(Object item) {
boolean result = super.remove(item);
if (listener != null) {
listener.onArrayListChanged();
}
return result;
}
// Diğer ArrayList metodlarını da gerektiği gibi override edebilirsiniz
public interface ArrayListChangeListener {
void onArrayListChanged();
}
}
private ObservableArrayList<String> warnings = new ObservableArrayList<>();
warnings.setListener(this::ListeDeğişinceÇağrılacakMetod);