0

I want to make a simple settings page, I went to Google's documentation below:

http://developer.android.com/guide/topics/ui/settings.html

created new android testing project, then I created the PreferenceScreen xml, and PreferenceFragment class inside my mainActivity class.

I want the settings screen to appear when I press the settings icon, but instead it only shows one unclickable item named settings.

I know my question is basic, but I tried a lot to make it work with no results.

any help please?

Muayad Salah
  • 701
  • 1
  • 8
  • 19

1 Answers1

1

If you want to open the settings from the Menu inside the action bar, as shown below :

enter image description here

First, you need to define a Menu item inside the menu.xml file for that activity :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
        <item android:id="@+id/action_settings" android:title="@string/action_settings"
            android:orderInCategory="100" android:showAsAction="never" />
</menu>

Second, you need to infate the menu, and then the events for the menu item click can be handles using the methods shown below :

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @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) {

            // write code to handle the click for the settings menu item!

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
Community
  • 1
  • 1
Arjun Komath
  • 2,802
  • 4
  • 16
  • 24
  • I have one more question thought,, I want to listen to the change of the preferences, I have tried the function onSharedPreferenceChanged, I don't know where exactly should this listener be invoked. When I change the preferences from the settings activity, I want to be able to read the new values from another activities, not only reading, but also I want something to happen (the value of some public variable of another activity should be modified right after the change), so, how to? – Muayad Salah Nov 20 '14 at 08:38
  • This might solve the problem, have a look at it : [link](http://stackoverflow.com/questions/3799038/onsharedpreferencechanged-not-fired-if-change-occurs-in-separate-activity) – Arjun Komath Nov 20 '14 at 12:31