Tabak
Tabak

Reputation: 13

File picker dialog in PreferenceFragment

I want to get a pick file dialog in my PreferenceActivity how do I get to that? Can I override onClick for PreferenceActivity somehow? Android API 14.

Here's my PreferensActivity:

    import android.preference.PreferenceActivity;
    import java.util.List;
public class SettingsActivity extends PreferenceActivity {

@Override
public boolean onIsMultiPane() {
    return true;
}

public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.pref_head, target);
    }
}

Header xml is:

    <?xml version="1.0" encoding="utf-8"?>
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">
<header
    android:fragment="com.bfx.rfid.FragmentSetApp"
    android:icon="@android:drawable/ic_menu_call"
    android:title="Application"
    android:summary="Application settings">
</header>
<header
    android:fragment="com.bfx.rfid.FragmentSetConnection"
    android:icon="@android:drawable/ic_menu_call"
    android:title="Connectivity"
    android:summary="Connection settings">
</header>
</preference-headers>

PreferenceFragment class:

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class FragmentSetApp extends PreferenceFragment {

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.fragment_set_app);
  }
}

PreferenceFragment xml is:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
    android:key="work_directory"
    android:title="Folder to work with">
</Preference>
<PreferenceCategory
    android:title="Work with a database file">
    <CheckBoxPreference
        android:key="DB_default"
        android:summary="Choose a default database file or pick one"
        android:title="Choose a database file"
        android:defaultValue="true"/>
    <EditTextPreference
        android:key="DB_URI"
        android:title="Database file"
        android:dependency="DB_default">
    </EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>

Upvotes: 1

Views: 2216

Answers (1)

MartinHaTh
MartinHaTh

Reputation: 1447

I don't think Andorid have a native file chooser, so you'll have to implement one yourself, or find a library.

You could then use the android:onClick property in PreferenceFragment.xml:

<EditTextPreference
    android:key="DB_URI"
    android:title="Database file"
    android:onClick="startFileChooser"
    android:dependency="DB_default"> 

and put this in you PreferenceFragment:

public void startFileChooser(MenuItem i){
     // Start the file chooser here
}

Of course, how you would to that depends on what file chooser you decide to go with. You would probably end up with the path to the selected file, which you would insert into SharedPreference.

Upvotes: 3

Related Questions