Discussion Forums  >  Images, Documents, File Locations

Replies: 10    Views: 126

LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
11/09/12 01:19 PM (13 years ago)

Buttons

When I am in the xml file in Eclipse, I name the button id @+/button1, but when I go to the java class and findViewByID(R.id. I can never find it. What am I missing? LA
 
Intrege
Veteran developer
Profile
Posts: 196
Reg: Sep 12, 2011
Philadelphia, P...
3,160
like
11/09/12 01:37 PM (13 years ago)
in the java class it should be Button myButton - (Button) findViewById(R.id.button1); where R.id.button1 defines the button in the xml. Take a look at David's video on Youtube for this. http://www.youtube.com/watch?v=_VMtRCaIksc&feature=plcp You will be a pro in no time, following his instructions.
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
11/09/12 01:39 PM (13 years ago)
Hey @Yousicc, Yes but when you type in R.id. it should show up in the dropdown menu suggestion, otherwise it dont recognize it as my button. LA
 
Intrege
Veteran developer
Profile
Posts: 196
Reg: Sep 12, 2011
Philadelphia, P...
3,160
like
11/09/12 01:43 PM (13 years ago)
you would have to type in R.id (and right after the d press the spacebar and Ctrl keys at the same time) this will bring the list up for you.
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
11/09/12 01:45 PM (13 years ago)
Yes i know but it's not there..lol example of my xml button: <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="58dp" android:layout_marginTop="54dp" android:text="Button" /> suggestions? LA
 
Intrege
Veteran developer
Profile
Posts: 196
Reg: Sep 12, 2011
Philadelphia, P...
3,160
like
11/09/12 01:48 PM (13 years ago)
Sorry for not understanding. You have the XML correct. Can you post just the buttons java activity also. Thanks
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
11/09/12 01:51 PM (13 years ago)
Well lol no..I started over. I'll try it again then post. Where is the best place in the code in java to place the button? LA
 
Intrege
Veteran developer
Profile
Posts: 196
Reg: Sep 12, 2011
Philadelphia, P...
3,160
like
11/09/12 01:54 PM (13 years ago)
no problem lol. within the OnCreate is fine.
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
11/09/12 01:59 PM (13 years ago)
Hey @Yousicc, I did it just like you said: Button myButton - (Button) findViewById(R.id.button1); it states: the value of the local variable myButton is not used. Suggestion? LA
 
Intrege
Veteran developer
Profile
Posts: 196
Reg: Sep 12, 2011
Philadelphia, P...
3,160
like
11/09/12 02:12 PM (13 years ago)
Take a look at the video link i posted earlier. start at 15:08 where he talks about buttons, it should put you on track.
 
LA
Aspiring developer
Profile
Posts: 3280
Reg: Aug 16, 2012
Jerseyville, IL
42,900
like
11/09/12 03:01 PM (13 years ago)
Ok, I think I got it but on the video he is going backwards, so my screen is called Contact. Here is the code but do i call the home screen Main? //button was clicked... Intent nextIntent = new Intent(MainActivity.this,ContactActivity.class); Contact.this.startActivity(nextIntent); LA
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.