When using aFileChooser at API Level < 11 ActionBar is not supported. This is no longer required because the AppCompatLibrary supports ActionBar on lower API levels.
Changing this is easy. Add the android-support-v7-appcompat library to the project, adjust a few lines of code and set Theme.AppCompat as the parent theme for all Activities.
Tested with the Example App on Android 2.3.3 in the Emulator.
Here is my Code for FileChooserActivity to achieve this.
/*
Copyright (C) 2013 Paul Burke
*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Main Activity that handles the FileListFragments
*
@version 2013-06-25
@author paulburke (ipaulpro)
*/
// Support Lib V7 ActionBarActivity instead of Fragment
public class FileChooserActivity extends ActionBarActivity implements
OnBackStackChangedListener, FileListFragment.Callbacks {
public static final String PATH = "path";
public static final String EXTERNAL_BASE_PATH = Environment
.getExternalStorageDirectory().getAbsolutePath();
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mFragmentManager.popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
Add the initial Fragment with given path.
*/
private void addFragment() {
FileListFragment fragment = FileListFragment.newInstance(mPath);
mFragmentManager.beginTransaction()
.add(android.R.id.content, fragment).commit();
}
/**
"Replace" the existing Fragment with a new one using given path. We're
really adding a Fragment to the back stack.
@param file The file (directory) to display.
*/
private void replaceFragment(File file) {
mPath = file.getAbsolutePath();
FileListFragment fragment = FileListFragment.newInstance(mPath);
mFragmentManager.beginTransaction()
.replace(android.R.id.content, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(mPath).commit();
}
/**
Finish this Activity with a result code and URI of the selected file.
@param file The file selected.
*/
private void finishWithResult(File file) {
if (file != null) {
Uri uri = Uri.fromFile(file);
setResult(RESULT_OK, new Intent().setData(uri));
finish();
} else {
setResult(RESULT_CANCELED);
finish();
}
}
/**
Called when the user selects a File
@param file The file that was selected
*/
@Override
public void onFileSelected(File file) {
if (file != null) {
if (file.isDirectory()) {
replaceFragment(file);
} else {
finishWithResult(file);
}
} else {
Toast.makeText(FileChooserActivity.this, R.string.error_selecting_file,
Toast.LENGTH_SHORT).show();
}
}
/**
Register the external storage BroadcastReceiver.
*/
private void registerStorageListener() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
registerReceiver(mStorageListener, filter);
}
When using aFileChooser at API Level < 11 ActionBar is not supported. This is no longer required because the AppCompatLibrary supports ActionBar on lower API levels.
Changing this is easy. Add the android-support-v7-appcompat library to the project, adjust a few lines of code and set Theme.AppCompat as the parent theme for all Activities.
Tested with the Example App on Android 2.3.3 in the Emulator.
Here is my Code for FileChooserActivity to achieve this.
/*
package com.ipaulpro.afilechooser;
import java.io.File;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager.BackStackEntry; import android.support.v4.app.FragmentManager.OnBackStackChangedListener; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; // Support Lib V7 import android.support.v7.app.ActionBarActivity; // Support Lib V7 import android.view.Menu; import android.view.MenuItem; import android.widget.Toast;
/**
@author paulburke (ipaulpro) */ // Support Lib V7 ActionBarActivity instead of Fragment public class FileChooserActivity extends ActionBarActivity implements OnBackStackChangedListener, FileListFragment.Callbacks {
public static final String PATH = "path"; public static final String EXTERNAL_BASE_PATH = Environment .getExternalStorageDirectory().getAbsolutePath();
private FragmentManager mFragmentManager; private BroadcastReceiver mStorageListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, R.string.storage_removed, Toast.LENGTH_LONG).show(); finishWithResult(null); } };
private String mPath;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
}
@Override protected void onPause() { super.onPause();
}
@Override protected void onResume() { super.onResume();
}
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState);
}
@Override public void onBackStackChanged() {
}
@Override public boolean onCreateOptionsMenu(Menu menu) { boolean hasBackStack = mFragmentManager.getBackStackEntryCount() > 0;
}
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mFragmentManager.popBackStack(); return true; }
}
/**
/**
FileListFragment fragment = FileListFragment.newInstance(mPath); mFragmentManager.beginTransaction() .replace(android.R.id.content, fragment) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) .addToBackStack(mPath).commit(); }
/**
/**
/**
/**