I have successfully set up 2 items on the action bar to open up 2 seperate Async Tasks to perform different tasks in the background.
My first item launches an AsyncTask class that will only download the images and save them to the SD card.
My Second item launches an AsyncTask class that will download the image, then set it as the users wallpaper.
The issue that I'm having is that when the second item is pressed(Setwallpaper), it will do the 2 tasks at the same time that have been separated by the 2 classes(Save and set the users wallpaper).
Then, when I remove the other launch code(setwallpaper/vice versa) it will just do the one task, not 2 at the same time.
I have no idea why this behaving like this, would someone help me out. Thanks...
Code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.test, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.SaveWallpaper:
new SaveWallpaperAsync(getActivity()).execute(mImageUrl);
return true;
case R.id.SetWallpaper:
new SetWallpaperAsync(getActivity()).execute(mImageUrl);
return true;
}
return super.onOptionsItemSelected(item);
}
Try something like this:
class SaveWallpaperAsync extends AsyncTask {
doInBackground(...) {
image = Utils.downloadImage(...);
Utils.saveToSdCard(image);
}
}
class SetWallpaperAsync extends AsyncTask {
doInBackground(...) {
image = Utils.downloadImage(...);
Utils.setBackground(image);
}
}
There is no break between cases :) while return should do the same think, you should try using break after return
Related
I have an Actionbar displayed on my maps fragment to which I have added a zoom-in button. When selected I would like to replace it with a zoom-out button.
When zoom-in is selected the it is obviously pases in to onOptionsItemSelected as the menu item, so it is easy to set it's attributes like this: viewZoomIn.setVisibility(viewZoomIn.GONE); My problem is, how do I get a reference to the zoom-out button on the action bar to set it as as I would like to viewZoomOut.setVisibility(viewZoomOut.VISIBLE);?
I thought I may be able to store the zoom-in and zoom-out views as instance variables and capture them when I inflate the Actionbar, like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
viewZoomIn = findViewById(R.id.zoom_in);
viewZoomOut = findViewById(R.id.zoom_out);
return super.onCreateOptionsMenu(menu);
This does not work.
Any help on getting hold of thes buttons, or advice on a better way of toggling my zoom-in/zoom-out buttons would be appreciated.
It probably shows, but I am pretty new to Java, so it would be preferable is any assistance were communicated in a simple manner.
Thank you.
You have to apply your visibility logic in onPrepareOptionsMenu(Menu) not in onCreateOptionsMenu(Menu) if you want to toggle MenuItem state.
private boolean currentlyZoomedIn;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// called the first time and after each "invalidateOptionsMenu()"
// if tha Activity is in the "zoomedIn" state, the zoomOut button will be visible
menu.findItem(R.id.zoom_in).setVisible(!currentlyZoomedIn);
menu.findItem(R.id.zoom_out).setVisible(currentlyZoomedIn);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.zoom_in:
// do your logic when zoom_in is clicked
currentlyZoomedIn = true;
break;
case R.id.zoom_out:
// do your logic when zoom_out is clicked
currentlyZoomedIn = false;
break;
}
// force the redraw of the menu
invalidateOptionsMenu();
return super.onOptionsItemSelected(item);
}
By the way, I suggest you to use only one button and move your logic only in onOptionsItemSelected(MenuItem) to avoid an instance variable and the call to invalidateOptionsMenu() in a way like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// zoom_out_in_id is the id for the common button
if (item.getItemId() == R.id.zoom_out_in_id) {
// get the curren title
final CharSequence title = item.getTitle();
// if the current title is the one of zoom_in button, you have to change its infos to zoom_out ones
if (title.equals("zoom_in_title")) {
// do your logic when zoom_in is clicked
item.setIcon(R.drawable.zoom_out_icon);
item.setTitle("zoom_out_title");
} else if (title.equals("zoom_out_title")) {
// do your logic when zoom_out is clicked
item.setIcon(R.drawable.zoom_in_icon);
item.setTitle("zoom_in_title");
}
}
return super.onOptionsItemSelected(item);
}
While working with my application, I had to come to come across a situation where I supposed to use menu which has to get displayed in entire application when user click menu button. So I used following code in Default activity but then realized that menu is displaying in that activity but not in all.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.del_boy_menu, menu);
//below comented code for changung dynamically
// MenuItem bedMenuItem = menu.findItem(R.id.home);
// bedMenuItem.setTitle("title changed");
// System.out.println("onCreate executed");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
System.out.println("onOptionSelected executed");
switch (item.getItemId()) {
case R.id.home:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainDeliveryBoyActivity.this, "Home is Selected", Toast.LENGTH_SHORT).show();
// MenuHomeActivity
startActivity(new Intent(context,MenuHomeActivity.class));
return true;
case R.id.delivered1:
Toast.makeText(MainDeliveryBoyActivity.this, "delivered is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.cancelled:
Toast.makeText(MainDeliveryBoyActivity.this, "cancelled is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.active:
Toast.makeText(MainDeliveryBoyActivity.this, "active is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So my question is should I copy and paste all the above code in all the activities? or is there a way where I can skip this?
Create one global activity called BaseActivity and make all of your activities extend it.
public class BaseActivity extends AppCompatActivity{
public void onCreate(Bundle iCreate){
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.del_boy_menu, menu);
....
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
....
}
}
And now all other activities should extend the BaseActivity, so you won't need to write code to inflate menu everytime.
public class Activity1 extends BaseActivity{
....
}
I believe each activity to have a unique menu. But there is a way you can do what you are trying to implement here.
You can create a base class that inherits from an Activity class and put all your menu logic on that base class.
And you can also refer to this answer, Reuse the Action Bar in all the activities of app and this article.
PS: I do not take credit for the answer, I just want to help. Cheers!
I'm not trying to change the main Icon , just a menu item's icon.
The Icon is essentially displays whether I am recording at that moment. I change the icon when it's tapped using
item.setIcon(R.drawable.recordstart);
In this method.
public boolean onOptionsItemSelected(MenuItem item) {
...
} else if (item.getItemId() == R.id.ab_menu_VRecord) {
if(recording)
{
item.setIcon(R.drawable.recordstop);
}else{
item.setIcon(R.drawable.recordstart);
}
}
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
Anyone know how I can do this outside this method.
Example:
class {
public MenuItem example;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar, menu);
example = menu.findItem(R.id.ab_menu_exampleview);
return true;
}
}
Then throughout your class you can use
example.setIcon("Your Image");
Not 100% sure where you are wanting to change the icon, but you can certainly cache or create a class member variable and point it at the the MenuItem in your Activity or Fragment for example. After they click it or you inflate it, assign it to the member variable and when you need to change it, you've got a reference or "cached" pointer to it to change the icon.
I think that is a UI change so you might have to be sure that you only call that on the UI thread.
if(!item.isChecked()){
item.setChecked(true);
item.setIcon(R.drawable.icon1);
}else{
item.setChecked(false);
item.setIcon(R.drawable.icon2);
}
is this?
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.
In my main activity I have a menu and when a menu option is selected an Intent is created and a new activity is started. When that activity completes the process should return back to the main activity and all its previous states according to the ActivityLifeCycle.
I notice that when it returns back to the main activity, nothing is accessable and the screen dims. I can only get back to what I expect when I press the menu softkey.
Has anyone experienced this issue before? Feedback would be appreciated!
Code sample below:
#Override
protected void onResume(){
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
return true;
case R.id.menu_decks:
Intent launchDecks = new Intent(this, stackDecks.class);
startActivity(launchDecks);
return true;
case R.id.menu_exit:
this.onDestroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The code in the first post is actually correct(I have something very similar). There is a great chance the error is in the menu.xml res file, even though it inflates without any problems. Check it is fully consistent in both places, and has the same items. I finally solved it after hours of experimenting.
You seemed to be confused with the use of super.
super.m() is used to call a superclass method.
If you inherit the method with no override then
super.m() = this.m()
See your super.onDestroy
It s absoluetly useless to override a method m() just to call super.m()
See your onResume
Sometimes it is usefull to call a super clas method, it allows you to benefit from this code in a subclass. For instance here onCreateMenuOptions is overriden and your subclass can benefit from some imitialisation code for a menu.
Regards,
Stéphane