how to show the SettingsActivity when pressing on 'Settings' button ? - android

I define a SettingsActivity on my app and i want to show this activity when pressing on the 3 dot on the right side of the screen.
How to do it ?
I can't find any manual/demo that help me to do it.

First of all, create a menu resource file and add the following code.
<group android:checkableBehavior="single">
<item
android:id="#+id/settings"
android:title="Settings"/>
</group>
then, in your MainActivity (Activity where you want to show the three dots in toolbar) create a method onCreateOptionsMenu and onOptionItemSelected,
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater.inflate(R.id.menu_settings, menu);
MenuItem menuItem = menu.findItem(R.id.settings);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getId();
if (id == R.id.settings){
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
return super.onOptionsItemSelected(item);
}

Related

How to get onPrepareOptionsMenu to "toggle" my preferences class

My code:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
}
This works to open the settings menu (Preferences.class) but I would like it to close the menu if it's already open when this system button is pressed. What can I add or change to make that happen?
Here is how you should be implementing what you want:
First thing first, you want to supply a "Settings" item that can selected by the user to get to your Preferences class. You do that by adding an item to your menu/main.xml and inflating it in onCreateOptionsMenu().
menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Settings"/>
...
</menu>
Inside your Main Activity, override this to inflate your main.xml file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
...
return super.onCreateOptionsMenu(menu);
}
And then override this to handle what happens when a Menu item is selected, in this case the Settings item:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_settings:
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
...
default:
return super.onOptionsItemSelected(item);
}
}
I was able to get the menu to close by adding this to my customized preferences menu class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
onBackPressed();
return false;
}
which calls this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
which takes the user back to the screen to play another round. I don't know if this is an acceptable solution for every case but it worked for me.

How to create an Overflow menu and combine it with an option menu?

I am new in Android development. I want to ask how can I create an overflow menu with two items. Also, how can I combine it with an option menu I already did, which looks like this:
public class MainActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater blabla = getMenuInflater();
blabla.inflate(R.menu.options_menu ,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.OurSponsors:
Intent i = new Intent("com.--.------.------");
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
return false;
}
xml
<item
android:title="Our Sponsors"
android:id="#+id/OurSponsors"
android:icon="#drawable/image"
/>
<item
android:title="Exit"
android:id="#+id/exit"
android:icon="#drawable/exit"
/>
I am a beginner.
Here is the developer page related to that http://developer.android.com/guide/topics/resources/menu-resource.html.
You will want to add android:showAsAction="collapseActionView" (or android:showAsAction="never" depending on your API level) to each menu item in the XML layout

Android - Menu Item cannot be resolved to a type in onOptionsItemSelected()

I created a simple activity (with a menu) and tried adding menu items but they don't show up when trying to actually give them some function in the onOptionsItemSelected() method. I'm not sure why it's not working, as I did the exact same thing in the menu for the main activity and it worked just fine. When typing in android.R.id.add_screen_submit_button for example, it is not recognized as existing. And if I forcefully just type it in and leave it the message "add_screen_submit_button cannot be resolved or is not a field" comes up. The menu is also in the correct folder (I actually just left it as is when creating the Activity). Thanks in advance.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/add_screen_submit_button"
android:orderInCategory="4"
android:showAsAction="always"
android:title="#string/add_screen_ok"
android:icon="#drawable/accept_icon" />
<item
android:id="#+id/add_screen_cancel_button"
android:orderInCategory="5"
android:showAsAction="always"
android:title="#string/add_screen_cancel"
android:icon="#drawable/cancel_icon" />
</menu>
Here's the code
public class AddActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.
//setupActionBar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case android.R.id.add_screen_submit_button:
Toast.makeText(this, "Map Selected", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Change
android.R.id.home:
to
R.id.home:
and the same with the other one.
android.R is for sdk resources and R.id.some_id is for ids you create

android - submenu click isn't selecting the right item

I have a menu with a submenu. The parent item is "Options" and its children are "Call", "Navigate", and "Edit". Here is my activity code that handles the menu click. The problem is that the first submenu always gets selected by the event. With this code if i click on "Call"(the first child item) the onOptionsItemSelected() method registers the R.id.record_edit item being selected. I don't know why this is happening since the "Call" menu item has the id of "record_call". I've tried switching the submenu items around in the xml but the first submenu item is the only one that registers and it always registers as R.id.record_edit. Not sure what's wrong with my code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_edit:
Intent intent = new Intent(this, EditRecordActivity.class);
myApp.setRecord(record);
Bundle b = new Bundle();
b.putBoolean("new", false);
b.putString("pageTitle", pageTitle);
b.putString("meta_universalid", meta_universalid);
intent.putExtras(b);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
xml menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/record_options"
android:showAsAction="ifRoom|withText"
android:title="..."
android:titleCondensed="Options"
android:enabled="true">
<menu>
<item
android:id="#+id/record_call"
android:showAsAction="ifRoom|withText"
android:title="#string/record_call"/>
<item
android:id="#+id/record_navigate"
android:showAsAction="ifRoom|withText"
android:title="#string/record_navigate"/>
<item
android:id="#+id/record_edit"
android:showAsAction="ifRoom|withText"
android:title="#string/record_edit"/>
</menu>
</item>
</menu>
just checked with your menu to be sure. It is working fine with me. Check whether your overall format is like the following code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_call:
Toast.makeText(this, "call", 1000).show();
return true;
case R.id.record_edit:
Toast.makeText(this, "edit", 1000).show();
return true;
case R.id.record_navigate:
Toast.makeText(this, "navigate", 1000).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

menu won't show up android

Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:
public class AppMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.appmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.hello:
sayHello();
return true;
case R.id.bye:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/hello"
android:title="Hello"
android:icon="#drawable/icon"/>
<item android:id="#+id/bye"
android:title="Bye" />
</menu>
Thanks !
This answer is in response to the comment discussion on the question.
You can't have to menu appear outside of your Activity. That means you have to start your Activity and then from inside your Activity you'll be able to get the menu appear on a menu button press.

Categories

Resources