androidsetting

How to create a single activity without headers


I'm developing an application where I set up an activity for the settings. I tried to use the android studio template to create an activity setting but the structure of the code that is being created is too complex. Can anyone tell me how to create a settingsActivity similar to the android studio template but without headers? Thanks in advance for the answer. Greetings.


Solution

  • To create a single settingActivity you can try the below pseudo code:

    public class SettingsUI extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            getFragmentManager().beginTransaction().replace(android.R.id.content,
                    new SettingsFragment()).commit();
            assert getSupportActionBar() != null;
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            PreferenceManager.setDefaultValues(this, R.xml.settings_preference, false);
        }
    
        public static class SettingsFragment extends PreferenceFragment {
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.settings_preference);
             }
         }
      }
    

    Then settings_preference.xml code:

    PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference 
    android:key="sample_key" 
    android:title="@string/sample_title" 
    android:inputType="number"  
    android:summary="@string/sample_summary"/>
    
    <EditTextPreference 
    android:key="sample2_key" 
    android:title="@string/sample2_title" 
    android:inputType="number"
    android:summary="@string/sample2_summary"/>
    
    </PreferenceScreen>