i have an issue regarding a item from a menu to act like a Button, and i don’t know how to solve it.
In a item from menu i refeer to a item expandable like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1" android:title="Item1" />
<item android:id="@+id/item2" android:title="Item2" />
</menu>
and i dont know how to define it, and use it like a button, like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String uri=null;
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_RINGTONE);
if (uri !=null) {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
Uri.parse(uri));
} else {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
(Uri)null);
}
startActivityForResult(intent, 1);
}
});
Thanks in advance,
So you want to implement a menu. For API 10 or lower, put menu.xml in res/menu/ and override these two functions in your activity:
For API 11+, consider using an action bar as explained here.
Update
For Preferences menus you will want to use findPreference() and define a custom setOnPreferenceClickListener(). Rather than copying code snippets here, follow any of these three detailed tutorials: