├── res
├── values
│ └── strings.xml
└── layout
│ ├── length_picker.xml
│ └── main.xml
├── project.properties
├── AndroidManifest.xml
├── proguard-project.txt
└── src
└── com
└── sqisland
└── android
└── length_picker
├── MainActivity.java
└── LengthPicker.java
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Length Picker
4 |
5 | +
6 | -
7 |
8 | Width
9 | Height
10 | Area
11 | Compute
12 | %1$d sq in
13 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-15
15 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
11 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/res/layout/length_picker.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
11 |
16 |
21 |
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/src/com/sqisland/android/length_picker/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.sqisland.android.length_picker;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.TextView;
7 |
8 | public class MainActivity extends Activity {
9 | private LengthPicker mWidth;
10 | private LengthPicker mHeight;
11 | private TextView mArea;
12 |
13 | @Override
14 | public void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.main);
17 |
18 | mWidth = (LengthPicker) findViewById(R.id.width);
19 | mHeight = (LengthPicker) findViewById(R.id.height);
20 | mArea = (TextView) findViewById(R.id.area);
21 | }
22 |
23 | @Override
24 | public void onResume() {
25 | super.onResume();
26 | updateArea(null);
27 | }
28 |
29 | public void updateArea(View v) {
30 | int area = mWidth.getNumInches() * mHeight.getNumInches();
31 | mArea.setText(getString(R.string.area_format, area));
32 | }
33 | }
--------------------------------------------------------------------------------
/res/layout/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
15 |
16 |
17 |
22 |
26 |
27 |
28 |
32 |
37 |
42 |
43 |
--------------------------------------------------------------------------------
/src/com/sqisland/android/length_picker/LengthPicker.java:
--------------------------------------------------------------------------------
1 | package com.sqisland.android.length_picker;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.os.Parcelable;
6 | import android.util.AttributeSet;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.widget.LinearLayout;
10 | import android.widget.TextView;
11 |
12 | public class LengthPicker extends LinearLayout implements View.OnClickListener {
13 | private static final String SUPER_STATE = "superState";
14 | private static final String NUM_INCHES = "numInches";
15 |
16 | private TextView mTextView;
17 | private View mMinusButton;
18 |
19 | private View mPlusButton;
20 | private int mNumInches = 0;
21 |
22 | public LengthPicker(Context context) {
23 | super(context);
24 | init();
25 | }
26 |
27 | public LengthPicker(Context context, AttributeSet attrs) {
28 | super(context, attrs);
29 | init();
30 | }
31 |
32 | public LengthPicker(Context context, AttributeSet attrs, int defStyle) {
33 | super(context, attrs, defStyle);
34 | init();
35 | }
36 |
37 | private void init() {
38 | LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
39 | Context.LAYOUT_INFLATER_SERVICE);
40 | inflater.inflate(R.layout.length_picker, this);
41 |
42 | mPlusButton = findViewById(R.id.plus_button);
43 | mTextView = (TextView) findViewById(R.id.text);
44 | mMinusButton = findViewById(R.id.minus_button);
45 |
46 | updateControls();
47 |
48 | mPlusButton.setOnClickListener(this);
49 | mMinusButton.setOnClickListener(this);
50 | }
51 |
52 | private void updateControls() {
53 | int feet = mNumInches / 12;
54 | int inches = mNumInches % 12;
55 |
56 | String text = String.format("%d' %d\"", feet, inches);
57 | if (feet == 0) {
58 | text = String.format("%d\"", inches);
59 | } else {
60 | if (inches == 0) {
61 | text = String.format("%d'", feet);
62 | }
63 | }
64 | mTextView.setText(text);
65 |
66 | mMinusButton.setEnabled(mNumInches > 0);
67 | }
68 |
69 | public int getNumInches() {
70 | return mNumInches;
71 | }
72 |
73 | @Override
74 | public Parcelable onSaveInstanceState() {
75 | Bundle bundle = new Bundle();
76 | bundle.putParcelable(SUPER_STATE, super.onSaveInstanceState());
77 | bundle.putInt(NUM_INCHES, mNumInches);
78 | return bundle;
79 |
80 | }
81 |
82 | @Override
83 | public void onRestoreInstanceState(Parcelable state) {
84 | if (state instanceof Bundle) {
85 | Bundle bundle = (Bundle) state;
86 | mNumInches = bundle.getInt(NUM_INCHES);
87 | super.onRestoreInstanceState(bundle.getParcelable(SUPER_STATE));
88 | } else {
89 | super.onRestoreInstanceState(state);
90 | }
91 | updateControls();
92 | }
93 |
94 | @Override
95 | public void onClick(View v) {
96 | switch (v.getId()) {
97 | case R.id.plus_button:
98 | mNumInches++;
99 | updateControls();
100 | break;
101 | case R.id.minus_button:
102 | if (mNumInches > 0) {
103 | mNumInches--;
104 | updateControls();
105 | }
106 | break;
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------