First cut at a color prefs editor.

This commit is contained in:
eehouse 2010-03-21 03:11:29 +00:00
parent e05b31585d
commit b20296fbd2
4 changed files with 188 additions and 44 deletions

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/color_display_sample"
android:layout_width="32sp"
android:layout_height="32sp"
android:layout_gravity="center_vertical"
android:layout_marginRight="6sp"
android:focusable="false"
android:clickable="false"
/>

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
>
<EditText android:id="@+id/edit_red"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="6sp"
android:numeric="decimal"
/>
<EditText android:id="@+id/edit_green"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="6sp"
android:numeric="decimal"
/>
<EditText android:id="@+id/edit_blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="6sp"
android:numeric="decimal"
/>
</LinearLayout>

View file

@ -33,52 +33,58 @@
<PreferenceScreen android:title="@string/prefs_colors"
>
<EditTextPreference android:key="@string/key_player0"
android:title="@string/player0"
android:defaultValue="0x000000"
/>
<EditTextPreference android:key="@string/key_player1"
android:title="@string/player1"
android:defaultValue="0xFF0000"
/>
<EditTextPreference android:key="@string/key_player2"
android:title="@string/player2"
android:defaultValue="0x0000FF"
/>
<EditTextPreference android:key="@string/key_player3"
android:title="@string/player3"
android:defaultValue="0x008F00"
/>
<EditTextPreference android:key="@string/key_bonus_l2x"
android:title="@string/bonus_l2x"
android:defaultValue="0xAFAF00"
/>
<EditTextPreference android:key="@string/key_bonus_l3x"
android:title="@string/bonus_l3x"
android:defaultValue="0x00AFAF"
/>
<EditTextPreference android:key="@string/key_bonus_w2x"
android:title="@string/bonus_w2x"
android:defaultValue="0xAF00AF"
/>
<EditTextPreference android:key="@string/key_bonus_w3x"
android:title="@string/bonus_w3x"
android:defaultValue="0xAFAFAF"
/>
<EditTextPreference android:key="@string/key_tile_back"
android:title="@string/tile_back"
android:defaultValue="0xFFFF99"
/>
<EditTextPreference android:key="@string/key_focus"
android:title="@string/focus"
android:defaultValue="0x7070FF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player0"
android:title="@string/player0"
android:defaultValue="0x000000"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player1"
android:title="@string/player1"
android:defaultValue="0xFF0000"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player2"
android:title="@string/player2"
android:defaultValue="0x0000FF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_player3"
android:title="@string/player3"
android:defaultValue="0x008F00"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_l2x"
android:title="@string/bonus_l2x"
android:defaultValue="0xAFAF00"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_l3x"
android:title="@string/bonus_l3x"
android:defaultValue="0x00AFAF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_w2x"
android:title="@string/bonus_w2x"
android:defaultValue="0xAF00AF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_bonus_w3x"
android:title="@string/bonus_w3x"
android:defaultValue="0xAFAFAF"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_tile_back"
android:title="@string/tile_back"
android:defaultValue="0xFFFF99"
/>
<org.eehouse.android.xw4.EditColorPreference
android:key="@string/key_focus"
android:title="@string/focus"
android:defaultValue="0x7070FF"
/>
</PreferenceScreen>
<PreferenceScreen android:title="@string/advanced"
>
<EditTextPreference android:key="@string/key_relay_host"

View file

@ -0,0 +1,92 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
package org.eehouse.android.xw4;
import android.preference.DialogPreference;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.app.Dialog;
// import android.app.AlertDialog;
import junit.framework.Assert;
public class EditColorPreference extends DialogPreference {
private Context m_context;
// private int m_color = 0;
private View m_sample = null;
public EditColorPreference( Context context, AttributeSet attrs )
{
super( context, attrs );
m_context = context;
setWidgetLayoutResource( R.layout.color_display );
setDialogLayoutResource( R.layout.color_edit );
}
@Override
protected void onBindView( View parent )
{
super.onBindView( parent );
m_sample = parent.findViewById( R.id.color_display_sample );
if ( null != m_sample ) {
m_sample.setBackgroundColor( getPersistedColor() );
}
}
@Override
protected void onBindDialogView( View view )
{
int color = getPersistedColor();
setOneByte( view, R.id.edit_red, color >> 16 );
setOneByte( view, R.id.edit_green, color >> 8 );
setOneByte( view, R.id.edit_blue, color );
}
@Override
public void onDismiss( DialogInterface dialog )
{
int color = (getOneByte( dialog, R.id.edit_red ) << 16)
| (getOneByte( dialog, R.id.edit_green ) << 8)
| getOneByte( dialog, R.id.edit_blue );
color |= 0xFF000000;
// Need to restore the preference, not set the background color
persistString( String.format( "%d", color) );
}
private void setOneByte( View parent, int id, int byt ) {
byt &= 0xFF;
EditText et = (EditText)parent.findViewById( id );
if ( null != et ) {
et.setText( String.format("%d", byt ) );
}
}
private int getOneByte( DialogInterface parent, int id ) {
int val = 0;
Dialog dialog = (Dialog)parent;
EditText et = (EditText)dialog.findViewById( id );
if ( null != et ) {
String str = et.getText().toString();
val = Integer.decode( str );
}
return val;
}
private int getPersistedColor()
{
String val = getPersistedString("");
int color;
try {
color = 0xFF000000 | Integer.decode( val );
} catch ( java.lang.NumberFormatException nfe ) {
color = 0xFF7F7F7F;
}
return color;
}
}