Skip to content

Commit

Permalink
custom button color
Browse files Browse the repository at this point in the history
  • Loading branch information
anggastudio committed Oct 8, 2020
1 parent b572c0c commit 861c4fa
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 59 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/anggastudio/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -68,6 +68,7 @@ private void showPrinterList() {
}

private void showPrinterListActivity() {
// only use this when your project is not androidX
Printama.showPrinterList(this);
}

Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/anggastudio/sample/mock/Mock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class BitMapModel implements Parcelable {
public BitMapModel() {
}


protected BitMapModel(Parcel in) {
bitmapName = in.readString();
bitmapImageBase64 = in.readString();
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/anggastudio/sample/model/PrintBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public class PrintBody {
String batchNum;
String timeStamp;

public PrintBody() {
}

public String getInvoice() {
return invoice;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/anggastudio/sample/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()));
Expand All @@ -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);
}
}
}

Expand All @@ -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;
}
}
}
51 changes: 31 additions & 20 deletions printama/src/main/java/com/anggastudio/printama/Printama.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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("--------------------------------");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion printama/src/main/res/layout/device_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

Expand All @@ -13,6 +16,7 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:textColor="@color/colorBlack"
tools:text="device name" />

<ImageView
Expand Down
53 changes: 32 additions & 21 deletions printama/src/main/res/layout/fragment_device_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGrayFA"
tools:context=".DeviceListFragment">

<ScrollView
Expand All @@ -19,6 +20,7 @@
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:textColor="@color/colorBlack"
android:text="Choose Printer"
android:textSize="16sp"
android:textStyle="bold" />
Expand All @@ -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" />

<Button
android:id="@+id/btn_test_printer"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:gravity="center"
android:text="Test"
android:textColor="@android:color/white"
android:textSize="14sp" />
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">

<Button
android:id="@+id/btn_save_printer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="Save"
android:textColor="@android:color/white"
android:textSize="14sp" />
<Button
android:id="@+id/btn_test_printer"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:gravity="center"
android:text="Test"
android:textColor="@android:color/white"
android:textSize="14sp" />

<Button
android:id="@+id/btn_save_printer"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:gravity="center"
android:text="Save"
android:textColor="@android:color/white"
android:textSize="14sp" />

</LinearLayout>

</LinearLayout>

Expand Down
2 changes: 1 addition & 1 deletion printama/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<color name="colorGrayC">#cccccc</color>
<color name="colorGrayD">#dddddd</color>
<color name="colorGrayE">#eeeeee</color>
<color name="colorGrayEF">#efefef</color>
<color name="colorGrayFA">#FAFAFA</color>

<color name="colorGreen">#5A932D</color>
<color name="colorYellow">#FFEB3B</color>
Expand Down

0 comments on commit 861c4fa

Please sign in to comment.