From f49bd68757d92df81a997d4bcb49e5c7034139a1 Mon Sep 17 00:00:00 2001 From: David Glenck Date: Mon, 23 Nov 2015 00:54:41 +0100 Subject: [PATCH] add ability to enter colour by numeric value #2 --- .../calendarcolor/ColorPickerActivity.java | 58 +++++++++++- .../main/res/layout/activity_color_picker.xml | 92 +++++++++++++------ app/src/main/res/values/strings.xml | 2 + 3 files changed, 119 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/ch/ihdg/calendarcolor/ColorPickerActivity.java b/app/src/main/java/ch/ihdg/calendarcolor/ColorPickerActivity.java index 0b639db..69a0c6e 100644 --- a/app/src/main/java/ch/ihdg/calendarcolor/ColorPickerActivity.java +++ b/app/src/main/java/ch/ihdg/calendarcolor/ColorPickerActivity.java @@ -3,21 +3,20 @@ import android.app.Activity; import android.content.ContentUris; import android.content.ContentValues; +import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.provider.CalendarContract; -import android.view.Menu; -import android.view.MenuItem; +import android.text.Editable; +import android.text.TextWatcher; import android.view.View; import android.widget.Button; -import android.widget.TextView; +import android.widget.EditText; import com.larswerkman.holocolorpicker.ColorPicker; import com.larswerkman.holocolorpicker.SaturationBar; import com.larswerkman.holocolorpicker.ValueBar; -import ch.ihdg.calendarcolor.R; - public class ColorPickerActivity extends Activity { static final String ARG_NAME = "arg_name"; @@ -45,6 +44,55 @@ protected void onCreate(Bundle savedInstanceState) { picker.setColor(color); picker.setOldCenterColor(color); + //write color hex code in text field + final EditText hexText = (EditText) findViewById(R.id.hexText); + String red = String.format("%02x", (color >> 16) & 0xFF ), + green = String.format("%02x", (color >> 8) & 0xFF), + blue = String.format("%02x", color & 0xFF); + hexText.setText( red + green + blue ); + findViewById(R.id.dummy).requestFocus(); + + //update hex value when color changes + picker.setOnColorChangedListener( new ColorPicker.OnColorChangedListener() { + @Override + public void onColorChanged(int color) { + //write color hex code in text field + String hex = String.format("%02x", (color >> 16) & 0xFF ) + + String.format("%02x", (color >> 8) & 0xFF) + + String.format("%02x", color & 0xFF); + if(!hexText.getText().toString().equals(hex)) { + hexText.setText( hex ); + } + } + }); + + //update color picker when a valid hex code is entered + hexText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {} + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {} + + @Override + public void afterTextChanged(Editable s) { + if( s.length() == 6 ) + { + try{ + int color = Color.parseColor('#' + s.toString()); + if( picker.getColor() != color ) { + picker.setColor( color ); + } + hexText.setTextColor( Color.BLACK ); + } + catch ( IllegalArgumentException e ) { + //ignore + hexText.setTextColor( Color.RED ); + } + } + } + }); + final Button buttoncancel = (Button) findViewById(R.id.buttoncancel); buttoncancel.setOnClickListener(new View.OnClickListener() { @Override diff --git a/app/src/main/res/layout/activity_color_picker.xml b/app/src/main/res/layout/activity_color_picker.xml index 2869cf2..4f2c255 100644 --- a/app/src/main/res/layout/activity_color_picker.xml +++ b/app/src/main/res/layout/activity_color_picker.xml @@ -1,13 +1,72 @@ - + + + + + + + + + + + + + + + + + + + + + + + @@ -28,27 +87,4 @@ - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9773ab3..fa32fa1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,5 +6,7 @@ ColorPickerActivity Cancel Save + rrggbb + #