Add a color picker.

This commit is contained in:
Alex Ning 2020-03-20 17:03:49 +08:00
parent 9e4dec362d
commit 587dfccd32
5 changed files with 333 additions and 3 deletions

View File

@ -3,7 +3,9 @@ package ml.docilealligator.infinityforreddit.Activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
@ -69,7 +71,7 @@ public class CustomizeThemeActivity extends BaseActivity {
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new CustomizeThemeRecyclerViewAdapter(); adapter = new CustomizeThemeRecyclerViewAdapter(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
@ -134,6 +136,17 @@ public class CustomizeThemeActivity extends BaseActivity {
} }
} }
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return false;
}
@Override @Override
protected SharedPreferences getSharedPreferences() { protected SharedPreferences getSharedPreferences() {
return sharedPreferences; return sharedPreferences;

View File

@ -8,6 +8,7 @@ import android.widget.Switch;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList; import java.util.ArrayList;
@ -15,14 +16,17 @@ import java.util.ArrayList;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeSettingsItem; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeSettingsItem;
import ml.docilealligator.infinityforreddit.CustomView.ColorPickerDialog;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_COLOR = 1; private static final int VIEW_TYPE_COLOR = 1;
private static final int VIEW_TYPE_SWITCH = 2; private static final int VIEW_TYPE_SWITCH = 2;
private AppCompatActivity activity;
private ArrayList<CustomThemeSettingsItem> customThemeSettingsItems; private ArrayList<CustomThemeSettingsItem> customThemeSettingsItems;
public CustomizeThemeRecyclerViewAdapter() { public CustomizeThemeRecyclerViewAdapter(AppCompatActivity activity) {
this.activity = activity;
customThemeSettingsItems = new ArrayList<>(); customThemeSettingsItems = new ArrayList<>();
} }
@ -53,7 +57,10 @@ public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((ThemeColorItemViewHolder) holder).themeItemInfoTextView.setText(customThemeSettingsItem.itemDetails); ((ThemeColorItemViewHolder) holder).themeItemInfoTextView.setText(customThemeSettingsItem.itemDetails);
((ThemeColorItemViewHolder) holder).colorImageView.setBackgroundTintList(ColorStateList.valueOf(customThemeSettingsItem.colorValue)); ((ThemeColorItemViewHolder) holder).colorImageView.setBackgroundTintList(ColorStateList.valueOf(customThemeSettingsItem.colorValue));
holder.itemView.setOnClickListener(view -> { holder.itemView.setOnClickListener(view -> {
new ColorPickerDialog(activity, customThemeSettingsItem.colorValue, color -> {
customThemeSettingsItem.colorValue = color;
((ThemeColorItemViewHolder) holder).colorImageView.setBackgroundTintList(ColorStateList.valueOf(color));
}).show();
}); });
} else if (holder instanceof ThemeSwitchItemViewHolder) { } else if (holder instanceof ThemeSwitchItemViewHolder) {
CustomThemeSettingsItem customThemeSettingsItem = customThemeSettingsItems.get(position); CustomThemeSettingsItem customThemeSettingsItem = customThemeSettingsItems.get(position);

View File

@ -0,0 +1,151 @@
package ml.docilealligator.infinityforreddit.CustomView;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import ml.docilealligator.infinityforreddit.R;
public class ColorPickerDialog extends AlertDialog {
private View colorView;
private EditText colorValueEditText;
private SeekBar seekBarA;
private SeekBar seekBarR;
private SeekBar seekBarG;
private SeekBar seekBarB;
private Button cancelButton;
private Button okButton;
private int colorValue;
private boolean changeColorValueEditText = true;
private ColorPickerListener colorPickerListener;
public interface ColorPickerListener {
void onColorPicked(int color);
}
public ColorPickerDialog(Context context, int color, ColorPickerListener colorPickerListener) {
super(context);
View rootView = getLayoutInflater().inflate(R.layout.color_picker, null);
colorView = rootView.findViewById(R.id.color_view_color_picker);
colorValueEditText = rootView.findViewById(R.id.color_edit_text_color_picker);
seekBarA = rootView.findViewById(R.id.a_seek_bar_color_picker);
seekBarR = rootView.findViewById(R.id.r_seek_bar_color_picker);
seekBarG = rootView.findViewById(R.id.g_seek_bar_color_picker);
seekBarB = rootView.findViewById(R.id.b_seek_bar_color_picker);
cancelButton = rootView.findViewById(R.id.cancel_button_color_picker);
okButton = rootView.findViewById(R.id.ok_button_color_picker);
colorView.setBackgroundColor(color);
colorValueEditText.setText(Integer.toHexString(color).toUpperCase());
colorValueEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String s = editable.toString();
if (s.length() == 6) {
try {
changeColorValueEditText = false;
colorValue = Color.parseColor("#" + s);
seekBarA.setProgress(255);
seekBarR.setProgress(Integer.parseInt(s.substring(0, 2), 16));
seekBarG.setProgress(Integer.parseInt(s.substring(2, 4), 16));
seekBarB.setProgress(Integer.parseInt(s.substring(4, 6), 16));
changeColorValueEditText = true;
} catch (IllegalArgumentException ignored) {
}
} else if (s.length() == 8) {
try {
changeColorValueEditText = false;
colorValue = Color.parseColor("#" + s);
seekBarA.setProgress(Integer.parseInt(s.substring(0, 2), 16));
seekBarR.setProgress(Integer.parseInt(s.substring(2, 4), 16));
seekBarG.setProgress(Integer.parseInt(s.substring(4, 6), 16));
seekBarB.setProgress(Integer.parseInt(s.substring(6, 8), 16));
changeColorValueEditText = true;
} catch (IllegalArgumentException ignored) {
}
}
}
});
String colorHex = Integer.toHexString(color);
if (colorHex.length() == 8) {
colorValue = Color.parseColor("#" + colorHex);
seekBarA.setProgress(Integer.parseInt(colorHex.substring(0, 2), 16));
seekBarR.setProgress(Integer.parseInt(colorHex.substring(2, 4), 16));
seekBarG.setProgress(Integer.parseInt(colorHex.substring(4, 6), 16));
seekBarB.setProgress(Integer.parseInt(colorHex.substring(6, 8), 16));
} else if (colorHex.length() == 6) {
colorValue = Color.parseColor("#" + colorHex);
seekBarA.setProgress(255);
seekBarR.setProgress(Integer.parseInt(colorHex.substring(0, 2), 16));
seekBarG.setProgress(Integer.parseInt(colorHex.substring(2, 4), 16));
seekBarB.setProgress(Integer.parseInt(colorHex.substring(4, 6), 16));
}
setOnSeekBarChangeListener(seekBarA);
setOnSeekBarChangeListener(seekBarR);
setOnSeekBarChangeListener(seekBarG);
setOnSeekBarChangeListener(seekBarB);
cancelButton.setOnClickListener(view -> dismiss());
okButton.setOnClickListener(view -> {
try {
colorValue = Color.parseColor("#" + colorValueEditText.getText().toString());
colorPickerListener.onColorPicked(colorValue);
dismiss();
} catch (IllegalArgumentException e) {
Toast.makeText(context, R.string.invalid_color, Toast.LENGTH_SHORT).show();
}
});
setView(rootView);
}
private void setOnSeekBarChangeListener(SeekBar seekBar) {
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (changeColorValueEditText) {
int aValue = seekBarA.getProgress();
int rValue = seekBarR.getProgress();
int gValue = seekBarG.getProgress();
int bValue = seekBarB.getProgress();
String colorHex = String.format("%02x%02x%02x%02x", aValue, rValue, gValue, bValue).toUpperCase();
colorValue = Color.parseColor("#" + colorHex);
colorView.setBackgroundColor(colorValue);
colorValueEditText.setText(colorHex);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}

View File

@ -0,0 +1,156 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"
android:text="@string/color_picker"
style="@style/MaterialAlertDialogTitleTextStyle" />
<View
android:id="@+id/color_view_color_picker"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#"
android:textColor="?attr/primaryTextColor"/>
<EditText
android:id="@+id/color_edit_text_color_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLength="8"
android:inputType="textCapCharacters|textNoSuggestions"
android:textColor="?attr/primaryTextColor" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="8dp"
android:paddingStart="32dp"
android:paddingEnd="32dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textColor="?attr/primaryTextColor" />
<SeekBar
android:id="@+id/a_seek_bar_color_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="32dp"
android:paddingEnd="32dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R"
android:textColor="?attr/primaryTextColor" />
<SeekBar
android:id="@+id/r_seek_bar_color_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="32dp"
android:paddingEnd="32dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G"
android:textColor="?attr/primaryTextColor" />
<SeekBar
android:id="@+id/g_seek_bar_color_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="32dp"
android:paddingStart="32dp"
android:paddingEnd="32dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:textColor="?attr/primaryTextColor" />
<SeekBar
android:id="@+id/b_seek_bar_color_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255" />
</LinearLayout>
<LinearLayout
android:layout_gravity="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingStart="12dp"
android:paddingEnd="12dp">
<Button
android:id="@+id/cancel_button_color_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
style="@style/MaterialAlertDialogNegativeButtonStyle" />
<Button
android:id="@+id/ok_button_color_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok"
style="@style/MaterialAlertDialogPositiveButtonStyle" />
</LinearLayout>
</LinearLayout>

View File

@ -602,4 +602,7 @@
<string name="theme_name_indigo_dark">Indigo Dark</string> <string name="theme_name_indigo_dark">Indigo Dark</string>
<string name="theme_name_indigo_amoled">Indigo Amoled</string> <string name="theme_name_indigo_amoled">Indigo Amoled</string>
<string name="color_picker">Color Picker</string>
<string name="invalid_color">Invalid Color</string>
</resources> </resources>