Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

[Android] Create button correctly, purely from code.

I do not use an IDE, I just use notepad++ and Ant to compile my project.
(I also build the correct project directory, with a tool I made that uses the android sdk) My only issue is I do not know a lot about android and how to create a button.
current code:
PHP Code

<?php 
final Button button = (Button) findViewById(R.id.button_id);
      
button.setOnClickListener(new View.OnClickListener() {
      public 
void onClick(View v) {

      }
      }); 
But I get error on " final Button button = (Button) findViewById(R.id.button_id);" more specifically R.id.button_id. I got the source from developer.android.com.

Exact error from ant: (PHP highlighting to keep format & spacing)
PHP Code

<?php 
[javac] C:\Users\Jake\Desktop\TestingApp\src\com\wozniak\company\TestActivity.java:14: error: cannot find symbol
  
[javac]      final Button button = (Button) findViewById(R.id.button_id);
  [
javac]             ^
  [
javac]   symbol:   variable id
  
[javac]   location: class R 


Edit: In case you just tell me to use an IDE that might help, I really would like to just get this working :P
So far Eclipse/ADT/etc just are a little slow on my machine.
Didnt test this so it might work, might not.

You are currently trying to GET a button with id "button_id" instead of creating a new one. Probably there is no button that has the id at all.

If you want to create a new button, first you have to get a layout or create a new one. The button will be attached on it later.

Getting the layout: (If you have a layout xml file with a layout declared in it)
Code
LinearLayout layout = (LinearLayout) findViewById(R.id.layoutid);
Replace "layoutid" with the id of your layout. Also if your layout is something else than LinearLayout then change that to the one you are using.

Creating a new layout: (If you dont have a layout xml file, create a new layout from java class)
Code
LinearLayout layout = new LinearLayout(context);
context: If you are in a class that extends Activity you can just use "this", else you have to get it from somewhere else.

Creating the button:
Code
Button button = new Button(context);
button.setText("Hello World!");
button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {

      }
});
context: If you are in a class that extends Activity you can just use "this", else you have to get it from somewhere else.

Adding the button to the layout:
Code
layout.addView(button);

Repeating again: I did not test this so it might not work.
If you want to use a button object created based on some XML (isn't it? I forgot) that defines the behaviour and style of the button, you need to make sure that this XML exists already. Check your layout folder (I think it is... I don't remember exactly where it is located) and see if you can find the button by the name "button_id" that you specified. If it's got a different name, use this different name.

Are you following a tutorial? It smells like copy/paste issues more than anything ;)

Ps.: If I remember right (it's been almost 2 years since I've worked with android hands-on) you shouldn't, per design definition, create components programmatically. They should all be specified in XML somewhere in your layouts.
Generally speaking this is preferred at least. It enables you to change the components easily without having to find it in the code, and it makes sure that you don't mix up XML designing and programmatic designing in your app.
[Image: TEGE4Zx.png]

Puzzle level: 2/5
(Dec 20, 2014 08:31 AM)nOrr Wrote:  If you want to use a button object created based on some XML (isn't it? I forgot) that defines the behaviour and style of the button, you need to make sure that this XML exists already. Check your layout folder (I think it is... I don't remember exactly where it is located) and see if you can find the button by the name "button_id" that you specified. If it's got a different name, use this different name.

Are you following a tutorial? It smells like copy/paste issues more than anything ;)

Ps.: If I remember right (it's been almost 2 years since I've worked with android hands-on) you shouldn't, per design definition, create components programmatically. They should all be specified in XML somewhere in your layouts.
Generally speaking this is preferred at least. It enables you to change the components easily without having to find it in the code, and it makes sure that you don't mix up XML designing and programmatic designing in your app.
No, no tutorial just an example from developer.android.com as I said in my post.
I guess ill be forced into using an IDE anyway :/
So AndroidStudio it might be.
(Dec 20, 2014 07:04 PM)_Wozniak_ Wrote:  No, no tutorial just an example from developer.android.com as I said in my post.
I guess ill be forced into using an IDE anyway :/
So AndroidStudio it might be.

Oh ok, but still it looks like a copy/paste error. The id "button_id" is a very indescriptive, ugly name for a button id - I thought you didn't actually make that one up yourself (apologies if you did, but you then need to also focus on useful naming and naming convensions). :)

Why would you not use an IDE anyway? Android Studio is pretty cool.
[Image: TEGE4Zx.png]

Puzzle level: 2/5


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by LynxJan 3, 2022 04:47 PM 1 184 Jan 3, 2022 04:47 PM
Last Post: yuumi
[C] by lo0srApr 12, 2021 10:32 PM 1 135 Apr 12, 2021 10:32 PM
Last Post: Sajuuk
[C] by ❖Onyx❖Oct 18, 2019 04:48 PM 11 343 Oct 18, 2019 04:48 PM
Last Post: SKIDROOTKIT