I'm using SharedPreferences in my Android app in the standard way. On the HTC WildFire device (resolution 240x320), the EditText is squashed up when the virtual keyboard is displayed.
Has anyone else come across this is there a solution? I've been stumped for days.
My code/XML is pretty straightforward:
public class PrefsActivity extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// don't display hidden preferences
getPreferenceScreen().removePreference(findPreference("hidden_prefs"));
}
}
And my preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="User Preferences">
<EditTextPreference
android:key="profile"
android:title="Profile"
android:summary="Your profile name"
android:name="Profile"/>
<EditTextPreference
android:key="password"
android:title="Password"
android:summary="Your password"
android:name="Password"
android:password="true"/>
<EditTextPreference
android:name="Server"
android:summary="The server to use"
android:title="Server"
android:key="server"/>
<EditTextPreference
android:name="Secret"
android:summary="The server secret"
android:title="Secret"
android:password="true"
android:key="secret"/>
<CheckBoxPreference
android:title="On Demand"
android:defaultValue="true"
android:summary="Check to enable On Demand"
android:key="on_demand"/>
<ListPreference
android:title="Date"
android:summary="Set the type of date used"
android:key="date"
android:defaultValue="next"
android:entries="@array/prefs_date_keys"
android:entryValues="@array/prefs_date_values" />
</PreferenceCategory>
<PreferenceCategory android:key="hidden_prefs" android:title="Hidden Preferences">
<EditTextPreference
android:name="Last Project ID"
android:summary="The last Project ID"
android:title="Last Project ID"
android:key="last_project_id"
android:inputType="number"/>
<EditTextPreference
android:name="Fast Sync"
android:summary="Use Fast Sync"
android:title="Fast Sync"
android:key="fast_sync"
android:inputType="number"/>
</PreferenceCategory>
I know this answer most likely comes too late, but in case anyone else is looking for a solution to this truly annoying problem, this is how I worked around it, trying to keep it as vanilla as possible (to me, custom layouts were a definite no-no in this case):
public class MyEditTextPreference extends EditTextPreference
{
public MyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void showDialog(Bundle bundle) {
super.showDialog(bundle);
Dialog dialog = getDialog();
if(dialog != null) {
Window window = dialog.getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE |
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
}
}
Using this code, the on-screen keyboard will be displayed on top of the dialog's buttons, but the EditText
remains visible.
- Screenshot 1 (Stock Android EditTextPreference)
- Screenshot 2 (MyEditTextPreference)
Two suggestions: 1) the easier way which may or may not give you enough room as it looks like your keyboard is taller than the standard android one: Hide the application title bar and the android status bar in the preference activity. This will move the dialogbox up a little.
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
2) the harder way would be to write your own custom preference control by extending DialogPreference or EditTextPreference and define your own layout for that control that has a smaller or no title, or perhaps smaller ok/cancel buttons or something. then put that custom control in your preferences.xml
After a few different tries following code worked well (dialog with edit box is not so nice but it is functional). EditTextPreference should be customized (resource file with preferences should be changed also). CustomTextPreference should have trivial 3 constructors.
public class CustomEditTextPreference extends EditTextPreference { ...
@Override
protected void showDialog(Bundle state)
{
super.showDialog(state);
// solving problem with insufficient space for showing EditText in EditTextPreference's dialog. This problem exists only
// on HTC WildFire with android OS 2.2
if ( Build.MODEL.equals("HTC Wildfire") && 8 == Integer.valueOf(android.os.Build.VERSION.SDK) )
{
_makeMoreRoomForEditText();
}
}
private void _makeMoreRoomForEditText()
{
EditText editText = getEditText();
View parent1 = (View) editText.getParent();
View parent2 = (View) parent1.getParent();
View parent3 = (View) parent2.getParent();
View parent4 = (View) parent3.getParent();
View parent5 = (View) parent4.getParent();
editText.setPadding(editText.getPaddingLeft(), 0, editText.getPaddingRight(), 0);
parent1.setPadding(parent1.getPaddingLeft(), 0, parent1.getPaddingRight(), 0);
parent3.setPadding(parent1.getPaddingLeft(), 0, parent1.getPaddingRight(), 0);
parent5.setPadding(parent5.getPaddingLeft(), 0, parent5.getPaddingRight(), 0);
ViewGroup par5 = (ViewGroup) parent5;
View v = par5.getChildAt(0);
v.setPadding(v.getPaddingLeft(), 3, v.getPaddingRight(), 3);// empirically determined values - looks fine only on HTC WildFire device with 2.2 Android
}
}
来源:https://stackoverflow.com/questions/6087131/sharedpreferences-edittext-dialog-squashed-on-htc-wildfire