Clicking the top home button doesn't do anything - android

I am using actionbarsherlock. I have three screens as follows
lists items in list activity
lists items in list activity after an item from first screen is clicked
shows details about the clicked item.
On second screen I want to have home button that takes the user back to first scree, and on third screen I want to have a home button that takes the user back to second screen.
Here is what I'm doing:
//on second activity:
getSupportActionBar().setTitle("First");
getSupportActionBar().setHomeButtonEnabled(true);
//on third activity:
getSupportActionBar().setTitle("Second");
getSupportActionBar().setHomeButtonEnabled(true);
This makes the button on top left show up but nothing happens when I click it.
I found online that this change needs to be made in onOptionsItemSelected as well. But I don't have android.id.home in my code. My IDE throws error on android....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == /*what should I put here*/) {
finish();
return true;
}
return true;
}

you should put here android.R.id.home - it is an Android home button id

Related

Trying to close menu by pressing outside of it

Hey guys I'm making a pocketbook in Unity. On the upper right side corner there's a menu button that I've coded to make a drop-down list of links appear that will bring you to different pages of the pocketbook. Right now the only way to make the list dissapear is to click on the menu button again, which is inconvenient. I want the user to be able disable the menu by tapping anywhere else on the screen (I've created an invisible button that covers the rest of the screen, only problem is I want it to be active only when the scroll menu is active) like a normal menu screen. This is the code I've created for it. Menu Button is the menu button, close button is a button I created that covers the rest of the screen, I'm struggling to make the button active only when the scroll menu is up otherwise it will stop the user from scrolling through the pages. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuButton : MonoBehaviour {
public GameObject scrollMenu;
public Button menuButton, closeButton;
public bool active = false;
// Use this for initialization
void Start () {
Button btn = menuButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
closeButton.enabled = false;
}
// Update is called once per frame
public void TaskOnClick ()
{
if (active == false)
{
scrollMenu.SetActive(true);
active = true;
closeButton.GetComponent<Button>().interactable = true;
} else
{
scrollMenu.SetActive(false);
active = false;
}
}
}
What you can try is.
1. Make a button part of your menu, it should cover the entire screen
2. it must visible/hide with your menu
3. it must be transparent.
4. on the button click event attach menu hide code.
I have done that and it's working perfectly.
see the image below

Updating navigation drawer after back press

I'm implementing an Android application with a Navigation Drawer and I have a problem with it.
I started from the template (Android app with navigation drawer) in Android studio and added a new section to it - Settings. When clicking the settings button the user is taken to a new Activity and the user can go BACK either by pressing the "up" button in the action bar (Which works perfectly) or by pressing the physical back button. That part doesn't work so good.
When pressing the "up" button and then opening the navigation bar, my home page is highlighted (selected). But when using the back button and opening the navigation drawer, the Settings item is selected, as if the user is viewing that page and not the home page.
So this is because the Navigation Drawer fragment isn't updated and I havent found any way to solve this problem. What I would like is for the Navigation Drawer to be recreated (Which is what I think happens when pressing the "up" button). Do you know how I can make this happen?
I started working on a solution based on this:
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
// Update your UI here.
}
});
But I don't know what to write there that will update the whole UI. (experimented with calling onCreate() but it's too ugly and can't be the right way).
Any suggestions?
UPDATE:
The onBackStackChanged event isn't sent when pressing BACK from another activity. Only for fragments in the current actvity.
You have to override the actionbar back button like this :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
//or
super.onBackPressed();
break;
}
return true;
}
I had the same problem.I used notifyDataSetChanged() in the ListView Adapter.
In getView() i used,
if(mListView.isItemChecked(position)) {
convertView.setBackgroundColor(getResources().getColor(R.color.md_black_1000_12));
}else{
convertView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
notifyDataSetChanged();

How do i detect when the back button on an Android action layout is tapped?

I'm making an Android app that allows the user to search a collection of information. I begin by displaying the collection, but when a search button in the actionbar is tapped, an action layout (search bar) shows in the action bar. When a search is conducted, the listview information is replaced with information from the query. However, when a user taps the back arrow button next to the search bar, I would like to replace the queried information with the original information.
Is there any way to detect when the back arrow button is tapped next to the search bar? Thank you very much for your help!
Menu XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="">
<item
android:id="#+id/search"
android:title="Search"
android:showAsAction="always|collapseActionView"
android:actionLayout="#layout/some_layout"
/>
</menu>
Activity Code: trying this: SearchView onMenuItemActionCollapse Not Working
but keep getting a Null Pointer Exception
There are some ways to detect it indeed. First thing that came up to me was the following: create a boolean with false as default value. Whenever you query for specific search results, you turn the boolean to the true. When the 'back arrow' (navigation up) button has been pressed, you check whether the boolean turns out to be true. If it's true, it means that the app doesn't show you the original information set and shouldn't close your activity.
For example (from Google docs, partly.):
public boolean originalContentReplaced = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
if(originalContentReplaced) {
// Do what you need to do here to get the original content back.
this.originalContentReplaced = false;
} else {
NavUtils.navigateUpFromSameTask(this);
// You could also use onBackPressed().
}
return true;
}
return super.onOptionsItemSelected(item);
}
I don't know how you query your new search results but when your search is successful, you should change originalContentReplaced variable to true.
You could use the very same if-else statement when you press on the hardware back button. Anyway, this should give you a very clear idea on how to detect when the navigation up/back arrow button is pressed. :)

Android Home Up Button -- Specify the Parent Activity Fragment

I am using a Navigation Drawer on the main activity of my app with three options. Option one is the default fragment being used in the main activity. Clicking option two just replaces the old fragment with a new, but stays in the same main activity. This is all good.
Next step is that I click one of the list items in option two and it opens a new activity+fragment. I implemented the Home Up button in the new fragment with
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The problem comes up with I hit the Home Up button. It takes me to the main activity with the initial fragment (option one) and not the option two fragment. How do I change this?
You need to override onOptionsItemSelected in Activity two and just finish it when home pressed
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

How to refresh the screen when back button is pressed

I have scenario with two screens.
Screen 1 shows data from from API in list format
There is a "+" button in menu bar
Clicking this button takes user to screen 2
User can enter some info on screen 2 and press the "save" button on top of this screen. This does a POST to my API and saves the data.
After saving, I would like to put the user back to screen 1. I've done that with this:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
new CreateSomethingTask(this,enteredName.getText().toString(), id).execute();
Intent listscreen = new Intent(getApplicationContext(), ShowListActivity.class);
startActivity(listscreen);
return true;
}
return true;
}
However, the added item is not shown. If I close my app and open it again then the item shows up.
Is there a good way to handle this? I like how the Github Android app handles this when creating a new Gist. But I'm not sure how to implement that.
You should start your screen2 with startActivityForResult(). That way you could send a result back and a code and proceed to refresh your screen1. See example : How to manage `startActivityForResult` on Android?
Below function maybe help you, didn't tried.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
callFunctionToRefreshList();
//or redraw data from api
//setContentView(R.layout.activity_book);
}

Categories

Resources