Open the settings preferences on clicking the vertical dotted line on the upper-left corner on android app

427 views Asked by At

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?

1

There are 1 answers

2
Arjun Komath On BEST ANSWER

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);
    }