I am trying to setup an activity and when I try to assign some of my views to names in the .java file its telling that R.layout.bathing does not have field datePicker1 and it does have it. Here is my two code snippets. What should I change to get this to work?
bathing.java
setContentView(R.layout.bathing);
setTitle("Please Select Bathing Options");
bathdate = (DatePicker)findViewById(R.layout.bathing.datePicker1);
bathtime = (TimePicker)findViewById(R.layout.bathing.timePicker1);
add = (Button)findViewById(R.layout.bathing.button1);
bathing.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="@string/bath_tracking"
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/datePicker1"
android:layout_marginTop="51dp"
android:text="@string/date"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/datePicker1"
android:layout_below="@+id/datePicker1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/timePicker1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="50dp"
android:text="@string/time"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/timePicker1"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Yes" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:text="@string/No" />
</RadioGroup>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radioGroup1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="36dp"
android:text="@string/hair"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_alignParentRight="true"
android:layout_below="@+id/radioGroup1"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:text="@string/Add"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="@string/comment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
You have to access view objects via their id, instead of
use
and so on. IDs are global, they are not specific to a certain layout.
That doesn’t neccessarily mean that each view has to have an unique name. The most important thing to know is that
findViewById()only searches in the view hierachy of the current activity (you can try this by calling it beforesetContentView()– it will always return null). In the end it means that IDs should be unique in a single layout file.There is no problem in having the same id in multiple layouts. Often it’s even useful. Consider having an activity with a normal layout in
res/layout/main.xmland a different layout for the landscape format inres/layout-land/main.xml. Android will manage these layouts for you. If you give views with identical purposes a different, unique id, you would have to do something along this in your code (pseudocode example):If both have the same ID you won’t have a conflict, but can access both via a single line call of
findViewById().