Results 1 to 3 of 3
  1. #1
    nour is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default how to choose between two answers in android Java

    i am new to android develop ,
    and i have that code that it should print on screen "hello" if i put in the text box "1"
    and "hi" if 2 , but it dosent work
    so any help will be accepted
    and here is the code:
    Java Code:
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class FindcityActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button button = (Button) findViewById(R.id.button1);
            final EditText nameField = (EditText) findViewById(R.id.editText1);
            final String name = nameField.getText().toString();
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
    
    if(name.equals("1"))
    {
    	 Toast.makeText(FindcityActivity.this, "Hello ", Toast.LENGTH_SHORT).show();
    }
    else if(name == "2")
    {
    	 Toast.makeText(FindcityActivity.this, "Hi ", Toast.LENGTH_SHORT).show();
    }
                	
                }
              });   
            
        }
      
    }
    thanks for the helpers

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: how to choose between two answers in android Java

    You have to evaluate the value of the textfield when clicking on the button, not before (because there you will get an empty string :D)
    And the first one ist right! Use equals instead of "==".

    Summary: remove the line 17 and change the listener code to
    Java Code:
    		button.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				if (nameField.getText().toString().equals("1")) {
    					Toast.makeText(FindcityActivity.this, "Hello ",Toast.LENGTH_SHORT).show();
    				} else if (nameField.getText().toString().equals("2")) {
    					Toast.makeText(FindcityActivity.this, "Hi ",Toast.LENGTH_SHORT).show();
    				}
    			}
    		});

  3. #3
    nour is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: how to choose between two answers in android Java

    thanks that works now

Similar Threads

  1. 15 most Java Interview Questions With Answers
    By manideep.d132 in forum Reviews / Advertising
    Replies: 2
    Last Post: 01-23-2012, 09:26 PM
  2. need answers to this java program
    By peixd in forum New To Java
    Replies: 2
    Last Post: 06-14-2010, 05:23 AM
  3. Java- Interview Questions and Answers
    By nishants in forum Java Software
    Replies: 0
    Last Post: 04-17-2010, 01:48 PM

Tags for this Thread

Posting Permissions

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