I am working on getting my preferences screen setup, but I can't seem to get my preference activity working right.
I created a new blank activity called SettingsActivity
that extends PreferenceActivity
and removed setContentView(R.layout.activity_settings);
from the onCreate
method, and replaced it with addPreferencesFromResource(R.xml.preferences);
.
In my main activity class, I setup a new intent to call my SettingsActivity
class when I clicked the settings button that android creates by default.
I added a test item to my preferences.xml
and ran it with this result. What happened to my Nav bar? Do I have to do something to add it? Android Studio shows that addPreferencesFromResource
is deprecated. What am I supposed to do?
MainActivity method that calls SettingsActivity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
SettingsActivity
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
Here is my preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBoxPreference
android:key="test check box"
android:title="Test"
android:defaultValue="false"/>
</PreferenceScreen>