airbourne
airbourne

Reputation: 119

Android Button Onclick listener not working

I have two buttons on my activity. One is working perfectly and one is not. I can't seem to find the problem. Here is the code. The create_profile button is not working. Can someone please tell me why?

    private Button create_profile,select_contact;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_main);

    select_contact = (Button)findViewById(R.id.add_contact);
    create_profile = (Button)findViewById(R.id.create_button);

    select_contact.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
            Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
            startActivityForResult(intentPickContact, PICK_CONTACT);    
        }
    });

    create_profile.setOnClickListener(new OnClickListener() {

        @SuppressLint("ShowToast")
        @Override
        public void onClick(View arg0) {
            Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG);
            System.out.println("PRessed");

        }
    });
    }

Here is the xml code

          <Button 
                    android:id="@+id/add_contact"
                    android:layout_width="85dp"
                    android:layout_height="wrap_content"
                    android:text="Add"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="2dp"
                    android:textSize="19dp"/>
        <Button 
            android:id="@+id/create_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Create Profile"
            android:textSize="18sp"
            android:layout_marginTop="8dp"/>

I have checked everything but can't seem to find the problem.

Upvotes: 1

Views: 7911

Answers (2)

user2235615
user2235615

Reputation: 1533

You can try to create a OnClickListener variable and than set both button to the same variable and separate them with a switch case statement like this:

OnClickListener onClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add_contact:
             //Do what you want for select_contact
            break;
        case R.id.create_button:
            //Do what you want for create_button
            break;
        default:
            break;
        }


    }
};

In your OnCreate method set it like this:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        select_contact = (Button)findViewById(R.id.add_contact);
        create_profile = (Button)findViewById(R.id.create_button);
        select_contact .setOnClickListener(onClickListener);
        create_profile .setOnClickListener(onClickListener);

}

If you want to check it, just add a breakpoints under the line case R.id.create_button

Even if this not solved your problem, I believe that it easier and more comfortable to work like this with onClickListener method :)

Upvotes: 0

Piyush
Piyush

Reputation: 1973

HI why are you not showing the toast.Toast code should be like this.

 Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG).show();

Upvotes: 6

Related Questions