Actionbar set the home button - android

I am using a NavigationDrawer with my ActionBar. It needs the home button and configured as up.
I try to put a button in the middle of the action. The only way I found is to use a custom layout.
But when I use it, my home title is erased. Even if it has the space to be displayed.
Is there a way to set the home button to always show his title ?
If not is there an other trick ?
Thanks in advance :)

actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
and add
// For back Button
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, DashbordActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}

try following code
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);

Related

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.

Android Action Bar Logo Home Button

I'm currently developing an android application and i'm looking to make the logo on the left of the action bar a menu button. I have enabled the logo using the code
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
from my research I should be able to make this a a menu button by using the code
if (id == R.id.home) {
Intent i = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(i);
}
in onOptionsItemSelected but this is not working for me.
Is there a way to change or find the correct ID for this logo as im starting to think R.id.home is incorrect
I think with enabling your home button with
actionBar.setDisplayHomeAsUpEnabled(true);
and then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//fire intent or whatever you require.. do over here
return true;
default:
return super.onOptionsItemSelected(item);
}}
I think this should work fine.

Android - actionBar.setDisplayHomeAsUpEnabled issues

In my activity which extends SherlockFragmentActivity I have action bar and have also set actionBar.setDisplayHomeAsUpEnabled(true) to come back to my previous activity. Everything works fine. But when i click on home button, the background color should be applied only to the app icon but it also applies to the "title" on action bar as shown in screen shot. I dont want this to happen. When i click on home button, only the app icon should be click able(background color should be applied only for app icon) and not the title. Any idea how can i do this?
my activity code:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(some_string);
Don't forget this part, because that home button is also a MENU button.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This is called when the Home (Up) button is pressed in the action bar.
// Create a simple intent that starts the hierarchical parent activity and
// use NavUtils in the Support Package to ensure proper handling of Up.
Intent upIntent = new Intent(this, MainActivity.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is not part of the application's task, so create a new task
// with a synthesized back stack.
TaskStackBuilder.from(this)
// If there are ancestor activities, they should be added here.
.addNextIntent(upIntent)
.startActivities();
finish();
} else {
// This activity is part of the application's task, so simply
// navigate up to the hierarchical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
The standard Android UI pattern is to include the title in the pressed state.
However, I think if you followed this link (describing how to add a custom view to the left of an actionbar) How to align items in action bar to the left? , and set the title to empty. The below code worked on my local device
ActionBar action=getActionBar();
action.setDisplayShowCustomEnabled(true);
action.setHomeButtonEnabled(true);
action.setDisplayShowTitleEnabled(false);
TextView title =new TextView(getApplicationContext());
title.setText("Your Title here");
title.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
action.setCustomView(title);

Android - Click on ActionBar app icon, create new activity instance

I have an ActionBar in my Android app (API Level 14).
There is a home button with my app icon. In MainActivity I write a short Text in an EditText View.
When I navigate to PreferenceActivity the icon gets an arrow to signal me, I can navigate to home Activity (MainActivity).
// PreferenceActivity-onCreate
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
I click on that app icon in ActionBar to return to MainActivity
// PreferenceActivity
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now my MainActivity was created again und the text in EditText is gone.
I thought I can keep alive the MainActivity with die Intent.FLAG_ACTIVITY_CLEAR_TOP.
I want to have a behaviour like i use my return button on device.
If you want to return to an existing instance of MainActivity, you need to do this:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Using CLEAR_TOP alone causes a new instance of MainActivity to be created.
I believe this is the correct way of doing this. https://stackoverflow.com/a/15933890/238768
Using Intent.FLAG_ACTIVITY_CLEAR_TOP will cause the exact opposite behaviour of what Gepro wants to do!

Back Arrow in Action bar Sherlock is not displaying

In my project I have used action bar sherlock library. I want to make back button in action bar I used following code
getSupportActionBar().setHomeButtonEnabled(true);
And
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
Every thing is working fine, but back Arrow (to the left of icon) is not displaying. I want to show Back Arrow also. Can anyone tell me what I am doing wrong.
You have to set it up this way:
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
Hope that helps.

Categories

Resources