My bottom navigation bar looks visually okay it contains 4 icons each moving you to a different activity and the default one is the "home" activity.
I made my navigation bar with a menu btw.
Whenever you click on an icon in order to move to another activity it moves you into that activity but the bottom navigation bar (who is supposed to highlight the screen you are in) doesn't update unless you press on the same icon again, whenever you click to move another activity it highlights the "home" activity first, only if you click the same icon does it update.
This is my code.
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.post:
Toast.makeText(CreateNewPost.this, "Post", Toast.LENGTH_SHORT).show();
break;
case R.id.myProfile:
Toast.makeText(CreateNewPost.this, "My Profile", Toast.LENGTH_SHORT).show();
Intent profileIntent = new Intent(CreateNewPost.this, UserProfile.class);
startActivity(profileIntent);
break;
case R.id.explore:
Toast.makeText(CreateNewPost.this, "Explore", Toast.LENGTH_SHORT).show();
Intent newPostIntent = new Intent(CreateNewPost.this, Explore.class);
startActivity(newPostIntent);
break;
case R.id.home:
Toast.makeText(CreateNewPost.this, "Home", Toast.LENGTH_SHORT).show();
Intent homeIntent = new Intent(CreateNewPost.this, HomeScreen.class);
startActivity(homeIntent);
break;
default:
}
return true;
}
And this is how it looks
I want the navigation bar to highlight the correct activity (the one you are on)
I'm building an app with a navigation drawer, and an action bar with a button which pressed should take users to a log in page. The log in page is an activity.
I have inserted what I believe to be the appropriate code, however the section in bold comes back with the following error: Annotations are not allowed here
This code previously worked, and now it has this error and my app obviously wont launch. Does anyone know why? Thanks!
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_LoginPage) {
Toast.makeText(getApplicationContext(), "Welcome to the Employee Log-in Page", Toast.LENGTH_LONG).show();
switch (item.getItemId()){
case R.id.action_LoginPage:
Intent intent = new Intent(this, LoginPageMain.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
**#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Code for Nav Bar Item Clicks (Button Clicks)
int id = item.getItemId();**
I am currently following the Google documents for Android programming. I am at the part where I need to perform a task when a button is pressed on the action bar. Here is the code I have:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
However on if(id == R.id.action_settings), I get an error saying that the symbol r can not be resolved. What could be causing this as it all looks fine to me.
Aside from missing id which renders OP's broken, anything below switch() is simply dead code and can be safely removed (I'd bet your IDE is already flagging you this anyway):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT
I get an error saying that the symbol r can not be resolved.
Not seeing real error message, I'd assume the complain here is due to the id but if it's really about R, then check import section to see if you got import android.R there. If so, remove it and try again.
if (id == R.id.action_settings) {
return true;
}
Won't compile because you are not declaring what id is anywhere before that.
Did you delete int id = item.getItemId(); by accident?
Edit : that whole if condition is redundant anyways, you are handling action_settings in code before.
I'm new to Android and I trying to do a simple app.
In the "home", I write my name and press the "send" button.
In the second page, the app show "Hello " + inserted name.
The problem is:
When I touch the back virtual button in nav bar, I come back to the home and in the input field there is always the name; but when I go back with the back button in header bar, the input field is empty.
I've tried with:
in onOptionsItemSelected:
switch (item.getItemId()){
case android.R.id.home:
Log.d("case","go back");
NavUtils.navigateUpFromSameTask(this); //or with finish();
return true;
}
it prints "case: go back" in console, but doesnt' work correcly.
My custom theme have as parent Theme.AppCompat.Light.DarkActionBar and if i put getSupportActionBar().setDisplayHomeAsUpEnabled(true); in onCreate, Android Studio tells me "Method invocation 'getSupportActionBar().setDisplayHomeAsUpEnabled(true)' may produce 'java.lang.NullPointerException'".
I solved adding this in the second Activity:
#Override
public Intent getSupportParentActivityIntent() {
return null;
}
and edit the switch/case function like this:
switch (item.getItemId()){
case android.R.id.home:
this.finish();
return false;
default:
return super.onOptionsItemSelected(item);
}
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...