0

I created a fragment layout that contains three number pickers. I used to use this fragment twice within activity_main.xml. Both sets of numberpickers appear properly, but I am unsure how to manipulate them programmatically because I don't know how to refer to each number picker individually. Basically, I am wondering if it is possible to refer to each numberpicker separately , given my current layout implementation.

The picker_fragment.xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/picker_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >

<NumberPicker
    android:id="@+id/redPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 />
<NumberPicker
    android:id="@+id/greenPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<NumberPicker
    android:id="@+id/bluePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

the activity_main.xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.colorpickertwo.MainActivity"
tools:ignore="MergeRootFrame" >

<include layout="@layout/picker_fragment"/>
<include layout="@layout/picker_fragment"/>


</LinearLayout>

I could just abandon my picker_fragment, or make a second fragment layout in order to give the second set of numberpicker's separate IDs, but my current implementation seems cleaner.

Thanks very much!

2 Answers2

0

Programatically you can refer to your Views like this:

NumberPicker redPicker = (NumberPicker)findViewById(R.id.redPicker);
NumberPicker greenPicker = (NumberPicker)findViewById(R.id.redPicker);
NumberPicker bluePicker = (NumberPicker)findViewById(R.id.bluePicker);

// and then do something with redPicker...

Here's the View documentation from android, check the IDs part.

Typo
  • 1,875
  • 1
  • 19
  • 32
  • I'm aware that I can use findViewById to get my numberpickers, I'm just not sure which of the pair is being returned, as the layout in which each picker is defined is used twice. Assigning findViewId(R.id.redPicker) to redPicker, and then doing something to redPicker (setMinValue for example) will alter the redPicker that is displayed first, but not the second, and I want to know how to alter both of them. – user3788532 Jun 30 '14 at 18:21
0

I found the answer here! Also this person phrases my question much better than I did.

How to specify id when uses include in layout xml file

Community
  • 1
  • 1