Newbie Question

This is a discussion on Newbie Question within the Beginning Developers forums, part of the Android Development category; Hello Guys, i am trying to learn to develope application on android using Eclipse and Java. So here is the code, and my aim is ...

Results 1 to 2 of 2

Thread: Newbie Question

  1. #1
    Android Lurker moses2011's Avatar
    Join Date
    Dec 2011
    Posts
    1

    Newbie Question







    Hello Guys, i am trying to learn to develope application on android using Eclipse and Java.
    So here is the code, and my aim is to make when i click button 1 the content of edittext 1 be copied to edittext 2 but seems to no avail.

    public
    class HelloActivity extends Activity implements OnClickListener {


    /** Called when the activity is first created. */




    private Button button1;


    private Button button2;


    private Button button3;


    @Override


    publicvoid onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);

    setContentView(R.layout.
    main);




    button1 = (Button)findViewById(R.id.button1);


    button1.setOnClickListener(this);




    button2 = (Button)findViewById(R.id.button2);


    button2.setOnClickListener(this);




    button3 = (Button)findViewById(R.id.button3);


    button3.setOnClickListener(this);

    }




    publicvoid onClick(View arg0) {


    // TODO Auto-generated method stub

    EditText nameField = (EditText) findViewById(R.id.edittext1);

    String name = nameField.getText().toString();


    if (name.length() == 0) {


    new AlertDialog.Builder(this)

    .setMessage(R.string.error_text1_missing)

    .setNeutralButton(R.string.error_ok,
    null)

    .show();


    return;

    }




    if (arg0 == button1)

    {




    int resourceId = arg0 == button1 ? R.string.edittext1; <=== the error



    String greeting = getResources().getString(resourceId, name);

    Toast.makeText(
    this, greeting, Toast.LENGTH_LONG).show();



    EditText greetingField = (EditText) findViewById(R.id.edittext2);

    greetingField.setText(greeting);

    }

  2. Android.net is the premier Android Forum. Registered users do not see these ads. .

  3. #2
    App Developer alostpacket's Avatar
    Join Date
    Jan 2011
    Location
    NY
    Posts
    240
    Thanked
    32 times
    Twitter
    alostpacket
    R.string.yourresourceID is an int

    The parameter that is passed to onClick is the View object that was clicked. It looks like you are trying to compare the View with the int in your short-hard if statement ( the one with like a=b? x:y; )

    See if this makes sense:

    Code:
    public void onClick( View arg0 )
    {
    
       //first lest get the ID of the view that was actually clicked
       //to do this we'll use view.getId();  this will return the int resource identifier
    
        int resourceId = arg0.getId();
    
    
    
        //now we can compare the resource ID of what was clicked with the resource ID of button1
    
        if ( resourceId == R.string.edittext1 )
        {
            //do something cool here
        }
    
    
    }
    //end onClick();
    
    

    Last edited by alostpacket; 12-10-2011 at 04:27 PM.

Remove Ads

http://www.scramblerducati.org/

Sponsored Links

Posting Permissions

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

Similar Threads

  1. a very newbie android question...please help
    By hasanz in forum Rescue Squad Help
    Replies: 2
    Last Post: 10-03-2011, 07:01 AM
  2. Newbie here from NC
    By bsbabcock in forum Introductions & Site Assistance
    Replies: 8
    Last Post: 05-08-2011, 12:46 AM
  3. Very Newbie
    By qingshan in forum Introductions & Site Assistance
    Replies: 5
    Last Post: 03-27-2011, 11:30 AM
  4. Newbie
    By Tammy Wheeler Perry in forum Introductions & Site Assistance
    Replies: 5
    Last Post: 03-24-2011, 09:26 PM
  5. newbie
    By jblanky in forum Introductions & Site Assistance
    Replies: 1
    Last Post: 12-23-2010, 12:44 AM

Search tags for this page

newbie for android development

Click on a term to search our sites for related topics.
Android Forum