Frage

I'm quite new to android develompent and I'm trying to figure out how to take users to my PreferenceActivity. I already created a Preferences class with it's corresponding layout however I cannot figure out how to take users to this Activity on phones without a physical menu button.

I'm using a Nexus 4 and it doesn't even have a physical menu button. I've seen the menu button (three vertical dots) pop up now and then on the virtual buttons (bottom right), how can I make that button appear on the phone and take users to settings onkeydown?

War es hilfreich?

Lösung

I've seen the menu button (three vertical dots) pop up now and then on the virtual buttons (bottom right)

What you are seeing is the "legacy menu button":


(source: blogspot.com)

It's a bad practice to still have that in your apps. It should get removed when you set your targetSdkVersion to API 11 or higher in your Android Manifest. It should then get shifted to the ActionBar. That should look something like this:


(source: blogspot.com)

Look at the overflow menu at the upper right corner.

If you want to know more about that, then read this.


If you want to start your PreferenceActivity by clicking on an item by the list that gets populated by either of these menus you have to create a menu.

Override onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

my_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/settings"
        android:title="Settings"
        android:showAsAction="never"/>
</menu>

Now override onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch(id) {
        case android.R.id.settings:
        // Start your Activity in here.
        break;
    }
    return super.onOptionsItemSelected(item);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top