Android PreferencesActivity showing previous fragment - android

I created a Preferences screen by extending PreferencesActivity as per the example here.
The first screen appears okay but when I click to see the second screen I see them one on top of the other.
So, taking advice from a number of other thread, I changed the background of the second fragment to black.
This worked in as much as I no longer see them both. But instead I see the FIRST one only except for the header.
The first screen looks like this:
and the second one looks like this:
Only the header line changed whereas the rest remained the same.
This is the code in my activity:
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesMain()).commit();
}
/**
* This fragment contains a second-level set of preference that you
* can get to by tapping an item in the first preferences fragment.
*/
public static class PreferencesMain extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onStart() {
super.onStart();
View view = getView();
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
}
/**
* This fragment contains a second-level set of preference that you
* can get to by tapping an item in the first preferences fragment.
*/
public static class PreferencesNotifications extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences_notifications);
}
#Override
public void onStart() {
super.onStart();
View view = getView();
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
}
#Override
protected boolean isValidFragment (String fragmentName) {
return true;
}
}
preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/settings_notifications">
<!-- This PreferenceScreen tag sends the user to a new fragment of
preferences. If running in a large screen, they can be embedded
inside of the overall preferences UI. -->
<PreferenceScreen
android:fragment="activities.SettingsActivity$PreferencesNotifications"
android:title="#string/settings_notifications_managePushTitle"
android:summary="#string/settings_notifications_managePushSummary">
android:background="#android:color/black"
<!-- Arbitrary key/value pairs can be included for fragment arguments -->
<extra android:name="someKey" android:value="somePrefValue" />
</PreferenceScreen>
</PreferenceCategory>
preferences_notifications.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/settings_notifications_managePushHeader">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="#string/settings_notifications_managePushEntry1Title"
android:summary="#string/settings_notifications_managePushEntry1Summary" />
</PreferenceCategory>

Add a header.xml to manage settings, and use onBuildHeaders method.
Resource headers.xml:
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:title="Digle"
android:fragment="activities.SettingsActivity$PreferencesMain"/>
</preference-headers>
Class SettingsActivity:
public class SettingsActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove all code in here
}
#Override
public void onBuildHeaders(List<Header> target) {
super.onBuildHeaders(target);
loadHeadersFromResource(R.xml.headers, target);
}
...
}
EDIT:
An alternative could be use subscreens. You have to place the group of Preference objects inside a PreferenceScreen.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="Settings"
android:title="#string/app_name">
<PreferenceCategory
android:title="#string/settings_notifications">
<PreferenceScreen
android:background="#android:color/black"
android:summary="#string/settings_notifications_managePushSummary"
android:title="#string/settings_notifications_managePushTitle">
<PreferenceCategory
android:title="#string/settings_notifications_managePushHeader">
<CheckBoxPreference
android:key="checkbox_preference"
android:summary="#string/settings_notifications_managePushEntry1Summary"
android:title="#string/settings_notifications_managePushEntry1Title"/>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>

Related

Android: How to show title bar in Preferences Fragment

I have searched the forums, but can't seem to figure out how to show a title bar in the Android Preferences Fragment. I see questions about how to hide it, but not about how to show it.
I have a preferences activity
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,new SettingsFragment()).commit();
}
}
And I have a preferences fragment:
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
And I have a preferences xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/title">
<CheckBoxPreference
android:key="regular"
However there is no title bar at the top of the preferences screen. Any ideas how to make one show?
You mean show title bar in the fragment?
you can try to inherit AppCompatActivity.

ListPreference without the radio [duplicate]

This question already has an answer here:
ListPreferences without any radio buttons?
(1 answer)
Closed 9 years ago.
I am making the preference screen for my app. Inside the preference screen, I have a ListPreference to “Spread the word” about the app. However, I don't want the radio buttons there. I want the whole dialog seem as if it is a list of things the user can do, and the user would select one option from the menu which will be executed. How do I do it? I am new to Android, coming from iOS background.
Thanks in advance!
I have this in my pref_settings.xml.
<PreferenceCategory
android:key="pref_key_tell_friends"
android:title="#string/pref_header_tell_friends" >
<ListPreference
android:entries="#array/spread_the_word"
android:entryValues="#array/spread_the_word"
android:key="pref_key_spread"
android:title="#string/pref_title_spread" />
</PreferenceCategory>
and this the fragment I’m loading in my activity —
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_settings);
}
}
A preference works just like any button when it is specified as a Preference in the XML, instead of a ListPreference, or any other type. So, I just added a listener to the preference, and opened an AlertDialog in the listener. Here’s the code —
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_settings);
Preference myPref = (Preference) findPreference("pref_key_spread");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
createListPreferenceDialog();
return false;
}
});
}
private void createListPreferenceDialog() {
Dialog dialog;
final String[] str = getResources().getStringArray(R.array.spread_the_word);
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setTitle("Spread the Word");
b.setItems(str, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int position){
Log.I(“Clicked the AlertDialog", + str[position]);
}
});
dialog = b.create();
dialog.show();
}
}
And here’s the changed XML —
<PreferenceCategory
android:key="pref_key_tell_friends"
android:title="#string/pref_header_tell_friends" >
<Preference
android:key="pref_key_spread"
android:title="#string/pref_title_spread" />
</PreferenceCategory>
Thanks #njzk2 for the pointer.

How do I programmatically add EditTextPreferences to my PreferenceFragment?

I am new to Android, so I need a little guidance on how to programmatically add EditTextPreference objects to my PreferenceFragment.
I am pulling in a list of values from a web service. I have saved them successfully to my SharedPreferences, and I use them to generate URLs (path portion).
I would like the users of my app to be able to edit these values, but after lots of searching on Google, it isn't clear to me how to programmatically add EditTextPreference objects to my PreferenceFragment.
Please note, my PreferenceFragment is working fine for the SharedPreferences values I hard code as names into the preference xml file (PreferenceScreen). I also know how to get my SharedPreferences, so don't worry about having to explain that portion to me.
I use addPreferencesFromResource in onCreate of my PreferenceFragment. Should I add them in the onCreateView? I was thinking I could get the PreferenceCategory and add them there? But again, I am not sure how to do that. I would really be grateful for the help!
// Code
PrefsFragment.java:
package com.example.lorddoineedhelp;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PrefsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
// I am guessing I need to do something here?
return v;
}
}
XML File for PreferenceFragment:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Hard coded values -->
<PreferenceCategory
android:title="General">
<CheckBoxPreference
android:key="debug"
android:title="Debug"
android:summary="Enable Debug" />
</PreferenceCategory>
<PreferenceCategory android:title="Address">
<EditTextPreference
android:key="ipAddress"
android:title="IP Address"
android:summary="IP Address used for Image pings"
/>
<EditTextPreference
android:key="port"
android:title="Port"
android:summary="Port used for Image pings" />
</PreferenceCategory>
<!-- Where I want to add the values from my web service -->
<PreferenceCategory
android:title="Paths"
android:key="urlPaths">
</PreferenceCategory>
</PreferenceScreen>
You can add preferences, e.g. EditTextPreference, CheckBox, etc, programmatically in the "onCreate" method of the PreferenceFragment.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load "dummy" (empty) preferences from an XML resource
addPreferencesFromResource(R.xml.preferences_channelconfig);
PreferenceScreen screen = this.getPreferenceScreen(); // "null". See onViewCreated.
// Create the Preferences Manually - so that the key can be set programatically.
PreferenceCategory category = new PreferenceCategory(screen.getContext());
category.setTitle("Channel Configuration");
screen.addPreference(category);
CheckBoxPreference checkBoxPref = new CheckBoxPreference(screen.getContext());
checkBoxPref.setKey(channelConfig.getName() + "_ENABLED");
checkBoxPref.setTitle(channelConfig.getShortname() + "Enabled");
checkBoxPref.setSummary(channelConfig.getDescription());
checkBoxPref.setChecked(channelConfig.isEnabled());
category.addPreference(checkBoxPref);
}
The crucial step is the addPreferencesFromResource(...), with a dummy xml to attach an empty PreferenceScreen to the fragment. Without this, there is no "top-level Preference that is the root of a Preference hierarchy", thus this.getPreferenceScreen() returns Null.
The XML I used was just:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:orderingFromXml="true">
</PreferenceScreen>
Hope that helps someone.
Here is the code to create a PreferenceFragment programmatically:
public class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity());
setPreferenceScreen(screen);
EditTextPreference preference = new EditTextPreference(screen.getContext());
preference.setKey("EditTextPreference");
preference.setTitle("Edit Text Preference");
preference.setSummary("Click the preference to edit text.");
screen.addPreference(preference);
}
}
I don't know if it works in the onCreateView method but it does in the onViewCreated.
Here is my code (Inside a subclass of PreferenceFragment):
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.getPreferenceScreen().addPreference(new EditTextPreference(getActivity()));
}

Android preferences page isn't displaying once clicked, how do I get this to work?

I am trying to create a preferences page that once an item is selected, it goes to the preference page for the app. When I click on the item, it doesn't work. Please can someone help me in finding out how to make this work? Here is the code:
Preference Java file:
public class UserSettingActivity extends PreferenceActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
Main Activity Java file specific code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Goes back to the home screen
case R.id.item1: Intent i = new Intent(this, UserSettingActivity.class);
startActivity(i);
Preferences XML file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="EditText"
android:key="name"
android:summary="Enter your name" />
<CheckBoxPreference
android:title="Vibrate"
android:defaultValue="true"
android:key="vibrateCheckbox"
android:summary="Turn vibrate on or off"/>
</PreferenceScreen>
I think that because you are passing the wrong context to the intent:
instead of this:
case R.id.item1: Intent i = new Intent(this, UserSettingActivity.class);
write this:
case R.id.item1: Intent i = new Intent(yourActivityName.this, UserSettingActivity.class);

Button in preference file

I have set the two buttons in preference.xml file. I want to go to main activity when I click the "ok" button. So for that code is where to write?
You can set an onPreferenceClickListener in your PreferenceActivity.
in preference xml :
<Preference android:title="Preference Button" android:summary="This works almost like a button" android:key="mypref" />
in preferences activity :
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Get the custom preference
Preference mypref = (Preference) findPreference("mypref");
mypref.setOnPreferenceClickListener(new OnPreferenceClickListener() { });
}
}

Categories

Resources