I have created an PreferenceActivity with a PreferenceFragment in it. But the content with all the settings is shown outside of it layout bounderies, what am I doing wrong?
SettingsActivity:
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(final 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);
}
}
}
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="My list of Preferences">
<CheckBoxPreference android:title="Checkbox Preference"
android:defaultValue="false" android:summary="This preference can be true or false"
android:key="checkboxPref" />
<EditTextPreference android:name="EditText Preference"
android:summary="This allows you to enter a string"
android:defaultValue="Nothing" android:title="Edit This Text"
android:key="editTextPref" />
<RingtonePreference android:name="Ringtone Preference"
android:summary="Select a ringtone" android:title="Ringtones"
android:key="ringtonePref" />
<PreferenceScreen android:key="SecondPrefScreen"
android:title="Secondary Level" android:summary="This is a sub PreferenceScreen">
<intent android:action="android.intent.action.VIEW"
android:targetPackage="com.as400samplecode" android:targetClass="com.as400samplecode.Preferences2" />
</PreferenceScreen>
<Preference android:title="Custom Preference"
android:summary="This works almost like a button" android:key="customPref" />
</PreferenceCategory>
</PreferenceScreen>
I solved it by using a normal Activity instead of PreferenceActivity.
You are not code added for Preferences using SharedPreferences Class
I have adding one use your checkbox preference example check below code
public class PrefsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
//xml Preference.xml
<PreferenceCategory
android:title="PreferenceCategory A">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="title_checkbox_preference"
android:summary="summary_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="PreferenceCategory B">
<EditTextPreference
android:key="edittext_preference"
android:title="title_edittext_preference"
android:summary="summary_edittext_preference"
android:dialogTitle="dialog_title_edittext_preference" />
</PreferenceCategory>
Related
I just added PreferenceActivity to my project in Android studio (auto generated) and I noticed, that my preferences navigation has subscreens. Instead of simple list with header categories (all inside single activity window), I'm getting my headers as simple list, then I need to click on item to get content displayed.
I don't want it, I need only simple preference screen without any navigation, with headers displayed as category label.
How to do that?
Just modify your PreferenceActivity code like this:
public class SettingsActivity extends AppCompatPreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
}
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
preference.setSummary(stringValue);
}
return true;
}
};
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
XML File:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<PreferenceCategory
android:key="primary_category"
android:title="Category 1">
<EditTextPreference
android:key="coffee_file"
android:title="EditText 1"
android:defaultValue="Value of EditText 1"
android:selectAllOnFocus="true"
android:inputType="text"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
</PreferenceCategory>
<PreferenceCategory
android:key="secondary_category"
android:title="Category 2">
<EditTextPreference
android:key="gin_file"
android:title="EditText 2"
android:defaultValue="Value of EditText 2"
android:selectAllOnFocus="true"
android:inputType="text"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
</PreferenceCategory>
</PreferenceScreen>
I am working of settings activity for my application. I seted up two headers for preferences and then i made fragment for each header. Problem is when i click on header, nothing shows up. Thank you for your help.
Here is my preference activity
public class Preferences extends PreferenceActivity {
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.prefheads, target);
}
}
Here is first fragment
public class Prefs1 extends PreferenceFragment {
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_prefs1);
}
}
}
Here is second fragment:
public class Prefs2 extends PreferenceFragment {
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_prefs2);
}
}
}
XML files:
Headers
<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<header
android:fragment="com.example.lubys.prefs.prefs1"
android:title="First category"
android:summary="This doesnt need summary" />
<header
android:fragment="com.example.lubys.prefs.prefs2"
android:title="Second cat"
android:summary="This is awful summary">
</header>
</preference-headers>
Fragment1:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.lubys.prefs.prefs1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PreferenceCategory
android:title="the title"
android:key="key2">
<EditTextPreference android:title="Your Name"
android:key="username"
android:summary="Please provide your username"></EditTextPreference>
<CheckBoxPreference android:title="Application Updates"
android:defaultValue="false"
android:summary="This option if selected will allow the application to check for latest versions."
android:key="applicationUpdates" />
<ListPreference
android:title="Download Details"
android:summary="Select the kind of data that you would like to download"
android:key="downloadType"
android:defaultValue="1"
android:entries="#array/c"
android:entryValues="#array/c" />
</PreferenceCategory>
</PreferenceScreen>
Fragment2:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.lubys.prefs.prefs1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PreferenceCategory
android:title="sms"
android:key="pref_key_storage_settings">
<CheckBoxPreference
android:key="saas"
android:summary="asde"
android:title="Something"
android:defaultValue="false"/>
</PreferenceCategory>
</PreferenceScreen>
check if u didn't get:
java.lang.RuntimeException:
Subclasses of PreferenceActivity must override isValidFragment(String) to verify that the Fragment class is valid!
XXX has not checked if fragment YYY is valid.
isValidFragment Android API 19
when android's isValidFragment() from PreferenceActivity gets called?
http://securityintelligence.com/new-vulnerability-android-framework-fragment-injection#.VRGSv1V_NBc
I use following java code to run this,
public class MainActivity extends ActionBarActivity {
int transitionTime = 300;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TransitionDrawable transition = (TransitionDrawable) findViewById(R.id.relativeLayoutMain).getBackground();
transition.startTransition(transitionTime);
}
Please help me to sole this problem.
Hi I'm new to android and what I'm try to do is change layout background color continuously.
Above method trigger error to me, I not found solution to this but I use different approach to do this.
I use timer with TransitionDrawable and here is my working copy of code.
Here I add "drawable" resources,
transition_drawable.xml (this is main resource xml set this resource file to your layer backbround)
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="#drawable/color_selector_01" />
<item android:drawable="#drawable/color_selector_02" />
</transition>
color_selector_01.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#919bfa"
android:endColor="#4e5bda"
android:type="linear" />
</shape>
</item>
</selector>
color_selector_02.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#fe5528"
android:endColor="#3c1ed4"
android:type="linear" />
</shape>
</item>
</selector>
In my main layout I set layer background to "transition_drawable"
here is my "activity_main.xml"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#drawable/transition_drawable" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/linkTerms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:singleLine="true"
android:text="#string/linkTerms" />
<TextView
android:id="#+id/and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#string/and" />
<TextView
android:id="#+id/linkPrivacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="#string/linkPrivacy" />
</LinearLayout>
</ScrollView>
Finlay here is my MainActivity.java file
#TargetApi(Build.VERSION_CODES.HONEYCOMB) #SuppressLint("NewApi") public class MainActivity extends ActionBarActivity {
int transitionTime = 6000;
private RelativeLayout Rlayout;
private TransitionDrawable trans;
final Handler handler_interact=new Handler();//not defined as final variable. may cause problem
View layout_interact;
#SuppressLint("NewApi") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout_interact =(View) findViewById(R.id.relativeLayoutMain);
Rlayout = (RelativeLayout)findViewById(R.id.relativeLayoutMain);
Resources res = this.getResources();
trans = (TransitionDrawable)res.getDrawable(R.drawable.transition_drawable);
Rlayout.setBackgroundDrawable(trans);
BackgroundColoerChangerCallTimer();
}
public void BackgroundColoerChangerCallTimer(){
//creating timer
Timer timer_interact=new Timer();
timer_interact.schedule(new TimerTask() {
#Override
public void run() {
UpdateGUI();
}
}, 3000);
}
private void UpdateGUI() {
handler_interact.post(runnable_interact);
}
//creating runnable
final Runnable runnable_interact = new Runnable() {
public void run() {
//ayout_interact.setBackgroundColor(Color.GREEN);
trans.reverseTransition(transitionTime);
BackgroundColoerChangerCallTimer();
}
};
I want to get a pick file dialog in my PreferenceActivity how do I get to that? Can I override onClick for PreferenceActivity somehow? Android API 14.
Here's my PreferensActivity:
import android.preference.PreferenceActivity;
import java.util.List;
public class SettingsActivity extends PreferenceActivity {
#Override
public boolean onIsMultiPane() {
return true;
}
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_head, target);
}
}
Header xml is:
<?xml version="1.0" encoding="utf-8"?>
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">
<header
android:fragment="com.bfx.rfid.FragmentSetApp"
android:icon="#android:drawable/ic_menu_call"
android:title="Application"
android:summary="Application settings">
</header>
<header
android:fragment="com.bfx.rfid.FragmentSetConnection"
android:icon="#android:drawable/ic_menu_call"
android:title="Connectivity"
android:summary="Connection settings">
</header>
</preference-headers>
PreferenceFragment class:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class FragmentSetApp extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_set_app);
}
}
PreferenceFragment xml is:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="work_directory"
android:title="Folder to work with">
</Preference>
<PreferenceCategory
android:title="Work with a database file">
<CheckBoxPreference
android:key="DB_default"
android:summary="Choose a default database file or pick one"
android:title="Choose a database file"
android:defaultValue="true"/>
<EditTextPreference
android:key="DB_URI"
android:title="Database file"
android:dependency="DB_default">
</EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>
I don't think Andorid have a native file chooser, so you'll have to implement one yourself, or find a library.
You could then use the android:onClick property in PreferenceFragment.xml:
<EditTextPreference
android:key="DB_URI"
android:title="Database file"
android:onClick="startFileChooser"
android:dependency="DB_default">
and put this in you PreferenceFragment:
public void startFileChooser(MenuItem i){
// Start the file chooser here
}
Of course, how you would to that depends on what file chooser you decide to go with. You would probably end up with the path to the selected file, which you would insert into SharedPreference.
Using Android Annotations. My prefs:
#SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT)
public interface MyPreferences {
#DefaultBoolean(true)
boolean myOption();
}
The preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="myOption"
android:title="My Option Name"/>
</PreferenceScreen>
My PreferencesActivity:
public class MyPreferencesActivity extends PreferenceActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
The problem is: in spite of the default value of myOption is true (and indeed - it is when calling MyPreferences_.myOption().get()) the checkbox in the preferences activity is unchecked by default.
The same happens for String preferences. They return default string given in #DefaultString annotation but it is not displayed in the PreferenceActivity. Only after I change the preference value from the activity, it is properly displayed.
I resolved default values duplication by:
values/preferences_defaults.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="default_myOption">true</bool>
<string name="default_myString">my string</string>
</resources>
Prefs:
#SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT)
public interface MyPreferences {
#DefaultRes(R.bool.default_myOption)
boolean myOption();
#DefaultRes(R.string.default_myString)
String myString();
}
And the preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="myOption"
android:defaultValue="#bool/default_myOption"
android:title="My Option Name"/>
<EditTextPreference
android:key="myString"
android:defaultValue="#string/default_myString"
android:title="My String Option"/>
</PreferenceScreen>
Now I can define default values for both annotations and PreferenceActivity in one XML file.