Yoklan Yoklan - 1 month ago 17
Android Question

Adding a settings page to an app

I'm having issues adding a settings page to my app.

I've created a SettingsActivity, SettingsFragment, a actibity_settings.xml layout and a preferences.xml file.

I've placed a button in my activity_main view and added an intent to the MainActivity to call the SettingsActivity.

However placing the button doesn't seem to do anything.

SettingsActivity

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

private SharedPreferences pref;

public static void start(Context context) {
Intent intent = new Intent(context, SettingsActivity.class);
context.startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onDetachedFromWindow() {
pref.unregisterOnSharedPreferenceChangeListener(this);
super.onDetachedFromWindow();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// TODO handle any special logic on change of a preference if you want
}
}


SettingsFragment

public class SettingsFragment extends PreferenceFragment {

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


MainActivity, omitted rest of the methods.

public class MainActivity extends Activity implements OnClickListener {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
...
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(BAR_COLOUR)));
context = getApplicationContext();

View createButton = findViewById(R.id.locate_button);
createButton.setOnClickListener(this);
latitudeTextView = (TextView) findViewById(R.id.latitude_textview);
latitudeTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.define_location, 0, 0, 0);
latitudeValueTextView = (TextView) findViewById(R.id.latitude_value_textview);
longitudeTextView = (TextView) findViewById(R.id.longitude_textview);
longitudeTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.define_location, 0, 0, 0);
longitudeValueTextView = (TextView) findViewById(R.id.longitude_value_textview);
accuracyTextView = (TextView) findViewById(R.id.accuracy_textview);
accuracyTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.info, 0, 0, 0);
accuracyValueTextView = (TextView) findViewById(R.id.accuracy_value_textview);
lastUpdateTextView = (TextView) findViewById(R.id.last_update_view);
lastUpdateValueTextView = (TextView) findViewById(R.id.last_update_date);

webView = (WebView) findViewById(R.id.map_view);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.setVisibility(View.INVISIBLE);

isPermissionGiven();

}


Preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_catOne"
android:key="pref_key_catOne" >
<CheckBoxPreference
android:key="mockCheckBox"
android:defaultValue="true"
android:title="@string/pref_mockCheckBoxOne"
android:summary="@string/pref_mockCheckBoxSummary" />
<CheckBoxPreference
android:key="mockCheckBox"
android:defaultValue="true"
android:title="@string/pref_mockCheckBoxTwo"
android:summary="@string/pref_mockCheckBoxSummary" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_catTwo"
android:key="pref_key_catTwo" >
</PreferenceCategory>
</PreferenceScreen>


activity_settings.xml

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
class="org.softshack.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


main_activity.xml (only the button)

<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/accuracy_textview"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/settingsButton" />

Answer

I'm not a fan of your on click pattern but if you want to keep it just use:

View settingsButton = findViewById(R.id. settingsButton);
settingsButton.setOnClickListener(this);