I have a question that I want to dial a phone call when I click on a text view which also contain a phone number. But when I click on a Text view it returns an error as:
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CustomCellActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Phone = (Button) findViewById(R.id.button1);
Phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sIntent = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:12345"));
sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(sIntent);
}
});
This code works for me. try it urself.
TextVIew dialers=(TextView)this.findViewById(R.id.dialer);
dialers.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
launchDialer(dialers.getText().toString());
}
});
Change it to :callIntent.setData(Uri.parse("tel:"+phone_no));
and i didnt add this: callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
but its working for me
Add this attribute to your textview component within the XML layout file like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="phone"
android:text="123 123 1234" />
this work for me :)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="phone"
android:background="#ADD8E6"
android:text="555-468-0602" />
Add permission to call in AndroidManifest.xml file.
<uses-permission android:name="android.permission.CALL_PHONE"/>
In your textview of your layout.xml file
<TextView
android:clickable="true"
android:autoLink="phone"
...
...
.../>
In your activity class. Run-time permission check added.
if (Build.VERSION.SDK_INT > 22) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MoreProgramDetailActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 101);
return;
}
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+" + phoneTV.getText().toString().trim()));
startActivity(callIntent);
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+" + phoneTV.getText().toString().trim()));
startActivity(callIntent);
}