From 861c4faf3f820302296bb98c9eb6d482344dc4a0 Mon Sep 17 00:00:00 2001 From: anggastudio Date: Thu, 8 Oct 2020 18:39:37 +0700 Subject: [PATCH] custom button color --- .../com/anggastudio/sample/MainActivity.java | 3 +- .../com/anggastudio/sample/mock/Mock.java | 11 +++- .../anggastudio/sample/model/BitMapModel.java | 1 - .../anggastudio/sample/model/PrintBody.java | 3 -- .../com/anggastudio/sample/util/Util.java | 2 +- .../printama/DeviceListFragment.java | 45 +++++++++++++--- .../com/anggastudio/printama/Printama.java | 51 +++++++++++------- .../com/anggastudio/printama/PrinterUtil.java | 2 +- printama/src/main/res/layout/device_item.xml | 6 ++- .../main/res/layout/fragment_device_list.xml | 53 +++++++++++-------- printama/src/main/res/values/colors.xml | 2 +- 11 files changed, 120 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/anggastudio/sample/MainActivity.java b/app/src/main/java/com/anggastudio/sample/MainActivity.java index 583ff59..5219aec 100644 --- a/app/src/main/java/com/anggastudio/sample/MainActivity.java +++ b/app/src/main/java/com/anggastudio/sample/MainActivity.java @@ -55,7 +55,7 @@ private void getSavedPrinter() { } private void showPrinterList() { - Printama.showPrinterList(this, printerName -> { + Printama.showPrinterList(this, R.color.colorBlue, printerName -> { Toast.makeText(this, printerName, Toast.LENGTH_SHORT).show(); TextView connectedTo = findViewById(R.id.tv_printer_info); String text = "Connected to : " + printerName; @@ -68,6 +68,7 @@ private void showPrinterList() { } private void showPrinterListActivity() { + // only use this when your project is not androidX Printama.showPrinterList(this); } diff --git a/app/src/main/java/com/anggastudio/sample/mock/Mock.java b/app/src/main/java/com/anggastudio/sample/mock/Mock.java index c05dcec..bf43105 100644 --- a/app/src/main/java/com/anggastudio/sample/mock/Mock.java +++ b/app/src/main/java/com/anggastudio/sample/mock/Mock.java @@ -7,9 +7,14 @@ import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Locale; public class Mock { + private Mock() { + + } + public static PrintModel getPrintModelMock() { PrintBody printBody = new PrintBody(); printBody.setDate("17/08/2020"); @@ -21,7 +26,9 @@ public static PrintModel getPrintModelMock() { Date date = new Date(); date.setTime(System.currentTimeMillis()); - printBody.setTimeStamp(new SimpleDateFormat("HH:mm:ss").format(date)); + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); + String format = simpleDateFormat.format(date); + printBody.setTimeStamp(format); PrintFooter printFooter = new PrintFooter(); printFooter.setInitial("INI BUKAN BUKTI PEMBAYARAN SAH"); @@ -31,7 +38,7 @@ public static PrintModel getPrintModelMock() { PrintHeader printHeader = new PrintHeader(); - //set print heeader + //set print header printHeader.setMerchantAddress1("Jalan Mawar No 7"); printHeader.setMerchantAddress2("Cibinong Bogor"); printHeader.setMerchantName("Kedai Juragan Bebek"); diff --git a/app/src/main/java/com/anggastudio/sample/model/BitMapModel.java b/app/src/main/java/com/anggastudio/sample/model/BitMapModel.java index a0d249f..61cc005 100644 --- a/app/src/main/java/com/anggastudio/sample/model/BitMapModel.java +++ b/app/src/main/java/com/anggastudio/sample/model/BitMapModel.java @@ -13,7 +13,6 @@ public class BitMapModel implements Parcelable { public BitMapModel() { } - protected BitMapModel(Parcel in) { bitmapName = in.readString(); bitmapImageBase64 = in.readString(); diff --git a/app/src/main/java/com/anggastudio/sample/model/PrintBody.java b/app/src/main/java/com/anggastudio/sample/model/PrintBody.java index 221ff94..91add76 100644 --- a/app/src/main/java/com/anggastudio/sample/model/PrintBody.java +++ b/app/src/main/java/com/anggastudio/sample/model/PrintBody.java @@ -12,9 +12,6 @@ public class PrintBody { String batchNum; String timeStamp; - public PrintBody() { - } - public String getInvoice() { return invoice; } diff --git a/app/src/main/java/com/anggastudio/sample/util/Util.java b/app/src/main/java/com/anggastudio/sample/util/Util.java index 987cea3..ede78b5 100644 --- a/app/src/main/java/com/anggastudio/sample/util/Util.java +++ b/app/src/main/java/com/anggastudio/sample/util/Util.java @@ -18,7 +18,7 @@ public static Bitmap decodeBase64(String base64) { } public static Bitmap getQrCode(String qrCode) { - // please see the dependencies to create QRCode + // please see the dependency to create QRCode // it's not included in Printama return QRCode.from(qrCode).bitmap(); } diff --git a/printama/src/main/java/com/anggastudio/printama/DeviceListFragment.java b/printama/src/main/java/com/anggastudio/printama/DeviceListFragment.java index be65de6..6ac2ae4 100644 --- a/printama/src/main/java/com/anggastudio/printama/DeviceListFragment.java +++ b/printama/src/main/java/com/anggastudio/printama/DeviceListFragment.java @@ -24,6 +24,8 @@ public class DeviceListFragment extends DialogFragment { private String mPrinterName; private Button saveButton; private Button testButton; + private int inactiveColor; + private int activeColor; public DeviceListFragment() { // Required empty public constructor @@ -58,7 +60,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat saveButton = view.findViewById(R.id.btn_save_printer); saveButton.setOnClickListener(v -> savePrinter()); mPrinterName = Pref.getString(Pref.SAVED_DEVICE); - toggleButtons(); RecyclerView rvDeviceList = view.findViewById(R.id.rv_device_list); rvDeviceList.setLayoutManager(new LinearLayoutManager(getContext())); @@ -71,17 +72,38 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat }); } + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + setColor(); + toggleButtons(); + } + + private void setColor() { + if (getContext() != null) { + if (this.activeColor == 0) { + this.activeColor = ContextCompat.getColor(getContext(), R.color.colorGreen); + } + if (this.inactiveColor == 0) { + this.inactiveColor = ContextCompat.getColor(getContext(), R.color.colorGray5); + } + } + + } + private void testPrinter() { Printama.with(getActivity(), mPrinterName).printTest(); } private void toggleButtons() { - if (mPrinterName != null) { - testButton.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorGreen)); - saveButton.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorGreen)); - } else { - testButton.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorGray5)); - saveButton.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorGray5)); + if (getContext() != null) { + if (mPrinterName != null) { + testButton.setBackgroundColor(activeColor); + saveButton.setBackgroundColor(activeColor); + } else { + testButton.setBackgroundColor(inactiveColor); + saveButton.setBackgroundColor(inactiveColor); + } } } @@ -92,4 +114,13 @@ private void savePrinter() { } dismiss(); } + + public void setColorTheme(int activeColor, int inactiveColor) { + if (activeColor != 0) { + this.activeColor = activeColor; + } + if (inactiveColor != 0) { + this.inactiveColor = inactiveColor; + } + } } diff --git a/printama/src/main/java/com/anggastudio/printama/Printama.java b/printama/src/main/java/com/anggastudio/printama/Printama.java index 5d01bfb..3f12114 100644 --- a/printama/src/main/java/com/anggastudio/printama/Printama.java +++ b/printama/src/main/java/com/anggastudio/printama/Printama.java @@ -92,6 +92,37 @@ private static BluetoothDevice getPrinter(String printerName) { return printer; } + public static void showPrinterList(FragmentActivity activity, OnConnectPrinter onConnectPrinter) { + showPrinterList(activity, 0, 0, onConnectPrinter); + } + + public static void showPrinterList(FragmentActivity activity, int activeColor, OnConnectPrinter onConnectPrinter) { + showPrinterList(activity, activeColor, 0, onConnectPrinter); + } + + public static void showPrinterList(FragmentActivity activity, int activeColor, int inactiveColor, OnConnectPrinter onConnectPrinter) { + Pref.init(activity); + BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter(); + int activeColorResource = activeColor == 0 ? activeColor : ContextCompat.getColor(activity, activeColor); + int inactiveColorResource = inactiveColor == 0 ? inactiveColor : ContextCompat.getColor(activity, inactiveColor); + if (defaultAdapter != null && !defaultAdapter.getBondedDevices().isEmpty()) { + FragmentManager fm = activity.getSupportFragmentManager(); + DeviceListFragment fragment = DeviceListFragment.newInstance(); + fragment.setDeviceList(defaultAdapter.getBondedDevices()); + fragment.setOnConnectPrinter(onConnectPrinter); + fragment.setColorTheme(activeColorResource, inactiveColorResource); + fragment.show(fm, "DeviceListFragment"); + } else { + onConnectPrinter.onConnectPrinter("failed to connect printer"); + } + } + + public static void showPrinterList(Activity activity) { + Pref.init(activity); + Intent intent = new Intent(activity, ChoosePrinterActivity.class); + activity.startActivityForResult(intent, Printama.GET_PRINTER_CODE); + } + public void printText(String text) { printText(LEFT, text); } @@ -148,26 +179,6 @@ public boolean printImage(Bitmap bitmap, int width) { return util.printImage(bitmap, width); } - public static void showPrinterList(FragmentActivity activity, OnConnectPrinter onConnectPrinter) { - Pref.init(activity); - BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter(); - if (defaultAdapter != null && !defaultAdapter.getBondedDevices().isEmpty()) { - FragmentManager fm = activity.getSupportFragmentManager(); - DeviceListFragment fragment = DeviceListFragment.newInstance(); - fragment.setDeviceList(defaultAdapter.getBondedDevices()); - fragment.setOnConnectPrinter(onConnectPrinter); - fragment.show(fm, "DeviceListFragment"); - } else { - onConnectPrinter.onConnectPrinter("failed to connect printer"); - } - } - - public static void showPrinterList(Activity activity) { - Pref.init(activity); - Intent intent = new Intent(activity, ChoosePrinterActivity.class); - activity.startActivityForResult(intent, Printama.GET_PRINTER_CODE); - } - public void printDashedLine() { util.printText("--------------------------------"); } diff --git a/printama/src/main/java/com/anggastudio/printama/PrinterUtil.java b/printama/src/main/java/com/anggastudio/printama/PrinterUtil.java index 743a9cb..fd0baa9 100644 --- a/printama/src/main/java/com/anggastudio/printama/PrinterUtil.java +++ b/printama/src/main/java/com/anggastudio/printama/PrinterUtil.java @@ -295,7 +295,7 @@ private ConnectAsyncTask(ConnectionListener listener) { @Override protected BluetoothSocket doInBackground(BluetoothDevice... bluetoothDevices) { BluetoothDevice device = bluetoothDevices[0]; - UUID uuid = null; + UUID uuid; if (device != null) { uuid = device.getUuids()[0].getUuid(); } else { diff --git a/printama/src/main/res/layout/device_item.xml b/printama/src/main/res/layout/device_item.xml index c0f422c..23552c6 100644 --- a/printama/src/main/res/layout/device_item.xml +++ b/printama/src/main/res/layout/device_item.xml @@ -3,7 +3,10 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="?android:attr/selectableItemBackground" + android:layout_marginStart="2dp" + android:layout_marginTop="1dp" + android:layout_marginEnd="2dp" + android:background="@color/colorWhite" android:foreground="?android:attr/selectableItemBackground" android:orientation="horizontal"> @@ -13,6 +16,7 @@ android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" + android:textColor="@color/colorBlack" tools:text="device name" /> @@ -27,31 +29,40 @@ android:id="@+id/rv_device_list" android:layout_width="match_parent" android:layout_height="wrap_content" - android:paddingStart="14dp" - android:paddingEnd="14dp" + android:paddingStart="8dp" + android:paddingEnd="8dp" tools:listitem="@layout/device_item" /> -