Search code examples
androidbuttonclick

Button's onClick() not getting called


I am trying to make a call button whereby when a user clicks on a button, it makes a phone call.

Now I have the Radio Button and the code for the button but when clicked it does nothing.

I already have the CALL_PHONE and READ_PHONE_STATE permissions set in the manifest but still not working when the button is pressed.

Here is my code for the radio button in the activity:

Button radioButton;
call();


radioButton = (Button) findViewById(R.id.radioButton);
}

private void call()
{
    Intent in = new Intent(Intent.ACTION_DIAL,Uri.parse("0000000000"));
    try{
        startActivity(in);
    }
    catch (android.content.ActivityNotFoundException ex)
    {
        Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
    }
}

Solution

  • First of all you are not calling the Call method on radio button click. Here is the Workaround of your problem.

    First Way:

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                   call();
    } });
    

    And Call your Method like this as you are doing

    private void call(){
        Intent in = new Intent(Intent.ACTION_DIAL,Uri.parse("0000000000"));
        try{
            startActivity(in);
        }
        catch (android.content.ActivityNotFoundException ex)
        {
            Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
        }
    }
    

    Second Way:

    Button myButton=(Button)findViewById(R.id.buttonId);
    
    myButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v){
           call();     
        }
    });