Reputation: 65
I am trying to add multiple buttons to my app so if you click one you automatically call a certain person, but now I'm stuck on the call-action, how can I make a call when there is being clicked on the button? I have the following code for my activity named telefoonnummers.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class Telefoonnummers extends Activity {
Button mHALbellen;
Button mWITbellen;
Button mWDGbellen;
Button mVlierbellen;
Button mHotelbellen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_telefoonnummers);
mHALbellen = (Button) findViewById(R.id.button1);
mWITbellen = (Button) findViewById(R.id.button3);
mWDGbellen = (Button) findViewById(R.id.button4);
mVlierbellen = (Button) findViewById(R.id.button5);
mHotelbellen = (Button) findViewById(R.id.button6);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_telefoonnummers, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void vanHalbellen(View view){
if (view == mHALbellen){
//WHICH CODE SHOULD BE HERE TO MAKE A PHONECALL WHEN THE BUTTON mHALBELLEN IS PRESSED??
}
}
}
Would you guys please help???
Upvotes: 0
Views: 133
Reputation: 82553
Add the following permission to your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
And then execute this Intent when the button is clicked:
String uri = "tel: phone_number_here";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
You can also use Intent.ACTION_DIAL
instead of Intent.ACTION_CALL
. This shows the dialer with the number already entered, but allows the user to decide wether to actually make the call or not. ACTION_DIAL
doesn't require the CALL_PHONE
permission.
Upvotes: 4