Hi all!

I bought a new HTC desire Z
I'm swedish so I wanted Å, Ä and Ö on my QWERTY-keyboard on the mobile but
I couldn't get a HTC desire Z with Å, Ä and Ö.
So I decided to fix it on my own. I am developing a SMS-sender where you can
type Å by pressing AAA, Ä by pressing AE and Ö by pressing OO.

It works fine. I can send SMS with it. The next step is to, instead of memorizing
and typing the phone number in the number input, make the contacts in my phone
available for the app. Then I just can load one of these contacts instead of typing.

I succeeded with listing the contacts, but When I press on the contact I want to message
to, I want the app to take the number from that contact and insert it on the number input.
and That's where the problems start. here is the code:

package swedish.txt;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.*;

public class SwedishText extends Activity {
    private static final int PICK_CONTACT = 3;
	/** Called when the activity is first created. */
	EditText editor;
	EditText number;
	
	private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this,SwedishText.class), 0);                
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        number = (EditText) findViewById(R.id.phonenumber);
        
        editor = (EditText) findViewById(R.id.text);
        editor.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String txt = editor.getText().toString();
				while (txt.contains("ae")||txt.contains("oo")||txt.contains("aaa")){
					txt = txt.replace("oo", "ö");
					txt = txt.replace("aaa", "å");
					txt = txt.replace("ae", "ä");
				}
				editor.setText(txt);
				editor.setSelection(txt.length());
			}
		});
        
        Button bQUIT = (Button) findViewById(R.id.quit);
        bQUIT.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        
        Button bSEND = (Button) findViewById(R.id.send);
        bSEND.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String txt = editor.getText().toString();
				String num = number.getText().toString();
				if (num.length()>0&&txt.length()>0){
					sendSMS(num, txt);
				}else{
					Toast.makeText(getBaseContext(), 
							"Please enter both phone number and message.", 
	                        Toast.LENGTH_SHORT).show();
				}
			}
		});
        
        Button bContacts = (Button) findViewById(R.id.contacts);
        bContacts.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
			    startActivityForResult(intent, PICK_CONTACT);
			}
		});
        
        
    }
    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
 
        // Identify our request code
        switch (reqCode) {
        case PICK_CONTACT:
 
            // If the operation succeeded
            if (resultCode == Activity.RESULT_OK) {
                 
                // A StringBuilder used to build the String
                // that we will display in our TextView
                String sb="";
                 
                // Get the Uri for the data returned by the contact picker
                // This Uri identifies the person picked
                Uri contactData = data.getData();
                 
                // Query the table
                Cursor contactsCursor = managedQuery(contactData,
                        null, null, null, null);
 
                // If the cursor is not empty
                if (contactsCursor.moveToFirst()) {
                     
                    // Get the id name and phone number indicator
                    String id = contactsCursor.getString(contactsCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String name = contactsCursor.getString(contactsCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    String hasPhoneNumber = contactsCursor.getString(contactsCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
 
                    String num="";
                     
                    // If the contact has a phone number
                    if (Integer.parseInt(hasPhoneNumber) > 0) {
                         
                        // Build the Uri to query to table
                        Uri myPhoneUri = Uri.withAppendedPath(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
                         
                        // Query the table
                        Cursor phoneCursor = managedQuery(
                                myPhoneUri, null, null, null, null);
 
                        // Get the phone numbers from the contact
                        int i = 0;
                        
                        phoneCursor.moveToPosition(0);
                        String phoneNumber = phoneCursor.getString(phoneCursor
                                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        num = phoneNumber;
                        number = (EditText) findViewById(R.id.phonenumber);
                        // Set the text in our TextView to display what we got
                        number.setText(num);
                        
                    } else {
                    }
                    
                }
            }
            break;
        }
    }
}

everything works, until I insert this code:

number = (EditText) findViewById(R.id.phonenumber);
// Set the text in our TextView to display what we got
number.setText(num);

the most important code. That code will insert the phone number into the textfield!

When I install it on the phone and select the contact, The hole app crashed.


Can someone help me? it would be excellent to select the contact I want to message to
instead of typing.

PS! if there are any bad english in here, I'm sorry for that. I have been told I'm Swedish:)

can you put here your layout xml file?
I think, you put wrong properties for number EditText.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.