I have created one class in which i want to set two different spinner for date picker. I want this because i want to set start event date and end event date, so how can i set two different spinner with date picker in one class??? thanks in advance.
Asked
Active
Viewed 512 times
0
-
Welcome to SO..What have you tried..? – Pragnani Mar 20 '13 at 16:06
-
i an trying to set two different spinner with two different date picker with spinner in one class.... because i want to set start event date and end event date individually. – user2092132 Mar 20 '13 at 16:21
1 Answers
0
Here is the triple number picker ive used in some of my applications. shoudl be easy to modifiy it to only use two numbers.
package com.t3hh4xx0r.nfcsecure.widgets;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import com.t3hh4xx0r.nfcsecure.R;
public class TripleNumberPickerPreference extends DialogPreference {
private static final String defaultPersistedString = "0:0:0";
private NumberPicker hours = null;
private NumberPicker mins = null;
private NumberPicker secs = null;
View rootView;
public TripleNumberPickerPreference(Context ctxt) {
this(ctxt, null);
}
public TripleNumberPickerPreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, 0);
}
public TripleNumberPickerPreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
@Override
protected View onCreateDialogView() {
String[] nums = new String[61];
for(int i=0; i<nums.length; i++) {
nums[i] = Integer.toString(i);
}
LayoutInflater lI = LayoutInflater.from(getContext());
rootView = lI.inflate(R.layout.triple_number_picker, null);
hours = (NumberPicker) rootView.findViewById(R.id.hours);
mins = (NumberPicker) rootView.findViewById(R.id.minutes);
secs = (NumberPicker) rootView.findViewById(R.id.seconds);
hours.setMaxValue(24);
hours.setMinValue(0);
hours.setDisplayedValues(nums);
mins.setMaxValue(60);
mins.setMinValue(0);
mins.setDisplayedValues(nums);
secs.setMaxValue(60);
secs.setMinValue(0);
secs.setDisplayedValues(nums);
deconstructPersistedData();
return (rootView);
}
private void deconstructPersistedData() {
hours.setValue(Integer.parseInt(getPersistedString(defaultPersistedString).split(":")[0]));
mins.setValue(Integer.parseInt(getPersistedString(defaultPersistedString).split(":")[1]));
secs.setValue(Integer.parseInt(getPersistedString(defaultPersistedString).split(":")[2]));
}
private String buildPersistedData() {
StringBuilder sB = new StringBuilder();
sB.append(hours.getValue());
sB.append(":");
sB.append(mins.getValue());
sB.append(":");
sB.append(secs.getValue());
sB.append(":");
long timeTillNextSecureLock = buildTimeTillSecureLock();
Log.d("TIME TILL NEXT LOCK SET", ""+timeTillNextSecureLock);
sB.append(timeTillNextSecureLock);
return sB.toString();
}
private long buildTimeTillSecureLock() {
double hoursSimple = hours.getValue();
double minsSimple = mins.getValue();
double secsSimple = secs.getValue();
double secsToMillis = 1000;
double minsToMillis = 60000;
double hoursToMillis = 3600000;
double secsAsMills = secsSimple * secsToMillis;
double minsAsMills = minsSimple * minsToMillis;
double hoursAsMills = hoursSimple * hoursToMillis;
return (long) (secsAsMills + minsAsMills + hoursAsMills);
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
String persisted = buildPersistedData();
if (positiveResult) {
if (callChangeListener(persisted)) {
persistString(persisted);
notifyChanged();
}
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return defaultPersistedString;
}
public static long subAndCheck(long a, long b) {
long ret;
String msg = "overflow: subtract";
if (b == Long.MIN_VALUE) {
if (a < 0) {
ret = a - b;
} else {
throw new ArithmeticException(msg);
}
} else {
ret = addAndCheck(a, -b, msg);
}
return ret;
}
private static long addAndCheck(long a, long b, String msg) {
long ret;
if (a > b) {
ret = addAndCheck(b, a, msg);
} else {
if (a < 0) {
if (b < 0) {
if (Long.MIN_VALUE - b <= a) {
ret = a + b;
} else {
throw new ArithmeticException(msg);
}
} else {
ret = a + b;
}
} else {
if (a <= Long.MAX_VALUE - b) {
ret = a + b;
} else {
throw new ArithmeticException(msg);
}
}
}
return ret;
}
}
And the layout -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView01"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/hours"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/minutes"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/seconds"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<NumberPicker
android:id="@+id/hours"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="3dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<NumberPicker
android:id="@+id/minutes"
android:layout_width="0dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="3dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<NumberPicker
android:id="@+id/seconds"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

r2DoesInc
- 3,759
- 3
- 29
- 60
-
can you please help me for date picker actually i am new in android..!! – user2092132 Mar 20 '13 at 16:32