0

cavemen. And Cavebabes. I'm trying to write a simple app to call a saved number. Below is the code, XML and manifest file. The code compiles but when I try to run it on my phone, running Marshmallow, the UI displays ok but when I press the button nothing happens. What am I doing wrong? A thousand thanks

package com.example.malcolmshingirai.mycall;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Button mCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCall = (Button) findViewById(R.id.button2);
    mCall.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel: 0123456789"));
            if (ActivityCompat.checkSelfPermission(MainActivity.this,            Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            startActivity(callIntent);
        }
    });
    }
}

XML----------------------------------------------------

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.malcolmshingirai.mycall.MainActivity">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/textView"
    app:layout_constraintVertical_bias="0.502" />

    <Button
    android:id="@+id/button2"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:text="@string/call_button"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

    </android.support.constraint.ConstraintLayout>

Manifest permissions --------------------------------------

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.malcolmshingirai.mypay">

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

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    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>

Again, a thousand thanks.

0 Answers0