In my application,When Touch on List view I need to open a image and When Action_UP called i need to release the image.Here my problem is ,i need to press item 5 second after that i will open image,but when i using onTouch event just i touch on list view the image will open.I need to increase the time of touch event please can any one help me?
Thanking in Advance.
Put a Handler in your onItemClickListener of your ListView inside of your adapter class (in getView function) :
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
}
}, 5000);
}
});
You can use setOnItemLongClickListener() of a listview. with an example given below:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos"+" "+pos);
return true;
}
});
I think this will help...
You can useGestureDetector. Just declare a new GestureDetector and in onTouchEvent method just make a switch clause by EventType. If it is ACTION_DOWN open an image, if it is ACTION_UP release it
NOTE : Do not setOnItemClickListener() on listview.
You can achieve this by doing some trick in your list adapter.
Go to your list adapter and in getView() method.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView.setOnLongClickListener(new OnLongClickListener() {
//your code
#Override
public boolean onLongClick(View arg0) {
//do your code to open image.
return true;
}
});
return convertView;
}
Related
So I have an AutoCompleteTextview which works fine, but I would like to know how to enforce a specific option.
The meaning is to use something like setText(), but setText is not good for me, because it doesn't activate the onItemClick event, and this is what I want to activate.
myAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parentView, View selectedItemView, ()int position, long id) {
// do something...
}
});
Thanks for any help.
You need to declare the listener as a member variable and force call it from outside, like this:
void onCreate(Bundle savedInstanceState){
// setContentView and other stuff
myAutoCompleteTextView.setOnItemClickListener(mOnItemListener);
}
AdapterView.OnItemClickListener mOnItemListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parentView, View selectedItemView,int position, long id) {
// do something...
}
};
and when you need to force call it, use:
mOnItemListener.onItemClick(/*put your forced parameters here*/);
for example:
mOnItemListener.onItemClick(null, null, 10, 20);
I usually use boolean for this but in this situation I can not. I have a code like below on my Spinner and everything works fine :
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
carNames = brand.getChildCarNames(brand.getListDataHeader().get(position));
makeAndShowDialogBox();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
The problem is when I use bolean what happens is that the choice that comes as Default is not showing the DialogBox if user chooses that Default value as-well. Such as "Tomato" comes as Default and user wants to select "Tomato" as well; nothing happens than.
What I want to do is also prevent this dialog box coming asap when Activity is opened but also I want to prevent "on selection of default value nothing happens" issue.
So, Is there any way to check exactly if the User pressed to select or not?
You can use a Boolean variable to determine if the spinner is just loaded, so you can prevent the dialog to show when the Activity is opened (or onResume). You can declare a variable outside the listener, and change its value inside the listener. Its value is changed inside the listener, so you have to do final Boolean array instead of regular Boolean.
You can try with the following code:
final Boolean[] spinnerLoaded = {false};
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (!spinnerLoaded[0]) {
spinnerLoaded[0] = true;
}
else {
carNames = brand.getChildCarNames(brand.getListDataHeader().get(position));
makeAndShowDialogBox();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Due to the items contents in ListView are little bit complex, my ListView takes a few seconds to roll to the particular item. So I want to add a Indeterminate progress bar to make it smooth.
To rolling to a particular item, I used listView.setSelection(n) to do that. Then I tended to use "setOnItemSelectedListener" event handler to hide the progress bar after rolling. But the event handler doesn't get call.
May be my concept are totally wrong, I am no idea how to do that (progress bar using like this case). If you show me the concept more details are also welcome.
Thank you very much.
public class ListViewAdapter extends BaseAdapter {
private ProgressBar progressBar;
private Handler progressBarHandler = new Handler();
boolean isProgressBarDone;
private void jumpToSelectedItem(View arg0) {
...
listView.setSelection(selectedDaYunItem);
}
ListView listView = (ListView)((MyActivity)cellView.getContext()).findViewById(R.id.listview);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
// onItemSelected event handler has not triggered.
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
isProgressBarDone = true;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
public class LiouYunCellAdapter {
cellView.setOnTouchListener(new View.OnTouchListener() {
// It takes a little seconds to operate.
private void jumpToSelectedItem(View arg0) {
....
}
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
progressBar = findProgressBar(arg0);
// ProgressBar start.
new Thread(new Runnable() {
public void run() {
isProgressBarDone = false;
while (!isProgressBarDone) {
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
}
});
}
}).start();
jumpToSelectedItem(arg0);
}
}
return true;
}
});
// End setOnTouchListener().
}
// end class LiouYunCellAdapter.
}
Ok, I dont see you triggering anything onclick excpet for setting isProgressBarDone to true. What you need to do is use the fomat below (which you already have) but put your code to scroll in there. OnClick you will get the position of what was selected. Use that id to then trigger the scroll. You should also initate your progress bar here and end it at the conclusion of that method. Right now your progress bar is running on its own thread and I don't see anything to stop it.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//You will get the positino of the item clicked here
//This is where your code to scroll should go as well as
//starting the progress bar
}
});
Take a look at the example I linked to below. It deals with the use of a progressdialog while loading a custom list view but I think the concept is applicable to your code. What you need to do is fix when you call the progress bar and dismiss it.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
I have a phrasebook, with the ability to save the sample to SD. I use a Gridview set up with the following code in place for the button adapter:
public View getView(int position, View convertView, ViewGroup parent) {
try {
final Sample sample = board.getSamples().get(position);
if (sample != null) {
Button button = new Button(context);
button.setText(sample.getName());
button.setTextColor(Color.WHITE);
button.setTextSize(12);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
context.play(sample);
}
});
// TODO Implement this correctly.
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
return context.saveToSD(sample);
}
});
return button;
}
} catch (IndexOutOfBoundsException e) {
Log.e(getClass().getCanonicalName(), "No sample at position "
+ position);
}
return null;
}
I am looking to integrate a context menu here on a Long press, to give the option of where to save the sample. I don't seem to be able to register the button for the context menu within this method (ie registerForContextMenu(button), as it gives me errors.
I am a bit stumped here, any pointers would be a great help.
Thanks
I take it that this is an old post but I came across it today as I was looking for an answer on the same topic. Like the question here I have a grid of items and wanted to show a context menu on long click.
I am not using a contextmenu but instead I am using an AlertDialog.
gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
{
showOptionsMenu(position);
return true;
}
});
public void showOptionsMenu(int position)
{
new AlertDialog.Builder(this)
.setTitle("test").setCancelable(true).setItems(R.array.myOptions,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
//take actions here according to what the user has selected
}
}
)
.show();
}
Hope this helps.
This is driving me nuts since it's something I've done before but can't figure out why it isn't working now...
I've got a menu button, implemented in the usual way via a menu.xml file and the onOptionsItemSelected method with a switch in it, that creates and displays a spinner.
I've added the setOnItemSelectedListener, but it never seems to trigger. The spinner appears, I pick an option or back out, neither onItemSelected or onNothingSelected are called.
Here is all the code between the "case" and "return true" of the menu-button-handling switch statement. (topThis is a variable referring to the context of the activity - works fine for all other toasts in the app)
String[] widgetModes = {"Mode 1", "Mode2"};
ArrayAdapter<String> widgetModeAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, widgetModes);
widgetModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner widgetModeSpinner = new Spinner(this);
widgetModeSpinner.setAdapter(widgetModeAdapter);
widgetModeSpinner.setPrompt("Choose Widget Mode");
widgetModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Toast.makeText(topThis, "derp", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
widgetModeSpinner.performClick();
Any ideas? I vaguely suspect that the fact I'm creating the Spinner programmatically is the problem...
I had the similar problem when I was implementing a spinner, I resolved it by getting the parent View and set Adapter-
spinner1 =(Spinner)findViewById(R.id.spinner1);
spinner1.setAdapter(BindSpinner("ProgramMaster",cols,null,true,""));
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
protected Adapter initializedAdapter=null;
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
if(initializedAdapter !=parentView.getAdapter() ) {
initializedAdapter = parentView.getAdapter();
return;
}
String selected = parentView.getItemAtPosition(position).toString();
if(abc.equals("Select") && !selected.equals("Select"))
{
do something
}
else
{
Do something
}
textQualification=selected;
SearchUpdated("Qualification");
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Remember that you can't re-select the same spinner item, it always sets the first item as selected if you are not adding some custom code to handle the spinner selection.
For the Toast not showing, I would suggest to always use the "MyActivity.this" as your context when creating a Toast inside a listener interface like this:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item is selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An spinnerItem was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
Toast.makeText(MyActivity.this, "Hello Toast",Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing, just another required interface callback
}
}); // (optional)
And the .show() at the end is easy to forget sometimes;)
Actually, if your spinner visibility is set to gone then it will trigger the click of it when you call performclick() method but it will not trigger its setOnItemSelectedListener
so you need to change the visibility then it will work
I know the question is a bit old, but in case you are waiting for a AsyncTask callback, make sure that you let your adapter know of the data changes by calling notifyDataSetChanged() on the callback thread!
#Override
public void onPostExecute(String result) {
///do something with your data
spinnerArrayAdapter.notifyDataSetChanged();
}