Spent a lot on working on the preferences settings in android google also couldn't help me at last I could do some experiement and made it work.
Goal : Open the Contacts and fill the phone number in one of the inputs for Preferences.
e.g You want to set the Number only Edit text
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_activity);
Preference preference = findPreference("EMERGENCY_PH_NO") ;
EditTextPreference editTextPreference = (EditTextPreference) preference;
}
}
now its easy just grab the handle and set the longpress event
All the code is given below
public class SettingsFragment extends PreferenceFragment {
EditText m_emergency_number = null;
final int EMERGENCY_CONTACT_PICK = 1337;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_activity);
Preference preference = findPreference("EMERGENCY_PH_NO") ;
EditTextPreference editTextPreference = (EditTextPreference) preference;
//TextView TextView = (TextView) editTextPreference.getEditText();
m_emergency_number = (EditText) editTextPreference.getEditText();
m_emergency_number.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
openContact(EMERGENCY_CONTACT_PICK);
return true;
}
});
}
void openContact(int who)
{
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
intent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(intent, who);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent aData) {
// TODO Auto-generated method stub
if (resultCode == Activity.RESULT_OK)
{
Uri contactUri = aData.getData();
//Intent intent = new Intent(Intent.ACTION_GET_CONTENT,contactData);
//startActivityForResult(intent, 1336);
String[] projection = {Phone.NUMBER};
Cursor cursor = getActivity().getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
if(EMERGENCY_CONTACT_PICK == requestCode)
{
m_emergency_number.setText(number);
}
}
}
}
No comments:
Post a Comment