Button in preference file - android

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() { });
}
}

Related

Android PreferencesActivity showing previous fragment

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>

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.

SharedPreferences sub PreferenceScreen with custom fragment class

I have a PreferenceScreen with a sub PreferenceScreen:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="SOUND">
<PreferenceScreen android:title="Sound Options"
android:fragment="com.test.SoundPreferenceFragment">
</PreferenceScreen>
</PreferenceCategory>
...
The sub preference is a custom implementation of a PreferenceFragment
public class SoundPreferencesFragment extends PreferenceFragment implements
OnSharedPreferenceChangeListener {
...
I am wondering how I am supposed to start the custom fragment. In my main activity I do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, settingsPreference).commit();
}
Which works great to start my main settings preference screen.
I then override:
#Override
public boolean onPreferenceStartFragment(PreferenceFragment fragment, Preference preference) {
getFragmentManager().beginTransaction().replace(android.R.id.content, new SoundPreferenceFragment()).commit();
return true;
}
That seems to work but I have a few questions.
Is that the right way?
When I go into the Sounds preference and hit the back button it takes me all the way out of settings. Is that how it should work?
I think I can also tell the sub preference to start a new intent, i.e. create a new SoundPreferencesActivity. Is that a more 'correct' way.
EDIT:
I also tried not to extend 'onPreferenceStartFragment' in my main activity hoping that android would take care of starting the new preference correctly' It does seem to start the new PreferenceFragment but it overlays the UI right on top of the main preferences UI.
You have to create a new activity and attach the SoundPreferencesFragment .
when you select the item in main setting activity , start the new activity

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.

Can't accesses setting activity (SharedPreferences)

When I run my application there is no menu button to go to the settings activity.
I have a settings activity which uses shared preferences. I call in the Main Activity like this:
private SharedPreferences settings;
private OnSharedPreferenceChangeListener listener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settings = PreferenceManager.getDefaultSharedPreferences(this);
listener = new OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
}
};
settings.registerOnSharedPreferenceChangeListener(listener);
And this is my settings activity:
public class SettingsActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
And my settings.xml is in XML folder and its very simple at the moment. I just want to be able to open settings in my application. The XML is like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="General settings" >
<EditTextPreference
android:key="pref_username"
android:title="User name" />
<CheckBoxPreference
android:key="pref_viewimages"
android:summary="Determines whether lists are shown with thumbnail photo"
android:title="View images"/>
</PreferenceCategory>
I've also added the settings activity to the Manifest file:
<activity
android:name=".SettingsActivity"></activity>
so far it eclipse doesn't show me any errors. But when I run my application there is no menu to go to the settings page! I was under the impression that when I create a settings activity, Android will automatically create a settings menu. But there is no menu button or anything. How can I fix this?
The menu and sharedpreferences are not directly linked. Shared preferences store values inside storage. Menu would be the graphical layout bringing you to PreferenceActivity that allow you to used shared preferences to store the settings.
You have to first inflate the menu like this in your activity :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
where menu contains the menu details like these (res/menu/main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<<item android:id="#+id/item1" android:title="Your setting name"></item>
</menu>
This would add "Your setting name" as an option when you click the menu button. Now to make the menu button do something use this (put it in your mainactivity) :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.item1:
//Do something here like in my case launch intent to my new settings menu
Intent options1 = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(options1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now the menu button called "Your setting name" will launch your settings activity.
I don't think it's created by default. I had to do it myself. Anyway, check the menu file for your activity and check if this menu is used in your activity.

Categories

Resources