Android Action Bar Logo Home Button - android

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.

Related

Action bar on Action back (Navigation Up) to custom activity?

I have many activities in my application
in that I have Given this
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
to Go back to Home Page from other activities
like Below
If I press those three buttons all menus are working
this is my Option menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.abc) {
Intent mypIntent = new Intent(this, abc.class);
startActivity(mypIntent);
return true;
}
else if (id == R.id.web) {
Intent webIntent = new Intent(this, Web.class);
startActivity(webIntent);
return true;
}
else if (id == R.id.about) {
Intent aboutIntent = new Intent(this, About.class);
startActivity(aboutIntent);
return true;
}
.
.
.
..
return super.onOptionsItemSelected(item);
}
Here There is no menu named id == R.id.login or id == R.id.home but its going to login few days back its gone to home activity
but If I press Back.. action back is redirect to Login page Inst-ed of Home
I have added a Login page for my application using shared preferences.. and it is now launcher activity..
Here In my Login activity on if once user is sign in it should it should redirect to Home activity on every time..
and its working fine..
But on action bar when I press arrow button it is redirecting to empty login page..
if I press cancel entire app is working .. my credentials are safe except this action bar..
Update
I have Given Intent also if Login credentials success redirect to Home activity on app start up it will check every time
every thing is as for fine except action back
how to fix this...
Alright make sure u do declare activities as below -
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
Now in every activity add below code block-
#Override
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);
}
Update
add a New class file to check login or not else use home as default.. and replace your new class as launcher in Manifest
Just override this method.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
{
Intent intent = new Intent(mContext, Activity.class);/*your activity name*/
startActivity(intent);
}
default:
return super.onOptionsItemSelected(item);
}
}

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 set the home button

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);

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.

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