highlight previous selected item when user come back from another activity - android

Activity1 has a listView. Clicking one item (let's say item 3) will start Activity2. Activity2 have a back button once clicked will bring user back to Activity1. What I want to achieve is to highlight the item 3 when user back to Activity1 so that the user have a sense where to continue. (May be i need to set the focus to item 3 as well.)
EDIT: Following code works.
public void onResume()
{
super.onResume();
//lastSelectedPosition saved in OnItemClickListener
lv.setSelection(lastSelectedPosition);
lv.requestFocusFromTouch();
}

well its pretty simple. just save the clicked item position of the list to a field when the list is clicked to launch your new activity.
Afterwards in the onResume() method just use myList.setSelection(savedPosition);
as for the highlighting, well focus works kinda bad especially if you have a bit more complex rows(buttons,checkboxes etc) and other ui elements beside the list that can take away the focus. i believe the best way to achieve this is just set the background of that particullar item onResume to the highlighted one and override onScroll listener to just change the background to your default when the list is scrolled. indeed its a workaround but it will work in 100% of the cases opposite to just focusing an item. plus maybe you can add animations on the view so you can make it look really nice and smooth.

Related

ListView updating Items without being recreated

I want my ListView to work something like the following:
When I press a button (probably from context-menu), I want the user to be able to select more then one item from ListView (probably using check-boxes), but those check-boxes should not be visible before that.
So, the point is, after the user presses a button (let's say "Delete more items"), the listview, should update itself, and appear on every row of the list, a checkbox should appear (allowing me to select the items ID to pass those to server).
How can I achieve that, without having to recreate the list from zero? (how to setVisibility ON, keeping the other content of the ListView as it is, and not doing another request to server).
PS. If you guys, have another better idea, on achieving the Delete More Items, would be much appreciated!
This is just an idea, haven't tried it myself: you build in a checkbox in your listitem layout. Normally, in the getView of your adapter, you set it invisible with
checkBox.setVisibility(8);
When you want to show them, you set some boolean
showBoxes
of your adapter to true, then in the getView oyu don't hide the checkboxes.
Then
notifyDataSetChanged
on the adapter.
Hope it's clear what I mean.

Efficient way to display details of list item

I am working on app that is targeted to 7" or above tablets. So target platform is Android 3.x.
I am looking for an efficient approach to display details for list item when it is clicked. I have a list displayed with bunch of items. (Note: Due to the nature of the application I do not want to share ListView with other view or Fragment in this activity). When an item of list gets clicked I have to display details of that time. Details of an item take pretty much whole screen. I have couple of approach in mind:
Simplest one is : on List item clicked simply start an Activity that displays details of that item. But I guess it didn't seem efficient as every time item is clicked this Detail Activity is created and then destroyed.
Create a custom dialog for Detail of list item and hold its reference in List Activity. As soon as item is clicked show this dialog displaying contents corresponding to clicked list item. With this approach I would like Dialog to take complete screen (any suggestion appreciated).
Define FrameLayout in ListView with Visibility Gone (so that frame layout dont take any space in ListView screen). This frame layout act as a container for Details Fragment. As soon as list item is clicked hide ListView and make frame layout visible. With this approach I am not efficiently navigate back and forth between list view and details view.
Should come from an Android expert. :)
Thank you for your time.
There isn't any reason why you can't take approach 1 (create an Activity to display the details). Even better combine 1 & 2 to create an Activity with a dialog theme with something like this in the AndroidManifest.xml...
<activity
android:name=".DetailsActivity"
android:theme="#android:style/Theme.Dialog">
</activity>
Give the Activity a 'Close' button which when clicked, calls finish() to exit the Activity. I have several dialog-themed Activities in my current project and they work really well.

Enable button click only after the page is loaded

I have a page which contains a listview alone. Upon clicking a row in the list, the next page is loaded. This next page contains a set of buttons. Upon clicking a row in the list, the loading of the next page may take sometime. So if someone is continuously clicking the row then at some point of time, the second page comes and the buttons inside that gets clicked, which I dont require. How can I prevent this clicking of buttons? Can someone help me out. Thanks in advance.
Use AsyncTask concept to load data for next activity, do as follow:
set setEnabled(false) to all buttons inside the onPreExecute() method.
do all the background task inside the doInBackground() method.
and now again setEnabled(true) to all the buttons so that it become enabled and user can click on the buttons.
Besides the fact that your activities shouldn't take that long to load initially, you could just set a boolean when startActivity() is called to start the new activity. Then check that boolean if the user clicks anything else before the next activity is loaded.
if(document.readyState==complete) //NB this property is not supported by firefox
{
//Activate buttons
}
i think you need to false click event and true while you want to use
like this::
button1.setClickable(false);
and when you want to enable just write
button1.setClickable(true);

Android ArrayAdapter tell all other views to refresh themselves

I need all of the other views in my arrayadapter to check their attributes and return to a default attribute at certain times
use case: listview items (a)(b)(c)(d) , when you touch (a) it's background turns black. This is done in the ontouchListener. But when you touch view (b) it's background turns black BUT (a)'s needs to turn back to the default which is not black
I dabbled around with recalling the arrayadapter at the end of the view.OnClickListener but the problem with this is that it also resets the scroll position of the view, so if this list was much longer - which it is - and user touched item (r) , then it would reset the list and put the user back at item (a) at the top
I was looking at notifydatasetchangedbut I am not sure how to use that and where it should work
insight appreciated
notifyDataSetChanged will do the job. You can call that at any time in your code and this will trigger the adapter to call getView for the visible list items again and so update their content. So basically the only thing you have to do is to update the list item states or information before calling that method.

Deselect any trackball/trackpad selection

Is there a way to programmatically deselect/wipe whatever the user has selected with the trackball/trackpad?
When I hit the back button on an Activity, the Activity it falls back to has a button that is selected as if the user had used the trackball/pad. I'm not sure what is selected on the previous Activity, but obviously something is. I'd like to programmatically wipe any selection just before the Activity finishes.
Looking through the JavaDoc for View I see a number of focus-related functions.
void clearFocus(); // drop focus from this view.
View findFocus(); // finds a view that is a child of this view that has focus, if any
View focusSearch(int dir); // finds the next view that can take focus in the given direction
void requestFocus
Sounds like findFocus().clearFocus() should do the trick (unless findFocus happens to return null)... you just need a handle to the other activity's View... which shouldn't be too hard if it's your code, or Non Trivial if it isn't.
If it IS your code, it seems like you could just add a clearFocus() to the button's onClickHandler.

Categories

Resources