How to add intent on menu event - android

I want to call activity on menu event.I am new to android so i am trying to open new screen on delete option on menu, for this i write switch case but i didnt get how to call another activity in that please help me my code is
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.delete: Intent i= new Intent(this,deleteAct.class);
startActivity(i);break;
case R.id.edit://calling another intent; break;
case R.id.showall://calling another intent; break;
}
return true;
}
please help me ..

Neha... Did you declare the activity in the AndroidManifest.xml file as in:
<activity android:name= "deleteAct"
android:label="#string/delete_act_title">
</activity>
Also, consider adding:
default:
return super.onOptionsItemSelected(item);
JAL
Some code here.

Related

Back Button in the Navigation Bar not Working?

I have created an android project, with a couple of activities.
I have also added the back button feature into the activities, however when I click it
nothing happens.
Have I done something wrong?
I've put this code into each of the activities, and none of them work
Any opinion/input is greatly appreciated.
Code for trying to go back from my Gallery activity to the main:
//GalleryActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
// return true;
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
and.. in res/menu/
<item
android:id="#+id/back_icon"
android:icon="#drawable/ic_action_back"
android:title="#string/back_title"
android:showAsAction="always"
/>
I downloaded the proper android design icons, and have added them to the drawable folders too.
EDIT:
Main Activity
button4= (Button) findViewById(R.id.button4);//find the button
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), GalleryActivity.class);
startActivity(i);
finish();//close main activity after start info activity
}
});// links to gallery page
you have to override onOptionsItemSelected and check for the id. For instance:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back_icon:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You have to specify the action of the options, included the home/back action as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.back_icon:
onBackPressed(); //Or whatever you want to do when back_icon is pressed!
return true;
default:
return super.onOptionsItemSelected(item);
}
}

option menu not clicked at first time in sherlockfragment

I have used option menu in my fragment.The problem is that when i go first time to fragment ,the option menu click event is not called.But when i go to another fragment.and again revist that fragment then the option menu click event is called...
following is the code
//Creating the option menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.newcarmenu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
break;
}
return super.onOptionsItemSelected(item);
}
please tell me why this happen?
Right now, you are returning super.onOptionsItemSelected(item) every time, so the selection is being passed on. You need to return true when your MenuItem has been selected. Try this instead:
switch(item.getItemId())
{
case R.id.menuNewCar:
_menuClickCallback.onMenuSelected();
return true;
default:
return super.onOptionsItemSelected(item);
}

How can I make a menu option expand in android?

I wrote this code in mainactivty.java
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("About");
return true;
}
It creates a menu, but I want it to open a new window when I click on the menu.
I'm not sure if this is what you want (cause it seems too obvious) but it sounds like you're asking for the following
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.someItem: //this is your menu item in the menu.xml
startActivity(new Intent(this, MyNewActivity.class)); //here you start your new activity
break;
}
}

Android: start activity from options menu

I am trying to start an activity from an options menu, but my app keeps crashing. The only error that I receive is an ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord,Intent) error in the debug window in Eclipse.
Below is the the code that I am using at the moment, but keep in mind I have tried multiple options, all of which end in the same misery, at the same piece of code - the startActivity statement (discovered by using breakpoints, since I'm not sure how to see the stack trace in the LogCat window, as described in my previous question Android/Eclipse: assistance with LogCat).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.changescheme, menu);
menu.findItem(R.id.changeScheme).setIntent(new Intent(this, ColourActivity.class));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
startActivity(item.getIntent());
return true;
}
And here is the changescheme.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/changeScheme" android:title="Change Colour Scheme" android:icon="#android:drawable/ic_menu_edit"></item>
</menu>
I have also tried using a switch(item.getItemId()) statement in the onOptionsItemSelected block as opposed to the menu.findItem in the onCreateOptionsMenu block, but still no luck.
I have defined the activity in my Manifest file. I can also start the activity from a regular button, and the first time the app opens on a device, the activity is started immediately after my splash screen, and I have had no problems with either of these methods.
To me this indicates that there is nothing wrong with the ColourActivity class or its associated layout file, but there is a problem with the implementation from the options menu.
I have also implemented this same method as shown above (in code) in a different app and had no problems, so I'm am really at a loss here.
Intent you are activating should point to some target component, which is not in your case, instead you should do following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
Intent intent = new Intent(ActivityA.this, ColourActivity.class);
/*Here ActivityA is current Activity and ColourActivity is the target Activity.*/
startActivity(intent);
return true;
}
Try with this,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.changeScheme:
Log.d("ChangeScheme", "Selected : ChangeScheme Option");
startActivity(new Intent(MainAcitivity.this, ColourActivity.class));
return true;
caseR.id.help:
Log.d("HelpMenu", "Selected : Help Option");
//Here put your code
return true;
}
}
Check this:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.changeScheme:
//start activity here
break;
case R.id.help:
//start activity here
break;
}
return true;
}
Hi adam your code seem to be perfectly fine while i am testing on my emulator, please check whether you have added the class name "ColourActivity" to your manifest file.
<activity android:name="ColourActivity"></activity>
I have solved the problem now.
It turns out the problem was not at all on the ListActivity class, it was in fact on the ColourActivity class.
I was attempting to parse a few colours in onCreate, but I had forgotten to include the # in one of the RGB colour strings, hence the crash!
Thanks heaps for everyone's help, Adam.

How to call Activity from a menu item in Android?

I'm attempting to call startActivity(myIntent) from the click of a menu button but my application crashes at that point.
The same startActivity call works fine from a regular button click, so, I assume the menu button is missing information about the context? Or maybe I'm totally off the mark here.
So... what's the correct way to have a menu item take me to a specific Activity?
I've revised my code based on the initial set of advice. Still crashing in the same place. The debugger doesn't enter the exception clause, the app just dies.
[EDITED WITH CODE SNIPPET]
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
try{
switch (item.getItemId()) {
case R.id.menuItemLang:
startActivity(new Intent("com.my.project.SETTINGS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}catch(Exception e){
log(e);
}
}
First option
You have to override onOptionsItemSelected method in your Activity, which is called when user clicks on the item in Options menu. In the method you can check what item has been clicked.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
There is also onContextItemSelected method which works similary, but for Context menu (I'm not sure, what menu you mean).
More information at http://developer.android.com/guide/topics/ui/menus.html
EDIT:
Second option
I think the first option is easier, but from your code I see, that you want to start activity as an action (because of String parameter in Intent constructor). To do this, you need to specify an action in your AndroidManifest.xml. So, if I would start activity ActivityForItemOne (from previous example) the <application> element in AndroidManifest.xml would look like this:
<application ...>
...
<activity android:label="Activity For First Item" android:name=".ActivityForItemOne">
<intent-filter>
<action android:name="my.app.ITEMONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
And the Intent will be:
Intent intent = new Intent("my.app.ITEMONE");
The my.app. is package of your application. It's not necessary to use your application package, but it's recommended for uniqueness of actions.
More information at:
Class Intent - Action and Category constants
Action element
Intents and Intent Filters
Hope this solve your problem.
More optimice:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
return true;
case R.id.item2:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
if There have 2 Class
1 MainActivity
2 Welcome
then you need to go from
welcom>MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.logout:
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
break;
case R.id.settings:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

Categories

Resources