I`m using SherlockActionBar for my application. In my manifest I define icon for logo and launch.
android:icon="#drawable/ic_launcher"
How to make it clickable and handle the event of it`s pressing? I want to be able to back to my dashboard by pressing the logo.
Use
actionBar.setDisplayHomeAsUpEnabled(true);
with
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can read more about the home button here http://developer.android.com/guide/topics/ui/actionbar.html#Home
It explains how to clear previous the stack of previous activities and has some more links to best practices on navigation using the home button.
Related
I have created a Toolbar for an activity in Android in Xamarin. I have enabled the back/home button with SupportActionBar.SetDisplayHomeAsUpEnabled(true);. I am trying to capture the event of pressing the back/home button with the following code, as instructed by this and many other stackoverflow posts:
public override bool OnOptionsItemSelected(IMenuItem item)
{
System.Diagnostics.Debug.WriteLine("OnOptionsItemSelected() called: " + item.ItemId);
switch (item.ItemId)
{
case Resource.Id.home:
System.Diagnostics.Debug.WriteLine("Home button pressed");
Finish();
return base.OnOptionsItemSelected(item);
default:
return base.OnOptionsItemSelected(item);
}
}
When I press the back button, OnOptionsItemSelected is called, but item.ItemId is not equal to Resource.Id.home. The former is 16908332 (tested on two different devices) but the latter is 2131492903. How can I capture the home/back button from the toolbar in Xamarin? One possible option is to hardcode the back button ID as 16908332, but I do not know if that number will stay the same permanently.
You are using the wrong resource, you want the one from the Android space:
Android.Resource.Id.Home
The right one to use is
case Android.Resource.Id.Home:
Finish();
break;
I am a noob in android development and I follow the tutoral of the android website.
1. In the part of "Starting Another Activity", I just copied the code and tried to run it, but I found after the activity is changed (changed to new page), the title of action bar will change to the name of the class of that activity.
2. When it talks about the respond of the action button, the code is written as:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, in the default code:
`public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}`
It only return true which contains no method to respond(no openSettings()), but a "setting" word still pop out when I press it.
3. How do I remove the action bar and make it full screen?
Don't fully understand your question (you really didn't ask one specifically) but I think this is what you're asking
How to change the title of a new activity?
Here: How do I change the android actionbar title and icon
How does settings open up?
Android does this automatically, if the onCreateOptionsMenu(Menu menu) is called.
How do i make a full screen activity?
Here: Fullscreen Activity in Android?
In the future, be sure to look through Google and StackOverflow for you answers, most likely someone has already asked a similar question
The Name of the activity is, in your case, is declared in AndroidManifest.xml. Check the android:label attribute of your activity. You can manipulate it through java code if you want. See this SO question.
Check the menu file under res/menu/your_menu_file.xml. I think it contains something like
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
The displayed icon is NOT setting icon, but overflow icon. If the overflow icon is clicked, it lists all menu items. In your case only one (i.e Setting)
To hide the action bar, include these in your onCreate() method
//getWindow().requestWindowFeature(Window.FEATURE_NO_TITLE);
getActionBar().hide();
You can also do it via xml. Look at this SO question.
I have an application that calls an activity several times from different activitys.
So, im trying to implement the "back button" in the action bar for this activity.
For doing this im using:
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
and:
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="view.TweetsIndividuoActivity" />
The problem now, is that i cannt set a parent activity to my android manifest, cause, i don't know who is the parent of this activity.
What is the solution ?
Thanks
It's easier than you think.
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
Method finish() will destroy your activity and show the one that started it. That's what you want if I understood you right.
Your current solution is meant for cases when you want go back to the same parent every time e.g. Gmail app does it. When you open email from notification and then press actionbar back button it will not navigate back to HOME screen but it will show you Gmail inbox.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
You will always go back to the activity from which you have launched the new activity.
No need to use the code below.
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="view.TweetsIndividuoActivity" />
I'm a newbie to Android too, but I solved this problem by calling the 2nd activity using "startActivityForResult(intent,1)" instead of "startActivity(intent)". I think this makes it a parent/child relationship instead of a sibling activity...?
I didn't need to use onOptionsItemSelected() or finish().
I am developing an Android app and I am trying to put a menu item into the ActionBar.
It has enough space, so it shouldn't be on the overflow or anything.
In my menu.xml I have added that item + android:showAsAction="ifRoom|withText"
However, no matter how large the screen is, that damn menu will not show up on the ActionBar (although it is present in the menu, if the user presses a key).
Unfortunately I cannot post any full-code since I am under a non-disclosure agreement, but I will answer all questions.
The section where I inflate the menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drinks, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.done:
//stuff
}
return super.onOptionsItemSelected(item);
}
Since you have only a menu item, use this attribute instead: android:showAsAction="always".
EDIT Above works all the time if you're running the code on post Honeycomb. But, in order to run on pre-Honeycomb, according to developer article, you need to extend from ActionBarActivity, that means adding compatibility support v4 & v7 and set the following theme for your activity:
<activity android:theme="#style/Theme.AppCompat.Light" ... >
... or a Them.AppCompat theme. Or use one of your own that extends from these.
How to handle "Up" button (SDK version 11+)? I am referring to the one at the top of screen, that holds the application icon.
In Android Design articles it was named as "Up button", but I didn't found it (or similar) in KeyEvent fields.
Implement onOptionsItemSelected() and watch for android.R.id.home "menu" events, as is described in the documentation.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// do something useful
return(true);
}
return(super.onOptionsItemSelected(item));
}
First change your AndroidManifest.xml file to have a parent activity declared. Eg
<activity android:name=".theory"
android:parentActivityName=".MainActivity"
android:label="#string/theory"
/>
<activity android:name=".experimental"
android:parentActivityName=".MainActivity"
android:label="#string/exp"
/>
Do this for all the activities other than the MainActivity.
Note the parentActivityName xml code
Then go to the respective java files and add the following code
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
You have you up button enabled now.