NumberPicker np;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
np = (NumberPicker)findViewById(R.id.np1);
String[] values = {"one","two","three", "four","five","six","seven","eight"};
np.setMaxValue(values.length-1);
np.setMinValue(0);
np.setDisplayedValues(values);
np.setWrapSelectorWheel(false);
}
Above is a snippet of my code. So I am trying to get the index value of number picker that is selected. How do I write it? I already have something like this:
int pushupReps = Integer.parseInt(push_ups.getText().toString());
but it does not seem to be working and I have this in the OnCreate() method:
push_ups = (EditText)findViewById(R.id.pushups);
This is my main xml used:
<LinearLayout
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" >
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm a Commando/Diver/Guards" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/age"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="Age: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<NumberPicker
android:id="@+id/np1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType= "none" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Push-ups: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/pushups"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:text="Sit-ups: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/situps"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/pushup_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="2.4km Run: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/run_minutes"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="mins" />
<EditText
android:id="@+id/run_secs"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="secs" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="143dp" >
<Button
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="Calculate" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="All Rights Reserved. Lee Guanhua 2014."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="8sp" />
int pos = np.getValue()
should get the index.
values[pos]
gets you the value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With