5

I add SettingActivity on my app, and I get this auto generated java files AppCompatPreferenceActivity.java and SettingsActivity.java .

What's the difference of the two? I came across with the link below but it doesn't discuss it.

Preference

I have no idea where to code.

Community
  • 1
  • 1
RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56
  • `What's the difference of the two?` The former is compatible with older OS versions, too. – Phantômaxx Feb 18 '16 at 12:23
  • @HrundiV.Bakshi - You mean the SettingsActivity is for older OS and AppCompatPreference is for API <= 21 ? Should I put my codes in both files, having the same code. – RoCkDevstack Feb 18 '16 at 12:31
  • I mean the SettingsActivity is for **newer** OS. I should see the code for both. I'm still using Eclipse and targetting API Level 18 (I dislike Android Studio and API Level 19+) – Phantômaxx Feb 18 '16 at 12:34

2 Answers2

3

Look no need to create general_pref.xml if you want one page settings, I have created something for you to better understand (should work for you):

res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="pref_title_notifications"
        android:key="pref_category_notifications_key">
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="notifications_on_off_key"
            android:title="pref_title_breaking_news_notification"/>

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="notifications_sound_on_off_key"
            android:title="pref_title_enable_notifications_sound"/>
    </PreferenceCategory>

    <PreferenceCategory
        android:title="pref_title_data"
        android:key="pref_category_data_key">
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="pref_images_load_on_off_key"
            android:title="pref_title_load_images_on_mobile_data"/>
    </PreferenceCategory>

</PreferenceScreen>

ActivitySettings.java

public class ActivitySettings extends Activity {
    @Override
    protected void onCreate(Bundle savedStateInstanceState) {
        super.onCreate(savedStateInstanceState);

        // Display fragment as main content
        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.preferences);  //load preference screen from xml directory.
        }
    }
}

EDIT

*

This is how Default SettingsActivity looks like in Tablet

* enter image description here

and in Phone

enter image description here

After All changes your Tablet settings will look like Phone settings, nothing else.Now Setting will look like Long list,instead of Headings, You'll have categories. As you see, in last picture, category of System Settings

enter image description here

Farid
  • 2,317
  • 1
  • 20
  • 34
  • Thank you for this, but my problem is how to get rid of the pref_header that is to be loaded as default. I created a SettingsActivity the one with template provided - which auto generated the AppCompatPrefences.. – RoCkDevstack Mar 24 '16 at 16:18
  • Replace your default SttingsActivity.java with with one in the post and preferences.xml with one in the post and just delete all other default pref_... xml including pref_header.xml. That should work. (If I got what you mean) – Farid Mar 24 '16 at 16:36
  • One last question, :) Thank you for the help and info - If I delete the pref_header.xml - it says that it is for tablet. – RoCkDevstack Mar 24 '16 at 16:40
  • Look at the answer-above after the **EDIT**. – Farid Mar 24 '16 at 17:08
  • Thank you, I decided not to choose the template provided by AS. I manually create that one. :) – RoCkDevstack Mar 24 '16 at 17:12
2

Seems I am late to game, but anyway. HOpe this sill help.

1. Where to put your code (ANSWER)

SettingsActivity.java

Do not mess with AppCompatPreferenceActivity.java. Every thing that you want to change should be coded inside SettingsActivity.java class.

2.What is AppCompatPreferenceActivity.java (ANSWER)

Well when using settings activity the class to be created is like:

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

In case we want to support upper versions of Android we use

public class SettingsActivity extends AppCompatPreferenceActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }

I think you have seen this libraries android-support-v4,android-support-v7 and etc, They all are meant to support upper SDKs (e.g version over 4, 7 and so on). And this class is kind of them.

Farid
  • 2,317
  • 1
  • 20
  • 34