From 8d69552b756ba44e254251edf08206f8866c639b Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 1 Mar 2019 01:20:02 +0200 Subject: [PATCH] fix profile color selector --- .../ui/profiles/MoLeColoredTextView.java | 44 ++++++++++++++++ .../ui/profiles/ProfileDetailFragment.java | 50 ++++++++----------- .../main/res/layout/color_selector_item.xml | 5 +- 3 files changed, 67 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/ui/profiles/MoLeColoredTextView.java diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/MoLeColoredTextView.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/MoLeColoredTextView.java new file mode 100644 index 00000000..cfd78b05 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/MoLeColoredTextView.java @@ -0,0 +1,44 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.ui.profiles; + +import android.content.Context; +import android.util.AttributeSet; + +import net.ktnx.mobileledger.utils.Colors; + +import androidx.appcompat.widget.AppCompatTextView; + +public class MoLeColoredTextView extends AppCompatTextView { + public MoLeColoredTextView(Context context) { + super(context); + } + public MoLeColoredTextView(Context context, AttributeSet attrs) { + super(context, attrs); + } + public MoLeColoredTextView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + @Override + public void setText(CharSequence text, BufferType type) { + super.setText(text, type); + int deg = (text == null || text.equals("")) ? -1 : Integer.valueOf(String.valueOf(text)); + if (deg == -1) deg = Colors.DEFAULT_HUE_DEG; + setBackgroundColor(Colors.getPrimaryColorForHue(deg)); + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 0f307e0a..59f27a79 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -19,7 +19,7 @@ package net.ktnx.mobileledger.ui.profiles; import android.app.Activity; import android.content.Context; -import android.os.Build; +import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -47,8 +47,6 @@ import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity; import net.ktnx.mobileledger.utils.Colors; -import org.jetbrains.annotations.NotNull; - import java.util.List; import androidx.annotation.ColorInt; @@ -194,11 +192,12 @@ public class ProfileDetailFragment extends Fragment { passwordLayout = rootView.findViewById(R.id.password_layout); colorSpinner = rootView.findViewById(R.id.colorSpinner); - ArrayAdapter adapter = ColorListAdapter - .createFromResource(rootView.getContext(), R.array.profile_colors, - R.layout.color_selector_item); -// Log.d("profiles", String.format("color count: %s", adapter.getCount())); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + ColorListAdapter adapter = + new ColorListAdapter(rootView.getContext(), R.layout.color_selector_item); + adapter.add(-1); + for (int i = 0; i < 360; i += 15) adapter.add(i); + Log.d("profiles", String.format("color count: %s", adapter.getCount())); +// adapter.setDropDownViewResource(R.layout.color_selector_item); colorSpinner.setAdapter(adapter); colorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override @@ -208,17 +207,9 @@ public class ProfileDetailFragment extends Fragment { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { final int primaryColor; - final int degrees = - Integer.valueOf((String) (parent.getAdapter().getItem(position))); + final int degrees = (Integer) parent.getAdapter().getItem(position); if (degrees < 0) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (getActivity() != null) primaryColor = getResources() - .getColor(R.color.colorPrimary, getActivity().getTheme()); - else primaryColor = Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG); - } - else { - primaryColor = getResources().getColor(R.color.colorPrimary); - } + primaryColor = Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG); } else primaryColor = Colors.getPrimaryColorForHue(degrees); @@ -332,41 +323,42 @@ public class ProfileDetailFragment extends Fragment { return valid; } - private class ColorListAdapter extends ArrayAdapter { + private class ColorListAdapter extends ArrayAdapter { public ColorListAdapter(@NonNull Context context, int resource) { super(context, resource); } public ColorListAdapter(@NonNull Context context, int resource, int textViewResourceId) { super(context, resource, textViewResourceId); } - public ColorListAdapter(@NonNull Context context, int resource, @NonNull String[] objects) { + public ColorListAdapter(@NonNull Context context, int resource, + @NonNull Integer[] objects) { super(context, resource, objects); } public ColorListAdapter(@NonNull Context context, int resource, int textViewResourceId, - @NonNull String[] objects) { + @NonNull Integer[] objects) { super(context, resource, textViewResourceId, objects); } public ColorListAdapter(@NonNull Context context, int resource, - @NonNull List objects) { + @NonNull List objects) { super(context, resource, objects); } public ColorListAdapter(@NonNull Context context, int resource, int textViewResourceId, - @NonNull List objects) { + @NonNull List objects) { super(context, resource, textViewResourceId, objects); } - @NotNull @Override - public View getView(int position, View convertView, @NotNull ViewGroup parent) { - String hueStr = getItem(position); - int hue = (hueStr == null) ? -1 : Integer.valueOf(hueStr); + public @NonNull + View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + Integer hueDeg = getItem(position); + int hue = ((hueDeg == null) || (hueDeg == -1)) ? Colors.DEFAULT_HUE_DEG : hueDeg; @ColorInt int primaryColor = Colors.getPrimaryColorForHue(hue); View view = convertView; if (convertView == null) { - view = getLayoutInflater().inflate(R.layout.color_selector_item, parent); + view = getLayoutInflater().inflate(R.layout.color_selector_item, null); } - view.setBackgroundColor(primaryColor); + view.setBackground(new ColorDrawable(primaryColor)); return view; } } diff --git a/app/src/main/res/layout/color_selector_item.xml b/app/src/main/res/layout/color_selector_item.xml index 66b95d1b..fa14dffd 100644 --- a/app/src/main/res/layout/color_selector_item.xml +++ b/app/src/main/res/layout/color_selector_item.xml @@ -1,6 +1,5 @@ - - \ No newline at end of file + android:textColor="@android:color/transparent"> \ No newline at end of file -- 2.39.5