I am fairly new to android.Recently I have been following this tutorial about making a navigation drawer for my app. Most of it seem pretty straightforward(http://blog.teamtreehouse.com/add-navigation-drawer-android). However,I am confused about what role onOptionItemSelected plays in the activity.
if (mDrawerToggle.onOptionsItemSelected(item)) {return true;}
if I remove the code above , the navigation drawer won't show when I click the toggle icon, and I am curious what is happening behind the scene when I include it and click on the drawer toggle icon.
Also what is the difference between returning false and true...I tried it out but nothing changed.
onOptionsItemSelected() is a method found to return menu item to perform action on it, if somehow its triggered.
check out the documentation for more info.
now why the drewer is not opening when you remove this method?
because it does open it.
how!
as i mentioned earlier this method returned a menu item that you have selected in this case the drawer icon which is located on the action bar.
what is the difference between returning false and true?
here it does perform the action regardless the returned data, it actually doesn't use the returned value so that won't effect the action that have been taken.
hope this will help.
Related
I've searched around for a while but I can't seem to find an answer to this. I have a theme for my Android application that closes an activity when the outside is clicked by using the XML script <item name="android:windowCloseOnTouchOutside">true</item>
However, I need a function to fire when this is done as it returns to the previous activity and passes information. onBackPressed() doesn't seem to pick this up and I can't find any information on what would.
RESULT
As far as I can uncover it's not possible to add information to the activity returned to OnActivityResult if you are using windowCloseOnTouchOutside as it directly uses onPause() with no break between.
My solution was to remake the popup window with a transparent surround that would be detected when the player clicks onto it. Then I created a function that is detected when the back button is pressed or the player clicks outside of the window
I'm currently learning Android development from a book and I've come to the topic of radio buttons. The book explains that you handle radio button clicks (within a RadioGroup) using setOnCheckedChangeListener() with an anonymous OnCheckedChangeListener class as an argument.
However, according to the Android documentation you can set the onClick attribute on radio buttons to simply refer to a method of your design and handle clicks there.
Is there a reason to choose one over the other? What I'm asking is if there is some difference between the two that I'm missing, or if they simply both do the same thing.
setOnCheckedChangeListener() is listening to checked state, so if it is changed programmatically your code will be triggered. onClick listener only detects changes on checked state only if the element is clicked.
In my Android Project, whenever I go from First Activity to Second, I get an left arrow button followed by the some text in the action bar. That left arrow only comes when I set my First Activity as the hierarchical parent of the Second Activity.
I want to remove that back button and replace it with an icon. I have read other posts for the same question, and so I have already tried the following code in my onCreate() method.
getActionBar().setDisplayHomeAsUpEnabled(false);
But after reaching to the second activity, the app gets crashed.
Please help me out.
Use getSupportActionBar() bar:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);
I know that there are many many posts for my question , but they don't work really, as I've tried many solutions.
I would like that my title Bar (1) stays on the top normally AND my navigation bar (2) is hidden or blocked.
I'm developing an application where the back button, home button, and app button are not allowed to be clicked. (the three buttons are locked or hidden)
I already use :
#Override
public void onBackPressed() {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
But the other buttons stay available. Please see the attached picture to get a better understanding.
Click here to see my screenshot
My overriding question is this: In Android PreferenceActivity, how can I write an OnClickListener which will have the same functionality as pressing the Android back button as I navigate through PreferenceScreen defined menus? That is to say, I would like users of my App to explicity see a menu choice "Back" which will bring them to the previous menu, or bring them out of the menu activity to their previous activity if they are at the root of this particular PreferenceActivity session.
The android developer documents tell us
Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.
And they are correct about that. I navigate happily through my menus by clicking on PreferenceScreen items to get to that screen, and using the Android back button to go Back up a level. But I'm not sure a casual user really understands the "Back" button, I know I didn't until I read about it in Developer docs. SO I would would like them to have an explicit Preference defined menu choice whos OnClickListener duplicates the function of the Android back button.
So I tried to put in a Preference in my menu that would go back. Having determined that a not Overriden onBackPressed in a my subclass of PreferenceActivity just referred back to Activity.onBackPressed() which merely calls finish(), I tried this OnClickListener:
private OnPreferenceClickListener clickFinishesSuccessfully = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
finish();
return true;
}
};
As it turns out, this did NOT do the same thing as pressing the back button! Pressing this button always took me out of the PreferenceActivity entirely, back to the Activity from which I had called my PreferenceActivity. Specifically, it did NOT navigate back through my menus no matter how deep I was when I clicked it.
I am guessing here: When I have gotten to a submenu by clicking an onscreen preference which is really a PreferenceScreen, I am no longer in my own PreferenceActivity. I must be in some other Activity?
So my functional question: what can I put in my OnClickListener of my "Back" Preference to get the same function as the Android Back button navigating through my menus?
I think the casual user should know about the back button. The button is used everywhere so it might be a problem getting used in the first day but after that it's natural. Without being used to the "back" button I can hardly imagine doing the everyday tasks.
The preference you want to add just duplicates functionality and doesn't provide a consistent way with the rest of the system. If Google was considering back being an uncommon thing for casual users would have added that option in phone's Settings which is also a PreferenceActivity.