Question

I'm trying to write an app using the Google Drive SDK. I'd like to use a Preference object in a PreferenceActivity that allows the user to select the Google account that they want to upload files to. I've been trying to use an EditTextPreference to launch the account picker intent. It works. However, after selecting the account, the EditText dialog appears. Is it possible to disable the dialog? Or am I going about this the wrong way?

preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
  <PreferenceCategory 
    android:title="@string/title_upload_category">
    <EditTextPreference
      android:title="@string/title_upload_acct"
      android:key="@string/key_upload_acct"/>
  </PreferenceCategory>
</PreferenceScreen>

PrefsFragment

public static class PrefsFragment extends PreferenceFragment{

@Override
public void onCreate(Bundle savedinstancestate) {
  GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(getActivity().getApplicationContext(), Arrays.asList(DriveScopes.DRIVE));
  EditTextPreference acctValue = (EditTextPreference)findPreference(getString(R.string.key_upload_acct));
  acctValue.setOnPreferenceClickListener(new OnPreferenceClickListener(){

    @Override
    public boolean onPreferenceClick(Preference preference) {
        startActivityForResult(credential.newChooseAccountIntent(), 1);
        return false;
    }               
  });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && data != null){
      String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
      acctValue.setSummary(accountName);
      acctValue.setText(accountName);
    }
}
Was it helpful?

Solution

You are doing everything correct except you should use a Preference instead of an EditTextPreference. Then the dialog won't show up, and the functionality of the preference is handled by you when you set the onPreferenceClickListener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top