My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package net.mandaria.tippytipper.preferences;

import net.mandaria.tippytipper.widgets.*;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.preference.DialogPreference;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class DecimalPreference extends DialogPreference
{
private static final String androidns = "http://schemas.android.com/apk/res/android";
private static final String appns = "http://schemas.android.com/apk/res/net.mandaria.tippytipper";

private NumberPicker mPickInteger, mPickDecimal;
private TextView mSplashText, mValueText;
private Context mContext;

private String mDialogMessage, mSuffix;
private float mDefault, mValue = 0;
private int mInteger, mDecimal = 0;

public DecimalPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;

mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
mSuffix = attrs.getAttributeValue(androidns, "text");
mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0);
}

@Override
protected View onCreateDialogView()
{
TableLayout.LayoutParams params;
// LinearLayout layout = new LinearLayout(mContext);
TableLayout layout = new TableLayout(mContext);
// layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);

mSplashText = new TextView(mContext);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);

TableRow row_header = new TableRow(mContext);
row_header.addView(mSplashText);

mPickInteger = new NumberPicker(mContext);
mPickDecimal = new NumberPicker(mContext);
mPickDecimal.setFormatter(NumberPicker.THREE_DIGIT_FORMATTER);

TextView dot = new TextView(mContext);
dot.setText(".");
dot.setTextSize(32);

TextView percent = new TextView(mContext);
percent.setText("%");
percent.setTextSize(32);

TableRow row_one = new TableRow(mContext);
row_one.setGravity(Gravity.CENTER);
row_one.addView(mPickInteger);
row_one.addView(dot);
row_one.addView(mPickDecimal);
row_one.addView(percent);

layout.addView(row_header);

TableLayout table_main = new TableLayout(mContext);
table_main.addView(row_one);

TableRow row_main = new TableRow(mContext);
row_main.setGravity(Gravity.CENTER_HORIZONTAL);
row_main.addView(table_main);

layout.addView(row_main);

if (shouldPersist())
mValue = getPersistedFloat(mDefault);

bindData();

return layout;
}

private void bindData()
{
mInteger = (int) Math.floor(mValue);
float decimal = (mValue * 1000) - (mInteger * 1000);
mDecimal = (int) decimal;
try
{
mPickInteger.setCurrent(mInteger);
mPickDecimal.setCurrent(mDecimal);
}
catch (Exception ex)
{
int test = 0;
test++;
}
}

@Override
protected void onBindDialogView(View v)
{
super.onBindDialogView(v);
bindData();
}

@Override
protected void onSetInitialValue(boolean restore, Object defaultValue)
{
super.onSetInitialValue(restore, defaultValue);
if (restore)
{
try
{
mValue = shouldPersist() ? getPersistedFloat(mDefault) : 0;
}
catch (Exception ex)
{
mValue = mDefault;
}
}
else
mValue = (Float) defaultValue;
}

@Override
protected void onDialogClosed(boolean positiveResult)
{
if (positiveResult == true)
{
super.onDialogClosed(positiveResult);
// HACK: "click" both picker inputs to validate inputs before closing the dialog
// this is to fix a problem of closing the dialog not causing the onFocusChange of the picker
// to be called
mPickInteger.onClick(null);
mPickDecimal.onClick(null);
String value = mPickInteger.getCurrent() + "." + mPickDecimal.getCurrentFormatted();
mValue = Float.valueOf(value);
if (shouldPersist())
persistFloat(mValue);
}
}
}

Change log

r156 by mandlar on Nov 26, 2010   Diff
Applying most of the changes from Stephen
Donaldson Jr's patch:

- renamed functions and variable to match
Java conventions (mostly changing first
letter upper to lower case).  He missed a
few, I fixed those
- CLEAR and Calculate! buttons added to
first screen.  I fixed Calculate button to
not fill the rest the rest of the screen,
it's the same height as the other buttons
- Holding off on his SplitBill changes
...
Go to: 
Project members, sign in to write a code review

Older revisions

r135 by mandlar on Jun 6, 2010   Diff
Merging development to trunk
r128 by mandlar on Jun 6, 2010   Diff
Merged development branch into trunk
r105 by mandlar on May 23, 2010   Diff
Merged developmenet branch into trunk
All revisions of this file

File info

Size: 4026 bytes, 150 lines
Powered by Google Project Hosting