I have a pretty weird issue. In my SettingsActivity, I have a DropDownPreference (the one that displays a list when clicked), and it works fine on Portrait mode.
However, when I go into Landscape mode, whenever the preference is clicked, it quickly disappears after being displayed, as if I had pressed the screen twice.
Here is my style, although I doubt there's something to do with it:
<style name="SettingsFragment">
<item name="android:navigationBarColor">?navBackground</item>
<item name="android:statusBarColor">?colorPrim</item>
<item name="android:colorBackground">?colorPrim</item>
<item name="android:colorForeground">?textForeground</item>
<item name="android:textColor">?textForeground</item>
<item name="alertDialogTheme">#style/DialogTheme</item>
<item name="colorAccent">?accentVariant</item>
<item name="preferenceCategoryTitleTextColor">?accentVariant</item>
</style>
Notes: My phone is a Samsung Galaxy Note 10, running Android 12 (API 31).
EDIT: Posting the activity's source code:
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ActivityUtils.applyTheme(this);
getTheme().applyStyle(R.style.SettingsFragment, true);
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settingsContainer, new SettingsFragment())
.commit();
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.settings, rootKey);
}
}
}
PS: Parent theme does not change any attributes aside colors.
Related
I'm using SettingsActivity as preference activity for my application like so:
public class SettingsActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getString(R.string.dark_theme_key), false)) {
setTheme(R.style.AppThemeDark);
} else {
setTheme(R.style.AppThemeLight);
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_preferences);
...
}
}
}
If I use getActivity().recreate(); (I have a SwitchPreference for dark and light theme. Dark theme is Theme.AppCompat and light is same but with .Light. If I change the theme I use recreate.) or change orientation of the screen (Resulting in onCreate of course), I get a weird problem when I'm on my light theme.
The problem is as follows -after a recreate, the text content of the EditTextPreference's turn white instead of black.
If I simply ignore savedInstanceState == null and just create a new fragment every time on SettingsActivity.onCreate, I lose the "screen state" (for example if I was scrolled all the way down before, after recreate Im scrolled back to the top. If I had a EditTextPreference open before, after recreate its closed).
I'm not sure how to restore a fragment screen state in case of recreation, or why my problem even occurs. Would appreciate help.
I'm using the new AppCompatActivity introduced in the AppCompat library version 22.1.
When I extend this Activity, the hardware back button no longer pops the back stack of my Fragments, it closes the Activity instead.
Here is how I'm changing fragments in my activity:
public void changeFragment(Fragment f) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_holder, f);
ft.addToBackStack(null);
ft.commit();
}
If I change MainActivity extends AppCompatActivity to MainActivity extends Activity the problem goes away and I am able to go backwards through my fragments.
Changing calls to getFragmentManager() to getSupportFragmentManager() results in devices running Android < 5.0 losing the Material theme, which was the main reason for implementing AppCompatActivity in the first place.
The style referenced in my manifest <application android:theme="#style/AppTheme">
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary_material_light</item>
<item name="colorPrimaryDark">#color/primary_dark_material_light</item>
<item name="colorAccent">#color/accent_material_light</item>
</style>
I was able to resolve this by overriding onBackPressed() in my Activity:
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
If anybody has any insight into why this extra step is necessary when using AppCompatActivity I would be interested to know.
use
getSupportFragmentManager()
instead of
getFragmentManager()
Are you extending your app theme from Theme.AppCompat.*?
I'm using editTextPreference to implement a Setting Activity. My problem is that when I click in an EditTextPreference it displays a dialog with the preference title but not appear an edittext to write.
I have searched for this problem but It seems it hasn't happened to anyone. Any help would be appreciated
I have this code :
#EActivity
#OptionsMenu(R.menu.settings_activity_menu)
public class ActivitySettings extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new FragSettings_())
.commit();
}
}
#EFragment
public class FragSettings extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener
{
#App
MyApplication app;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
#AfterViews
void init()
{
EditTextPreference url =(EditTextPreference)findPreference(app.getResource(R.string.CONFIG_URL_SERVER));
EditTextPreference user =(EditTextPreference)findPreference(app.getResource(R.string.CONFIG_USER_);
if(url!=null)
url.setSummary(app.getSettings().getSharedPreferences().getString(app.getResource(R.string.CONFIG_URL_SERVER), ""));
if(user!=null)
user.setSummary(app.getSettings().getSharedPreferences().getString(app.getResource(R.string.CONFIG_USER_), ""));
}
#Override
public void onResume() {
super.onResume();
// Register as listener to validate settings values
app.getSettings().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
// Remove listener
app.getSettings().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
EditTextPreference connectionPref = (EditTextPreference) findPreference(key);
// Set summary to be the user-description for the selected value
connectionPref.setSummary(app.getSettings().getSharedPreferences().getString(key, ""));
}
}
My preferences.xml, in these editTextPreference is where I have the problem, the input doesn't appear :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/settings_server">
<EditTextPreference
android:key="#string/CONFIG_USER_"
android:title="#string/settings_user"/>
<EditTextPreference
android:inputType="textPassword"
android:key="#string/CONFIG_PASS_"
android:title="#string/settings_password"/>
<EditTextPreference
android:key="#string/CONFIG_URL_SERVER"
android:title="#string/settings_url"/>
</PreferenceCategory>
<Preference
android:key="#string/AREA"
android:title="#string/settings_area"/>
</PreferenceScreen>
What I really not understand is Your Parameters, but let me suspect You are giving the correct references with these to the Preferences. But usually, You have to initialize them with the correct Object:
Preference url =findPreference(Parameters.CONFIG_URL_SERVER);
Preference user =findPreference(Parameters.CONFIG_USER_);
must be
EditTextPreference url =(EditTextPreference)findPreference(Parameters.CONFIG_URL_SERVER);
EditTextPreference user =(EditTextPreference)findPreference(Parameters.CONFIG_USER_);
And it´s better to set the keys inside Your xml layout as a reference to a string in strings.xml, for example:
<EditTextPreference
android:key="#string/user_pref_key"
android:title="#string/settings_user"/>
EDIT
Be sure to use the correct context. For this, You have to wait until the Activity is attached by Override onAttachActivity. Make a global Context:
private Context mContext;
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
mContext=activity;
}
Then You can refer it in Your PreferenceFragment.
EditTextPreference url =(EditTextPreference)findPreference(mContext.getResources().getString(R.string.user_pref_key));
At the end I found what was happening. I wanted to use the same text style that I was using in all my application, so in ManifestFIle.xml I had this:
<activity
android:name=".mobile.settings.ActivitySettings_"
android:theme="#style/PreferenceActivity"
android:label="#string/title_activity_settings"/>
Which refers to this style:
<style name="PreferenceActivity" parent="AppTheme">
<item name="android:editTextPreferenceStyle">#style/CodeFont</item>
<item name="android:preferenceCategoryStyle">#style/CodeFont</item>
</style>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">#dimen/text_size_medium</item>
</style>
It seems that if I use editTextPreferenceStyle it makes disappear the box input. This is my solution:
<activity
android:name=".mobile.settings.ActivitySettings_"
android:theme="#style/AppTheme"
android:label="#string/title_activity_settings"/>
And to custom the edit text I use this http://secutyhf.org/wordpress/zeegers/2014/12/16/android-edittextpreference-style/
<style name="AppTheme" parent="android:Theme.Holo">
<item name="android:editTextStyle">#style/EdittEXTsTYLE</item>
</style>
<style name="EdittEXTsTYLE" parent="android:Widget.EditText">
</style>
I have a FragmentActivity and a few fragments in my application.
In some conditions , i'll pop a dialog from activity, it will be fullscreen but it should stay below main action bar. is it possible ? ( I want to keep main action bar visible )
My Dialog Style
<style name="FullScreen" parent="#android:style/Theme.Holo.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Dialog
public class FDialog extends Dialog implements OnClickListener{
Context mContext;
public FDialog(Context context) {
super(context,R.style.FullScreen);
mContext = context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_content);
}
Thanks
After searching in the website, I found this answer that could help with your problem. It suggests to implement the Dialog with a Fragment. That way you have access to action bar easy. On the other hand, you have the disadvantage to work with the fragments flow, but it also very easy.
Hope it helps!
Show Dialog nearly full screen in my case (With ActionBar & overlay)
I am trying to add theme support to app. It is working, but I want user to choose between themes. The problem is, I cannot do that I tried so many things. I am using ListPreference to define a list of arrays for user to choose. I cannot link those listpreference entry values with util.
If I edit "0" in Util with any number, themes work but it doesnt work when I change those entry values (from list in phone)IDK why.
Below is the code
Settings.java
public class Settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Util.setAppTheme(this);
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager
.beginTransaction();
PrefsFragment mPrefsFragment = new PrefsFragment();
mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
mFragmentTransaction.commit();
}
public static class PrefsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
Util.java
public class Util extends Activity {
public static void setAppTheme(Activity a) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(a);
int mTheme = Integer.parseInt(sp.getString("theme", "0"));
if(mTheme==0)
{
a.setTheme(R.style.Dark);
}
if(mTheme==1)
{
a.setTheme(R.style.Light);
}
if(mTheme==2)
{
a.setTheme(R.style.LimeLight);
}
if(mTheme==3)
{
a.setTheme(R.style.MojoLight);
}
if(mTheme==4)
{
a.setTheme(R.style.SanMarinoLight);
}
if(mTheme==5)
{
a.setTheme(R.style.LimeDark);
}
if(mTheme==6)
{
a.setTheme(R.style.MojoDark);
}
if(mTheme==7)
{
a.setTheme(R.style.SanMarinoDark);
}
}
}
preferences.xml
<PreferenceCategory
android:title="#string/preference_category2_title">
<ListPreference android:key="list2_preference"
android:title="#string/list2_title"
android:summary="#string/list2_summary"
android:entries="#array/list2_preferences"
android:entryValues="#array/list2_preferences_values"
android:dialogTitle="#string/list2_dialog_title"/>
<SwitchPreference android:key="switch1_preference"
android:title="#string/switch1_title"
android:switchTextOff="#string/switch1_textoff"
android:switchTextOn="#string/switch1_texton"
/>
</PreferenceCategory>
styles.xml
<!-- Dark -->
<style name="Dark" parent="android:Theme.Holo">
<item name="android:windowBackground">#drawable/activity_background_dark</item>
</style>
<!-- Light -->
<style name="Light" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar_Light</item>
<item name="android:windowBackground">#drawable/activity_background_light</item>
</style>
arrays.xml
<resources>
<string-array name="list2_preferences">
<item>Dark</item>
<item>Light</item>
<item>Light Lime</item>
<item>Light Mojo</item>
<item>Light San Marino</item>
<item>Dark Lime</item>
<item>Dark Mojo</item>
<item>Dark San Marino</item>
</string-array>
<string-array name="list2_preferences_values">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</string-array>
I will be thankful if anyone can help me out?
What is your on code for the onItemSelected method of your list?
Also you can't change the theme of an activity after you've initialized it's layout.
In my current application I also allow the user to change the theme. To achieve this I simply start the activity again passing an argument (the selected theme) and finish the current one.
In the activity onCreate() I then check the argument. If it's -1 (activity first time start) I get the default theme.
private static final String INTENT_EXTRA = "theme";
private boolean onCreate;
#Override
protected void onCreate(Bundle savedInstanceState) {
onCreate = true;
themeId = getIntent().getIntExtra(INTENT_EXTRA, -1);
if (themeId == -1) {
themeId = SharedPreferencesManagment
.getApplicationThemeResourceId(SharedPreferencesManagment
.getIntApplicationTheme(this));
} else {
themeId = SharedPreferencesManagment
.getApplicationThemeResourceId(themeId);
}
setTheme(themeId);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options_theme);
initSpinner();
}
In the list's onItemSelected() method I call something like:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!onCreate) {
Intent intent = new Intent(this, OptionsThemeActivity.class);
intent.putExtra(INTENT_EXTRA, getSelectedTheme());
startActivity(intent);
finish();
}
onCreate = false;
}
Also note the onCreate-boolean as the onItemSelected()-method is also called when the activity inits the spinner.
(Was the simpliest workaround to prevent a infinte loop)
ALSO:
Why is Util a Activity? If the code you've posted is everything you do with Util you don't need to extend Activity as you pass the context.