Finding the ID of a widget in a view class

fazal

New Member
What I have is a canvas that takes up nearly all of the screen, then under that I want a row of buttons and some other widgets. So I did this.\[code\]XML Code <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:id="@+id/myLayout"android:orientation="vertical"android:layout_height="match_parent" ><com.zone.manager.Tab3 android:id="@+id/tab3_display" android:layout_width="fill_parent" android:layout_height="620dp" /> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" > <Button android:id="@+id/addZone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add Zone" /> <Button android:id="@+id/helpZone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Help" /> <SeekBar android:id="@+id/seekBar1" android:paddingTop="9dp" android:layout_width="179dp" android:layout_height="wrap_content" /> </LinearLayout>\[/code\]Java Code \[code\]public class Tab3 extends View implements OnTouchListener, OnClickListener { public Tab3(Context context, AttributeSet attrs) { View parent = (View) getParent(); addZone = (Button) parent.findViewById(R.id.addZone); addZone.setOnClickListener(this); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); . . . draws a bunch of stuff } @Override public void onClick(View v) { switch (v.getId()) { case R.id.addZone: String w = "W~20~0~0~0~0~0"; Log.d("ZoneSize", "Zone set"); MyApplication.preferences.edit().putString( "ZoneSize", w ).commit(); MyApplication.preferences.edit().putBoolean("ZoneSizeReady", true).commit(); break; } }\[/code\]However my problem with this is that I believe the code is not reconising where the addZone is. Because when I have the \[code\]addZone.setOnClickListener\[/code\] active my program will crash, but when I don't have it active the layout looks like how I want it. What must I do to fix this?
 
Back
Top