Android Community
Results 1 to 5 of 5

Thread: getting buttons to work

  1. #1
    Join Date
    Sep 2008
    Posts
    32

    Default getting buttons to work


    Hey

    I am now at a stage where I need buttons to work so that I can get my code to react to a button press, for the time being, what I want to do is to have my layout view change once a button is pushed.

    So as like in a menu, you push a button and you are taken to another screen. Like that.

    I read somewhere that you have to create some way for the system to recognize button presses, found something short about it on this site:

    http://code.google.com/android/devel...fications.html

    But my java knowledge is still a bit lacking, I got along ok'ish on the xml now, but now I need a way to connect different layouts together so I can have a branching menu like system.

    I imagine it would be something like creating a new class designed for this specifically and then that is called or something?

    Hope someone can point me in the right direction.

  2. #2
    Join Date
    Oct 2008
    Posts
    516

    Default Re: getting buttons to work

    I havent looked much into the layouts and how to change them, but for a button press you need to create an event listener,, see below:

    final Button ButtonName= (Button) findViewById(R.id.buttonID);

    ButtonName.setOnClickListener(new View.OnClickListener()
    {
    public void onClick(View v)
    {
    // Your code here

    }
    });

  3. #3
    Join Date
    Sep 2008
    Posts
    32

    Default Re: getting buttons to work

    Thanks, will look into this.

  4. #4
    Join Date
    Sep 2008
    Posts
    32

    Default Re: getting buttons to work

    Quote Originally Posted by jmejiaa View Post
    I havent looked much into the layouts and how to change them, but for a button press you need to create an event listener,, see below:

    final Button ButtonName= (Button) findViewById(R.id.buttonID);

    ButtonName.setOnClickListener(new View.OnClickListener()
    {
    public void onClick(View v)
    {
    // Your code here

    }
    });
    Ok to specify my question a bit more based on this:

    Take this code here, taken from a standard hello world project that I have continued to build on, if I wanted to make buttons work with this, where would I put the code that you wrote? I have buttons and such made in xml graphics files, and they have been given ids that are present in the R.id.name


    Also another question, in the code you write you write "final Button Buttonname=(Button) findViewById(R.id.buttonID);"

    What does the "final" mean and do?


    Code:
    package xxxx.xxxx.hello;
    
    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 savedInstances) {
            super.onCreate(savedInstances);
            setContentView(R.layout.main);
        }
    }

  5. #5
    Join Date
    Nov 2008
    Posts
    67

    Default Re: getting buttons to work


    Quote Originally Posted by KanedaSyndrome View Post
    What does the "final" mean and do?
    http://java.sun.com/docs/books/jls/t...es.html#4.12.4

    similar meaning to "const"

    I would take a shot at your first question too, but i can't guarantee it. I think you want to put that button code in your OnCreate function.

Similar Threads

  1. G1 keyboard buttons ?
    By Roman383 in forum General Phone Chat
    Replies: 7
    Last Post: 12-26-2010, 11:45 PM
  2. Dial and Home buttons stopped working
    By lovehatesex in forum General HTC Chat
    Replies: 54
    Last Post: 10-03-2010, 09:18 AM
  3. Official: Icons/Buttons Thread
    By Lukster in forum Developers Guild
    Replies: 9
    Last Post: 11-05-2008, 06:49 PM
  4. Painting out the red and green buttons on the G1
    By overmedium in forum Developers Guild
    Replies: 8
    Last Post: 10-18-2008, 10:06 AM
  5. Volume Buttons / GUI
    By embrace LB in forum General Phone Chat
    Replies: 19
    Last Post: 10-07-2008, 02:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •