I have two buttons that increment and decrement a value by one with each press and they're working just fine with the onClickListener. I see that an onLongClickListener exists, which I assume is for touch and hold events. How would I have the number rapidly increment/decrement if the button is held?
Am I correct in assuming that onLongClickListener only fires once per long click? Is there a more appropriate listener or a property somewhere that I'm not aware of?
You may implement it as in the following code.
package org.me.rapidchange;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends Activity implements OnKeyListener,
OnTouchListener, OnClickListener {
private class UpdateCounterTask implements Runnable {
private boolean mInc;
public UpdateCounterTask(boolean inc) {
mInc = inc;
}
public void run() {
if (mInc) {
mHandler.sendEmptyMessage(MSG_INC);
} else {
mHandler.sendEmptyMessage(MSG_DEC);
}
}
}
private static final int MSG_INC = 0;
private static final int MSG_DEC = 1;
private Button mIncButton;
private Button mDecButton;
private TextView mText;
private int mCounter;
private Handler mHandler;
private ScheduledExecutorService mUpdater;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INC:
inc();
return;
case MSG_DEC:
dec();
return;
}
super.handleMessage(msg);
}
};
mIncButton = (Button) findViewById(R.id.inc_button);
mDecButton = (Button) findViewById(R.id.dec_button);
mText = (TextView) findViewById(R.id.text);
mIncButton.setOnTouchListener(this);
mIncButton.setOnKeyListener(this);
mIncButton.setOnClickListener(this);
mDecButton.setOnTouchListener(this);
mDecButton.setOnKeyListener(this);
mDecButton.setOnClickListener(this);
}
private void inc() {
mCounter++;
mText.setText(Integer.toString(mCounter));
}
private void dec() {
mCounter--;
mText.setText(Integer.toString(mCounter));
}
private void startUpdating(boolean inc) {
if (mUpdater != null) {
Log.e(getClass().getSimpleName(), "Another executor is still active");
return;
}
mUpdater = Executors.newSingleThreadScheduledExecutor();
mUpdater.scheduleAtFixedRate(new UpdateCounterTask(inc), 200, 200,
TimeUnit.MILLISECONDS);
}
private void stopUpdating() {
mUpdater.shutdownNow();
mUpdater = null;
}
public void onClick(View v) {
if (mUpdater == null) {
if (v == mIncButton) {
inc();
} else {
dec();
}
}
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER;
boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN
&& event.getAction() != KeyEvent.ACTION_MULTIPLE;
if (isKeyOfInterest && isReleased) {
stopUpdating();
} else if (isKeyOfInterest && isPressed) {
startUpdating(v == mIncButton);
}
return false;
}
public boolean onTouch(View v, MotionEvent event) {
boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
if (isReleased) {
stopUpdating();
} else if (isPressed) {
startUpdating(v == mIncButton);
}
return false;
}
}
I had this same goal and ended up using an OnLongClick to catch the down portion to start a repeating event via a handler, then the normal OnClick to catch the release and stop it. Works beautifully for me.
mOngoingRunnable = new Runnable() {
public void run() {
// do stuff
mHandler.postDelayed(mOngoingRunnable, delayMsecs);
}
};
public boolean onLongClick(View view) {
mHandler.post(mOngoingRunnable);
mOngoing = true;
return false;
}
public void onClick(View view) {
if (mOngoing) {
mHandler.removeCallbacks(mOngoingRunnable);
mOngoing = false;
}
}
Related
I need to add a microphone image button on the right of the EditText inside EditTextPreference so when this image button is hit, the Recognizer will be triggered to convert speech to text.
Try this..
Speech To Text
For Speech to text you take some other example... or Enabling Offline Mode.
setOnTouchListener Method used for your click you want take right drawable
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class demon extends Activity {
EditText editComment;
private final int SPEECH_RECOGNITION_CODE = 1;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.demo);
editComment=(EditText) findViewById(R.id.edittext);
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0)
{
editComment.setEnabled(false);
editComment.setText("Recognizer not present");
}
editComment.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
startSpeechToText();
return true;
}
}
return false;
}
});
}
private void startSpeechToText() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speak something...");
try {
startActivityForResult(intent, SPEECH_RECOGNITION_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
"Sorry! Speech recognition is not supported in this device.",
Toast.LENGTH_SHORT).show();
}
}
/**
* Handle the results from the voice recognition activity.
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SPEECH_RECOGNITION_CODE: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String text = result.get(0);
editComment.setText(text);
}
break;
}
}
}
}
Text To speech
demo.java
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class demo extends Activity {
EditText editComment;
TextToSpeech t1;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.demo);
editComment=(EditText) findViewById(R.id.edittext);
editComment.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
String toSpeak = editComment.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
return true;
}
}
return false;
}
});
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
}
public void onPause(){
if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edittext"
android:padding="10dp"
android:layout_margin="10dp"
android:drawableRight="#drawable/icon"/>
</RelativeLayout>
Enabling Offline Mode
(source: androidhive.info)
I am trying to create an android application which acts as a Web Socket server. Here is my MainActivity.java.
package com.example.websocket;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import org.apache.http.conn.util.InetAddressUtils;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText port, msg;
Button listen, send;
TextView status;
int p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
port = (EditText)findViewById(R.id.editText1);
msg = (EditText)findViewById(R.id.editText2);
listen = (Button)findViewById(R.id.button1);
send = (Button)findViewById(R.id.button2);
status = (TextView)findViewById(R.id.textView1);
listen.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String temp = port.getText().toString();
p = Integer.parseInt(temp);
try
{
custom_web_socket wsocket = new custom_web_socket(p);
wsocket.start();
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class custom_web_socket extends WebSocketServer
{
public custom_web_socket(int port_add) throws UnknownHostException {
super(new InetSocketAddress(port_add));
// pass
}
#Override
public void onClose(WebSocket arg0, int arg1, String arg2, boolean arg3) {
status.setText("Connection closed.");
}
#Override
public void onError(WebSocket arg0, Exception arg1) {
status.setText((CharSequence) arg1);
}
#Override
public void onMessage(WebSocket arg0, String arg1) {
// TODO Auto-generated method stub
}
#Override
public void onOpen(WebSocket arg0, ClientHandshake arg1) {
status.setText("Connected");
}
}
}
When I run the program, the application starts. However, wheneve I try to enter value into the EditText field, the application hangs and ends. I am referring this example for web sockets - https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/ChatServer.java
I have imported the necessary jar file. This is my first attempt at android programming and I am not sure what the problem could be.
You are using an event onClick.
So whenever you are trying to type something , it calls your method. and hangs up.
your action should happen when user finished typing the code.
You can use following code
port.setOnEditorActionListener(
new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent
event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode()==KeyEvent.KEYCODE_ENTER)
{
if (!event.isShiftPressed()) {
// the user is done typing.
return true; // consume.
}
}
return false; // pass on to other listeners.
}
});
Also , refer to http://developer.android.com/reference/android/widget/EditText.html
you will get the clear idea.
I have many customview in the scrollview. I have implemented swipe feature on the the customview . When I swipe customview to left , scrollview also move vertical. I don't want scroll view to be moved vertical while swiping. I thing Action_Cancel event of touch is going to scrollview which I have to stop. I tried to stop it but it is not working .
Below is the
package com.AA.KK.customui;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GestureDetectorCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.AA.ax4mobile.AAPreferences;
import com.AA.ax4mobile.Constants;
import com.AA.ax4mobile.Session;
import com.AA.ax4mobile.Utils;
import com.AA.ax4mobile.network.Network;
import com.AA.ax4mobile.network.NetworkResponseListener;
import com.AA.KK.R;
import com.AA.KK.activities.KKBaseActivity;
import com.AA.KK.activities.PDFViewerActivity;
public class MailLayout extends RelativeLayout implements OnClickListener, OnTouchListener,
GestureDetector.OnGestureListener {
private CheckBox checkBox;
private TextView title;
private TextView fileSize;
private ImageView fileIcon;
private ImageView tray;
private String url;
private String fileId;
private ViewGroup swipeView;
private boolean isPreview;
private boolean isSelected = true;
private GestureDetectorCompat mDetector;
private static final int SWIPE_OFFSET = 20;
private static final int ANIMATION_OFFSET= 30;
public MailLayout(Context context) {
super(context);
init(context);
// TODO Auto-generated constructor stub
}
#SuppressLint("NewApi")
private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.activity_mail_layout, this);
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
setLayoutParams(linLayoutParam);
swipeView = (RelativeLayout) findViewById(R.id.swipe);
swipeView.setOnTouchListener(this);
title = (TextView) findViewById(R.id.file_name);
fileIcon = (ImageView) findViewById(R.id.file_image);
fileSize = (TextView) findViewById(R.id.file_size);
tray = (ImageView) findViewById(R.id.clickme);
tray.setOnClickListener(this);
checkBox = (CheckBox) findViewById(R.id.chk_checkBox);
mDetector = new GestureDetectorCompat(Utils.getInstance().getContext(), this);
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public boolean isPreview() {
return isPreview;
}
public void setPreview(boolean isPreview) {
this.isPreview = isPreview;
}
public ViewGroup getSwipeView() {
return swipeView;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public void setTitleText(String text) {
title.setText(text);
}
public String getTitleText() {
return (String) title.getText();
}
public void setFileSize(String text) {
fileSize.setText(text);
}
public void setChecked(boolean checked) {
checkBox.setChecked(checked);
}
public boolean getChecked() {
return checkBox.isChecked();
}
public void setFileIcon(String fileName) {
if (fileName.contains(".pdf") || fileName.contains(".PDF")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_pdf_selected);
} else if (fileName.contains(".docx") || fileName.contains(".DOCX")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_docx_selected);
} else if (fileName.contains(".doc") || fileName.contains(".DOC")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_doc_selected);
} else if (fileName.contains(".ppt") || fileName.contains(".PPT")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_ppt_selected);
} else if (fileName.contains(".pptx") || fileName.contains(".PPTX")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_pptx_selected);
} else if (fileName.contains(".png") || fileName.contains(".PNG")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_png_selected);
} else if (fileName.contains(".gif") || fileName.contains(".GIF")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_gif_selected);
} else if (fileName.contains(".csv") || fileName.contains(".CSV")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_csv_selected);
} else if (fileName.contains(".bmp") || fileName.contains(".BMP")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_bmp_selected);
} else if (fileName.contains(".txt") || fileName.contains(".TXT")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_txt_selected);
} else if (fileName.contains(".jpg") || fileName.contains(".jpeg") || fileName.contains(".JPG")
|| fileName.contains(".JPEG")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_jpg_selected);
} else if (fileName.contains(".xls") || fileName.contains(".XLS")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_xls_selected);
} else if (fileName.contains(".xlsx") || fileName.contains(".XLXS")) {
fileIcon.setBackgroundResource(R.drawable.ic_file_xlsx_selected);
} else {
fileIcon.setBackgroundResource(R.drawable.ic_file_selected);
}
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) {
checkBox.setOnCheckedChangeListener(listener);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.clickme:
doAddToTray(getFileId());
break;
}
}
public void setFileId(String fileId) {
this.fileId = fileId;
// TODO Auto-generated method stub
}
public String getFileId() {
return fileId;
}
public static void dismissProgress() {
Message msg = new Message();
msg.what = Constants.UIMSG_HIDE_PROGRESSDIALOG;
Bundle data = new Bundle();
data.putStringArray("caller", Utils.getInstance().getStackTraceArray());
msg.setData(data);
Session.getInstance().getMsgHandler().sendMessage(msg);
}
public static void showProgress() {
String message = Utils.getInstance().getContext().getString(R.string.loading);
Message msg = new Message();
msg.what = Constants.UIMSG_SHOW_PROGRESSDIALOG;
Bundle data = new Bundle();
data.putString("message", message);
data.putStringArray("caller", Utils.getInstance().getStackTraceArray());
msg.setData(data);
Session.getInstance().getMsgHandler().sendMessage(msg);
}
public static void doAddToTray(final String ids) {
AAPreferences settings = AAPreferences.getPreferences(Utils.getInstance().getContext());
String accToken = settings.getString(Constants.REQUEST_KEY_ACCESS_TOKEN, null);
String servername = settings.getString(Constants.REQUEST_KEY_SERVERNAME, null);
JSONObject req = new JSONObject();
Network net = Network.getInstance();
try {
req.accumulate(Constants.REQUEST_KEY_ACCESS_TOKEN, accToken);
req.accumulate(Constants.REQUEST_KEY_SERVERNAME, servername);
req.accumulate(Constants.REQUEST_KEY_ID, ids);
req.accumulate(Constants.REQUEST_KEY_PARENT_ID, "");
showProgress();
NetworkResponseListener listener = new NetworkResponseListener() {
#Override
public void handleSuccessResponse(List<NameValuePair> request, JSONObject response, boolean cancelflag) {
dismissProgress();
Toast.makeText(Utils.getInstance().getContext(), R.string.file_added_to_move_tray,
Toast.LENGTH_LONG).show();
String count = null;
try {
JSONObject json = response.getJSONObject("payload");
count = json.getString("total_count");
if (!TextUtils.isEmpty(count))
Utils.getInstance().saveTrayCount(count);
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(Constants.ACTION_UPDATE_TRAY);
LocalBroadcastManager.getInstance(Utils.getInstance().getContext()).sendBroadcast(intent);
}
#Override
public void handleErrorResponse(List<NameValuePair> request, JSONObject response, boolean cancelflag) {
dismissProgress();
String count = null;
try {
JSONObject json = response.getJSONObject("payload");
count = json.getString("total_count");
Utils.getInstance().saveTrayCount(count);
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(Constants.ACTION_UPDATE_TRAY);
LocalBroadcastManager.getInstance(Utils.getInstance().getContext()).sendBroadcast(intent);
Message msg = Network.defaultErrorManager(request, response);
if (msg != null) {
KKBaseActivity.defaultUIHandler.sendMessage(msg);
}
}
};
net.linkTray(req, listener);
} catch (JSONException e) {
e.printStackTrace();
}
};
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// TODO Auto-generated method stub
Log.e("onFling", "event"+e1.getAction());
if (swipeView.getX() > -(tray.getWidth() + SWIPE_OFFSET)) {
swipeView.animate().translationX(0).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
} else {
if (isPreview) {
swipeView.animate().translationX(0).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
} else {
swipeView.animate().translationX(-(tray.getWidth() + SWIPE_OFFSET)).alpha(1).setDuration(ANIMATION_OFFSET).setListener(null);
}
}
return false;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
if (isSelected) {
if (swipeView.getX() <= 0)
swipeView.setX(e2.getX() - e1.getX() + swipeView.getX());
}
return true;
}
#Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
String s = getUrl();
if (getUrl() == "" || getUrl() == null || getUrl().equalsIgnoreCase("null")) {
Toast.makeText(Utils.getInstance().getContext(), "File is not viewable", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Utils.getInstance().getContext(), PDFViewerActivity.class);
intent.putExtra("filename", getTitleText());
intent.putExtra("view_url", getUrl());
intent.putExtra("preview_from_notification", true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Utils.getInstance().getContext().startActivity(intent);
}
return true;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.e("onTouch", "event"+event.getAction());
boolean flag;
if (isPreview) {
tray.setVisibility(View.INVISIBLE);
} else {
tray.setVisibility(View.VISIBLE);
}
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
flag = onFling(event, event, 0f, 0f);
} else {
flag = mDetector.onTouchEvent(event);
}
return flag;
}
/* #Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.e("onTouchEvent", "event"+event.getAction());
return super.onTouchEvent(event);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev){
Log.e("dispatchTouchEvent", "event"+ev.getAction());
if(ev.getAction()== 3)
return false;
else
return super.dispatchTouchEvent(ev);
}*/
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.e("onInterceptTouchEvent", "event"+ev.getAction());
return true;
}
}
in your onScroll()
put this -
ViewParent parent = getParent();
if(parent!= null){
parent.requestDisallowOnInterceptTouchEvent(true);
}
I have two buttons that increment and decrement a value by one with each press and they're working just fine with the onClickListener. I see that an onLongClickListener exists, which I assume is for touch and hold events. How would I have the number rapidly increment/decrement if the button is held?
Am I correct in assuming that onLongClickListener only fires once per long click? Is there a more appropriate listener or a property somewhere that I'm not aware of?
You may implement it as in the following code.
package org.me.rapidchange;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends Activity implements OnKeyListener,
OnTouchListener, OnClickListener {
private class UpdateCounterTask implements Runnable {
private boolean mInc;
public UpdateCounterTask(boolean inc) {
mInc = inc;
}
public void run() {
if (mInc) {
mHandler.sendEmptyMessage(MSG_INC);
} else {
mHandler.sendEmptyMessage(MSG_DEC);
}
}
}
private static final int MSG_INC = 0;
private static final int MSG_DEC = 1;
private Button mIncButton;
private Button mDecButton;
private TextView mText;
private int mCounter;
private Handler mHandler;
private ScheduledExecutorService mUpdater;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INC:
inc();
return;
case MSG_DEC:
dec();
return;
}
super.handleMessage(msg);
}
};
mIncButton = (Button) findViewById(R.id.inc_button);
mDecButton = (Button) findViewById(R.id.dec_button);
mText = (TextView) findViewById(R.id.text);
mIncButton.setOnTouchListener(this);
mIncButton.setOnKeyListener(this);
mIncButton.setOnClickListener(this);
mDecButton.setOnTouchListener(this);
mDecButton.setOnKeyListener(this);
mDecButton.setOnClickListener(this);
}
private void inc() {
mCounter++;
mText.setText(Integer.toString(mCounter));
}
private void dec() {
mCounter--;
mText.setText(Integer.toString(mCounter));
}
private void startUpdating(boolean inc) {
if (mUpdater != null) {
Log.e(getClass().getSimpleName(), "Another executor is still active");
return;
}
mUpdater = Executors.newSingleThreadScheduledExecutor();
mUpdater.scheduleAtFixedRate(new UpdateCounterTask(inc), 200, 200,
TimeUnit.MILLISECONDS);
}
private void stopUpdating() {
mUpdater.shutdownNow();
mUpdater = null;
}
public void onClick(View v) {
if (mUpdater == null) {
if (v == mIncButton) {
inc();
} else {
dec();
}
}
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER;
boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN
&& event.getAction() != KeyEvent.ACTION_MULTIPLE;
if (isKeyOfInterest && isReleased) {
stopUpdating();
} else if (isKeyOfInterest && isPressed) {
startUpdating(v == mIncButton);
}
return false;
}
public boolean onTouch(View v, MotionEvent event) {
boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
if (isReleased) {
stopUpdating();
} else if (isPressed) {
startUpdating(v == mIncButton);
}
return false;
}
}
I had this same goal and ended up using an OnLongClick to catch the down portion to start a repeating event via a handler, then the normal OnClick to catch the release and stop it. Works beautifully for me.
mOngoingRunnable = new Runnable() {
public void run() {
// do stuff
mHandler.postDelayed(mOngoingRunnable, delayMsecs);
}
};
public boolean onLongClick(View view) {
mHandler.post(mOngoingRunnable);
mOngoing = true;
return false;
}
public void onClick(View view) {
if (mOngoing) {
mHandler.removeCallbacks(mOngoingRunnable);
mOngoing = false;
}
}
I have a ListView which is updated by some service sending intent. If update event arrives while I'm pressing some item in the ListView I get some weird behavior. The default orange rectangle in the pressed item disappears and some other item(s)'s text becomes darker (as if its item is being pressed).
How do I postpone ListView update after it becomes "not pressed"? Or more specifically which events should I listen to in order to determine that ListView is no longer pressed? (I can create some thread executed periodically to update when it's appropriate but I think it's overkill). Or maybe there are better solution or workaround.
Here is sample code illustrating the problem. Service sends update intents every 2 seconds. If I try to long press some item in the list I get the weird behavior I described above.
The activity:
package org.me.listviewupdate;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ArrayAdapter;
public class MyActivity extends ListActivity {
private class MyHandler extends Handler {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE_DATA:
mAdapter.notifyDataSetChanged();
break;
default:
super.handleMessage(msg);
break;
}
}
}
private class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
mHandler.sendEmptyMessage(MSG_UPDATE_DATA);
}
}
private static final int MSG_UPDATE_DATA = 0;
private String[] mItems = new String[] { "Item1", "Item2", "Item3", "Item4" };
private ArrayAdapter<String> mAdapter;
private Handler mHandler;
private BroadcastReceiver mBroadcastReceiver;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mHandler = new MyHandler();
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, mItems);
setListAdapter(mAdapter);
mBroadcastReceiver = new MyBroadcastReceiver();
registerReceiver(mBroadcastReceiver, new IntentFilter(MyService.UPDATE_EVENT));
startService(new Intent(this, MyService.class));
}
#Override
protected void onDestroy() {
unregisterReceiver(mBroadcastReceiver);
stopService(new Intent(this, MyService.class));
super.onDestroy();
}
}
The service:
package org.me.listviewupdate;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MyService extends Service {
private class MyUpdateTask implements Runnable {
public void run() {
sendBroadcast(new Intent(UPDATE_EVENT));
}
}
public static final String UPDATE_EVENT =
"org.me.listviewupdate.intent.event.UPDATED";
private static final int UPDATE_INTERVAL = 2;
private ScheduledExecutorService mUpdater;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mUpdater = Executors.newSingleThreadScheduledExecutor();
}
#Override
public void onDestroy() {
mUpdater.shutdownNow();
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
mUpdater.scheduleAtFixedRate(new MyUpdateTask(), UPDATE_INTERVAL,
UPDATE_INTERVAL, TimeUnit.SECONDS);
}
}
Thank you.
My solution. Well, just in case it may help somebody here is my solution. The code added to MyActivity.onCreate():
getListView().setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
boolean isReleased = event.getAction() == MotionEvent.ACTION_UP
|| event.getAction() == MotionEvent.ACTION_CANCEL;
if (mHasPendingUpdate && isReleased) {
mHandler.sendEmptyMessage(MSG_UPDATE_DATA);
}
return false;
}
});
getListView().setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER;
boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
if (mHasPendingUpdate && isKeyOfInterest && isReleased) {
mHandler.sendEmptyMessage(MSG_UPDATE_DATA);
}
return false;
}
});
Also I added a variable mHasPendingUpdate and modified MyHandler:
private class MyHandler extends Handler {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE_DATA:
if (getListView().isPressed()) {
mHasPendingUpdate = true;
} else {
mAdapter.notifyDataSetChanged();
mHasPendingUpdate = false;
}
break;
default:
super.handleMessage(msg);
break;
}
}
}
Catch the longpress and prevent the dataset from being updated. Override the listview's onlongpress method. I'll post code in a minute
Update:
Actually. That might not work since it could update while you're pressing but before the longpress method is called. If what I suggested above doesn't work, I would implement onClickListener on your ListActivity, listen for any motion event, set the global variable preventing the update, and return false in the onMotionEvent() method so that the list item can consume the click.