Skip to content

Commit

Permalink
add ability to enter colour by numeric value #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalfree committed Nov 22, 2015
1 parent 312e85e commit f49bd68
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 33 deletions.
58 changes: 53 additions & 5 deletions app/src/main/java/ch/ihdg/calendarcolor/ColorPickerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
92 changes: 64 additions & 28 deletions app/src/main/res/layout/activity_color_picker.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:id="@+id/scrollView"
android:layout_weight="1"
android:layout_gravity="center">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical|center_horizontal">

<com.larswerkman.holocolorpicker.ColorPicker
android:id="@+id/picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<com.larswerkman.holocolorpicker.SaturationBar
android:id="@+id/saturationbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<com.larswerkman.holocolorpicker.ValueBar
android:id="@+id/valuebar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="@dimen/bar_length"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/color_hex_field_text"
android:id="@+id/textView" />

<LinearLayout
android:orientation="horizontal"
android:id="@+id/dummy"
android:focusable="true" android:focusableInTouchMode="true"
android:layout_width="0px" android:layout_height="0px"/>

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/hexText"
android:layout_gravity="center_horizontal"
android:hint="@string/color_hex_field_hint"
android:inputType="textNoSuggestions"/>
</LinearLayout>

</LinearLayout>
</ScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:layout_margin="0dp"
android:padding="0dp">
Expand All @@ -28,27 +87,4 @@

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">

<com.larswerkman.holocolorpicker.ColorPicker
android:id="@+id/picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<com.larswerkman.holocolorpicker.SaturationBar
android:id="@+id/saturationbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<com.larswerkman.holocolorpicker.ValueBar
android:id="@+id/valuebar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

</RelativeLayout>
</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
<string name="title_activity_color_picker">ColorPickerActivity</string>
<string name="cancel">Cancel</string>
<string name="save">Save</string>
<string name="color_hex_field_hint">rrggbb</string>
<string name="color_hex_field_text">#</string>

</resources>

0 comments on commit f49bd68

Please sign in to comment.