-1

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();
    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
Riley Java
  • 43
  • 9

2 Answers2

1

create RadioGroup and then RadioButton inside RadioGraoup

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

        //gettext you write with radiobutton
        String  dailString=callRadioButton.getText().toString();

        //check if you select the radio button
        if(dailString.trim().equals("dailnumber"){
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:0123456789")); 
            startActivity(intent); 
        }else{ 
        // do action with other android button
        }
    } 
});

Edited

java code

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.btn_radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton callRadioButton=(RadioButton)findViewById(checkedId);

                //gettext you write with radiobutton
                String  dailString=callRadioButton.getText().toString();

                //check if you select the radio button
                if(dailString.trim().equals("Dail")){
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:0123456789"));
                    startActivity(intent);
                }else{
                    // do action with other android button
                }
            }
        });
    }
}



***XML***

minifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.radiogroupPermistion">
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

XML'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:id="@+id/btn_radio_group"
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dail"
            android:layout_weight="1"
            android:id="@+id/dail_id"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/otherid"
            android:layout_weight="1"

            />
    </RadioGroup>
</LinearLayout>
</LinearLayout>

Edit 2

Button Listener

java code

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button dailnumber = (Button) findViewById(R.id.btn_call_number);
    dailnumber.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
      Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:0123456789"));
                    startActivity(intent);     }});
  }}
Nouman Shah
  • 534
  • 1
  • 9
  • 22
  • i think you does not specify the action correctly try this – Nouman Shah Aug 15 '16 at 08:59
  • @RileyB did this work? If yes, how did it work? Your notes could be useful for someone who comes across the same issue in future. – Sufian Aug 15 '16 at 10:07
  • it crashes. it does not have any error message in the log. i have no idea why because it does not have any errors in the code. i may need the whole code for java class and the xml. help if you can – Riley Java Aug 15 '16 at 10:33
  • in build.gradle make sure that targetSdkVersion 22 , gradle compileSdkVersion 22 in api 23 you have to add permission at run time – Nouman Shah Aug 15 '16 at 10:49
  • i have done everything to detail but still refuses to run on device(Still no errors on log). Its a Samsung 5, android 4.4.2. Your solution is way too nice, Thank you. i just don't know where the problem is. – Riley Java Aug 15 '16 at 12:27
  • can you believe it i have same device s5 mini and it working on it. so don't know what is problem in your case – Nouman Shah Aug 15 '16 at 12:36
  • thanks. hey quick one, what if i changed the radio button to an actual button, when pressed it brings up the dial menu?? would that still work?? if so, how would you recommend i go about it because my whole entire project is waiting for this function to work(this and an email one). – Riley Java Aug 15 '16 at 14:14
  • yeah it will work on button click Depend upon you requirement. – Nouman Shah Aug 15 '16 at 14:16
  • same requirements, just the button changes from RadioButton to small button and then the phone call function stays the same. – Riley Java Aug 15 '16 at 14:49
  • I saw my mistake. i learned from it. i didn't know the Permissions on android worked like that, mostly the checkPermission one – Riley Java Aug 17 '16 at 07:33
  • glade to know you find your bug – Nouman Shah Aug 17 '16 at 19:10
0

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();     
    }
});
Muazzam A.
  • 647
  • 5
  • 17
  • this is what i have : Button radioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second_screen); RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioButton); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { – Riley Java Aug 15 '16 at 11:28
  • @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { call(); } }); radioButton = (Button) findViewById(R.id.radioButton); } private void call() { Intent in = new Intent(Intent.ACTION_DIAL, Uri.parse("0723263921")); try{ startActivity(in); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(getApplicationContext(), "your Activity not found", Toast.LENGTH_SHORT).show(); } – Riley Java Aug 15 '16 at 11:29