PDA

View Full Version : How Do I Access Items ~Built~ In xml?



androidkeep
04-16-2012, 06:28 PM
Just started java programming, because of Android. Just started Android programming.
How do I access items built in xml?
I am using the hello example that came with the SDK
Here is my HelloAndroid.java:
------------

package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);}
}

------------
I have some checkboxes I want access to
In PACKAGE EXPLORER under:
res->layout->main.xml
I have the IU; I changed the layout to a table layout
-------------
...


<TableRow>
<CheckBox
android:id="@+id/checkbox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="onCheckboxClicked"
android:text="@string/Lt"/>

...
-------------
the program works / compiles; and runs on a phone. but it is just a Face - no code / action.
When I try to access the checkbox from within HelloAndroid.java, it does not know of a ~checkbox01~ "...can not be resolved to a type".
So, How do I access items built in xml?
Where does the meat / code in a Android program go?
In a Package or Class?
And how does one make certain that these Ps or Cs know about the XML objects?

Orby
05-12-2012, 07:58 AM
Hi,

You need to create a new Checkbox object and then play with it... for example... in your OnCreate:


Checkbox mycheck = (CheckBox) findViewById(R.id.checkbox01);

Basically find the view using the id, which in this case is "checkbox01" and override View to CheckBox to make it work.

BR,
Orby