Kodi
Kodi

Reputation: 109

Phone call click for textview

Okay so I've looked and tried to make sense of some other code that I've found but nothing has been working out for me. I'm trying to get the user to click the textview and have it take them to their phones dial-er.

Here's the Java:

public class Info extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.moreinfo);
    Button appoint =(Button) findViewById(R.id.btnAppoint);
    TextView phone =(TextView) findViewById(R.id.phoneTxt);
    String url = phone.getText().toString();
   Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
    appoint.setOnClickListener(new OnClickListener () {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(Info.this, EmailContact.class));
        }
    });

    phone.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:+"+phone.getText().toString().trim()));
            startActivity(callIntent );
        }
});
}
}

I've also put android: clickable = true so I don't see if that's the problem. Any help would be appreciated!

Upvotes: 0

Views: 2156

Answers (2)

jtt
jtt

Reputation: 13541

Based on your description its red because if you're going to use an class method level object within an anonymous obejct, the object must be defined as final.

final TextView phone =(TextView) findViewById(R.id.phoneTxt);

And for further measure, ensure you are using the right permission to do the call action in your manifest:

<uses-permission android:name="android.permission.CALL_PHONE" />

Upvotes: 1

Khantahr
Khantahr

Reputation: 8518

Change phone.getText().toString().trim() to use the View object supplied to the onclick method:

phone.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent callIntent = new Intent(Intent.ACTION_DIAL);
    callIntent.setData(Uri.parse("tel:+"+((TextView)arg0).getText().toString().trim()));
    startActivity(callIntent );
}
});

Additionally, if you just want to show the dialer with a phone number loaded, you're using the wrong intent action, Intent.ACTION_DIAL instead of Intent.ACTION_CALL will show the dialer. The Intent.ACTION_CALL that you're using will actually initiate the phone call, and to make that work, you need to add the appropriate permission to your Manifest:

<uses-permission android:name="android.permission.CALL_PHONE" />

Upvotes: 2

Related Questions