0

I'll start by explaining what the finished program should do. First, I would like it to set a OnClickListener for a specific button. Once the button is clicked, I will assume that user has entered something into the EditText. I read in what the user has typed into the EditText, than I check to see if it is a specific string. If it is, than I add a TextView than set the text of that TextView to ask the user what number he would like to call. I add a EditText for the user to enter data into, than a button. Once the user has entered data into that EditText and has clicked the button, I want to create a new object called theCall, and the app makes an intent from within the class phone. Here is my current code:

Main Class:

package com.sam.projectDAD;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ProjectDADActivity extends Activity {
/** Called when the activity is first created. */

private EditText functionName,input;
private Button subCommand,subParam;
private TextView response;
private LinearLayout convo;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    response = new TextView(this);
    convo = new LinearLayout(this);
    input = new EditText(this);
    subParam = new Button(this);

    functionName = (EditText) findViewById(R.id.functionName);
    subCommand = (Button) findViewById(R.id.unFocus);

    subCommand.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            String functionNameText = functionName.getText().toString();
            if (functionNameText == "call") {
                convo.addView(response);
                response.setText("What phone number would you like to call?");
                convo.addView(input);
                convo.addView(subParam);
                subParam.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                            phone theCall = new phone(response.getText().toString());
                    }

                });
            }
        }
    });

}
}

phone class:

package com.sam.projectDAD;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnFocusChangeListener;

public class phone extends Activity {
public phone(String phone){
    call(phone);
}

private void call(String phoneNum){
    String uri = "tel:" + phoneNum.trim() ;
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse(uri));
    startActivity(intent);
}

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.33" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/unFocus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

<EditText
    android:id="@+id/functionName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>


    </LinearLayout>
</ScrollView>

</LinearLayout>

My problem is, when I enter "call" into the initial EditText, nothing happens.

Any help is appreciated!

Thanks,

Sam

2 Answers2

1

You need to add convo to your root LinearLayout. Perhaps like this inside your onClick() method:

LinearLayout root = (LinearLayout) findViewById(R.id.scrollView1).getParent();
root.addView(convo);

Also you cannot use == to compare Strings in Java, you must use equals():

if (functionNameText.equals("call")) {

(For more information on why please read: How do I compare strings in Java?.)

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
0

try following :

  1. Use try catch,

    try { //your code} catch(ActivityNotFoundException activityException) {Log.e("helloandroid dialing example", "Call failed", e); }

  2. Add <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> to your manifest.