Get Actionbar click event xamarin.android - android

Im have added a back icon to the action bar,now when i click on that i need to go back to the activity that started it.Is there a actionbar click event which i can hook to and call the this.finish method

In order to work the home button properly and capture it's action event. You must have to first enabled the home button by calling setHomeButtonEnabled() for targeting android API level 14 onwards.
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
Now you've to override the onOptionsItemSelected method inside your activity to capture the home button action of actionbar and also "Back" key on some of the android devices.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish(); // this will dismiss the current activity
}
return true;
}

Related

Back Button in ActionBar of App

Generally switching from one activity to another activity hamburger icon is replaced with back arrow. I want to control the functionality of that arrow. I have seen many contents here but most of them were related to hardware's back button. How can I control that?
I am trying the functionality in case of fragments. Also I have Navigation drawer attached with the hamburger icon.
I tried this-
if(id == android.R.id.home){
getSupportFragmentManager().beginTransaction().replace(R.id.main_container, new AmbulanceMap()).commit();
getSupportActionBar().setTitle("Book A Ride");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
but doesnt work as I hoped.
I want my back button to change the fragmentto previous fragment.
I had the same problem once. Just like you, things like checking if Android.R.id.home is clicked didn't work.
But I solved it using that:
Set navigation listener to toolbar:
toolbar.setToolbarNavigationClickListener(v -> onBackPressed());
If it should be within fragment:
Create an public method in activity.
In fragment's onAttach (or later) cast getActivity() to your activity and call method you was defined previously.
Example:
// YourActivity
public void setHomeListener(OnLickListener listener){
toolbar.setToolbarNavigationClickListener(listener);
}
//Fragment's onCreate
((YourActivity)getActivity()).setHomeListener(v -> onBackPressed());
//Fragment's onDestroy
((YourActivity)getActivity()).setHomeListener(null);
And, of course, set home us up enabled to show back arrow.
EDIT
if you don't use labmdas u should use:
(YourActivity)getActivity()).setHomeListener(new OnClickListener() {
#Override
public void onClick(View v) {
YourFragment.this.onBackPressed();
}
});
The back button of the ActionBar is a menuItem so you need to override onOptionsItemSelected like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
//Your back button logic here
break;
}
return true;
}
Also donĀ“t forget to add getSupportActionBar().setDisplayHomeAsUpEnabled(true); after setting the Toolbar

How to detect if the Up button was pressed

In my activity the action bar shows only the left arrow and the title of the activity.
When I press the left arrow the activity goes back to the previous activity, but no event is registered in the onKeyUp, OnkeyDown and OnBackPressed methods.
But when I press the Back key on the phone (at the bottom) the activity goes back to the previous one and all the methods onKeyUp, OnKeyDown and OnBackPressed register an event (in the logcat).
How can I capture that left arrow (I think it is called the UP button)?
The reason I need to capture the key is to know in the onPause method that the activity is destroyed by the user and not by the system (for example, if the user switches to another activity).
By further investigating he matter I found out that the UP button gives an event that is captured by the onOptionsItemSelected method and since there is no other button on the menu I know it is this button.
see http://developer.android.com/guide/topics/ui/actionbar.html#Handling
Handling clicks on action items
When the user presses an action, the system calls your activity's onOptionsItemSelected() method. Using the MenuItem passed to this method, you can identify the action by calling getItemId(). This returns the unique ID provided by the tag's id attribute so you can perform the appropriate action. For example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
onUpButtonPressed();
return true;
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Note: If you inflate menu items from a fragment, via the Fragment
class's onCreateOptionsMenu() callback, the system calls
onOptionsItemSelected() for that fragment when the user selects one of
those items. However, the activity gets a chance to handle the event
first, so the system first calls onOptionsItemSelected() on the
activity, before calling the same callback for the fragment. To ensure
that any fragments in the activity also have a chance to handle the
callback, always pass the call to the superclass as the default
behavior instead of returning false when you do not handle the item.
To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
Yes you are right, you can detect if the up button was pressed in the onOptionsItemSelected method. This should work:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Do something here. This is the event fired when up button is pressed.
return true;
}
return super.onOptionsItemSelected(item);
}

Android Toolbar with both home and back button

Is it possible to display both home icon and back icon in the toolbar?
1) Is it possible change the order of display of back button icon and home icon.
Currently it displays the arrow button first and then the logo (home button)
2) Second requirement is to clear the activity stack on clicking the home icon and going back to previous screen in case of back button.
I have the following code which will display a arrow back key and home icon which is set as logo. Is it possible to handle click events on both these icons:
Toolbar toolbar = (Toolbar)findByViewID(R.id.toolbar);
toolbar.setNavigationIcon(R.drwable.btn_back);
setSuppportActionBar(toolbar);
getSupportActionBar().setLogo(R.drawable.home_icon);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I am able to handle to the click on arrow icon by handling it in onOptionsITemSelected method.
Is there a way to handle click on logo icon?
My idea is to use the home button click to clear the stack of activities and use the back button to navigate back to previous screen.
I tried with
toolbar.setNavigationOnClickListener()
but it has no effect on back button click.
Handling android.R.id.home works when handled in
onOptionsItemSelected()
For navigating back. This worked for me.
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}
try with this
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
getActivity().finish();
}
return true;
}
});
Design our custom layout as a separate "toolbar_content.xml" and include this layout inside toolbar tag in your "main_layout.xml".
Write click listeners for your items in "toolbar_content.xml" in your base activity so that listeners will be available thru out the app.

Enabling the action bar's home button

Pressing on the app's icon doesn't change the activity, but I'd like it to return to the app's MainActivity.
I've made my app's icon appear on the left of the action bar by doing the following:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
I'm assuming I should define some functionality in the onOptionsItemSelected() methods of the activities concerned. I've gotten a log of the MenuItem's ID when the app's icon is pressed and onOptionsItemSelected() gets called, but obviously I can't hardcode that id into the switch statement. What should I do?
You should set android:parentActivityName for your activity in the manifest file.
android:parentActivityName="com.example.myfirstapp.MainActivity"
You also need to handle the click in onOptionsItemSelected for the id of android.R.id.home:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
See more on Android developer website.
This is how I'd do it
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.HomeButtonId){
//Return Home
}
}

Actionbar menu onclick

Is there any way to use the standard menu button in the action bar to do something? In my case a sliding menu, I already have code that works in the onClick of a normal button. I just want to use the menu button in the action bar instead. Is this possible? Or will I have to customize the action bar and not use the button that is already there?
I believe you mean the ActionBar Home icon.
First you have to enable it
getActionBar().setHomeButtonEnabled(true);
Then you have to handle the event
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//do your work
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FYI if you want to follow the Navigation drawer pattern you should read this tutorial.

Categories

Resources