How to create a single activity without headers

370 Views Asked by At

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.

2

There are 2 best solutions below

0
codeFreak On BEST ANSWER

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>
1
Lead Developer On

In the styles.xml file, please make one style tag if you want to remove actionbar(you said header) in only specific activity.

<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar" />

And goto AndroidManifest file, please apply above style to your activity something like this.

<activity android:name=".SettingActivity" android:theme="@style/AppTheme1"></activity>

if you want to remove actionbar in all of your activities, then you can change AppTheme style in the styles.xml file like below.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/mainColor</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    ... ...