I want to disable long press on keys on my custom keyboard.for implementing customkeyboard i extended View class.like below but there is no method to handle longpress
public class CustomKeyboard extends View {
#Override
public void onPress(int arg0) {
Log.d("Gajanand", "onPress:GK ");
}
#Override
public void onRelease(int primaryCode) {
Log.d("Gajanand", "onRelease: GK");
}
#Override
public void onText(CharSequence text) {
}
#Override
public void swipeDown() {
}
#Override
public void swipeLeft() {
}
#Override
public void swipeRight() {
}
#Override
public void swipeUp() {
}
}
help me to handle on long press. i want to disable long press completely.
You can use PopupWindow class and populate it with custom layout.
PopupWindow popup = new PopupWindow(context);
popup.setContentView(custom);
On click in the popup you can dismiss it.
#Override
public void onRelease(int primaryCode) {
Log.d("Gajanand", "onRelease: GK");
popup.dismiss();
}
Happy coding!!
Related
During the Rewarded video play i want to disable the back button and set a minimum waiting time . is it possible to do?On which function should i change to complete my desired functionality?
here is my code
if (mAd.isLoaded()) {
mAd.show();
} else {
startActivity(new Intent(EssayActivityQstnShow2.this, Essay_Answer_Show.class));
}
and here is Rewarded video ads handling methods
private void loadRewardedVideoAds() {
if (!mAd.isLoaded()) {
mAd.loadAd(getResources().getString(R.string.rewardedvideoid), new AdRequest.Builder().build());
}
}
#Override
public void onRewardedVideoAdLoaded() {
}
#Override
public void onRewardedVideoAdOpened() {
// onBackPressed();
}
#Override
public void onRewardedVideoStarted() {
// onBackPressed();
}
#Override
public void onRewardedVideoAdClosed() {
startActivity(new Intent(EssayActivityQstnShow2.this, Essay_Answer_Show.class));
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
loadRewardedVideoAds();
}
#Override
public void onRewardedVideoCompleted() {
startActivity(new Intent(EssayActivityQstnShow2.this, Essay_Answer_Show.class));
}
Try below code
private boolean isVideoPlaying;
#Override
public void onRewardedVideoStarted() {
isVideoPlaying = true;
}
#Override
public void onRewardedVideoAdClosed() {
isVideoPlaying = false;
}
and than onBackPress() check, if the video is playing or not
#Override
public void onBackPressed() {
if (!isVideoPlaying)
super.onBackPressed();
}
Before implementing this, please read the policies carefully.
You must override the onBackPressed callback in activity and remove line super.onBackPressed(). then if the user taps the back button the activity will not close.
If you have no access to the Essay_Answer_Show activity check can you create another activity which extends that or clone library and tries to modify activity?
I wonder if this is possible to do in AppCompatActivity. I want to let top most Fragment do some action before AppCompatActivity super.onBackPressed(); kill Fragment
HereĀ“s the mockup code which speaks for itself:
#Override
public void onBackPressed() {
ToppMosteFragment.doSomeActionBeforeBackPressedKillYou();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// this is not working..
super.onBackPressed();
}
}, 1000);
}
You can try to make doSomeActionBeforeBackPressedKillYou() return a boolean
and do
#Override
public void onBackPressed() {
if(ToppMosteFragment.doSomeActionBeforeBackPressedKillYou()) {
super.onBackPressed();
}
}
EDIT:
If you defined the animation , you can add a listener :
Animation animation = actionButton.getHideAnimation();
animation.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
listener.onFabAnimationEnd();
}
});
actionButton.setHideAnimation(animation);
listener is a interface that is implement by your activity class
And in you activity
#Override
public void onFabAnimationEnd() {
super.onBackPressed();
}
I got an Idea,
This could be an universal thing -
Create BaseFragment having the method like below -
public class BaseFragment extends Fragment {
public boolean onFragmentBackPressed() {
return true;
}
}
extend your MyFragment to BaseFragment
class MyFragment extends BaseFragment {
// override method
#Override
public boolean onFragmentBackPressed() {
return false;
}
}
Check in your MainActivity that if BaseFragment method onFragmentBackPressed() if returning false or what, if yes - perform some action before exiting
#Override
public void onBackPressed() {
if (MyFragment instance if BaseFragment) {
if (!((BaseFragment) MyFragment).onFragmentBackPressed()) {
// perform and action
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
I am using this library CamView from LivotovLabs to scan barcodes. It works as expected however it does not focus for specific phones so I want to implement a focus on touch and my solution is as below. However its not working,the phone does not focus on the barcode.
camera = (ScannerLiveView) findViewById(R.id.camview);
camera.setFocusable(true);
camera.setFocusableInTouchMode(true);
camera.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
camera.requestFocus();
return true;
}
});
camera.setScannerViewEventListener(new ScannerLiveView.ScannerViewEventListener() {
#Override
public void onScannerStarted(ScannerLiveView scanner) {
}
#Override
public void onScannerStopped(ScannerLiveView scanner) {
}
#Override
public void onScannerError(Throwable err) {
Toast.makeText(ACME_Scanner.this, "Scanner Error: " + err.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCodeScanned(String data) {
Toast.makeText(ACME_Scanner.this, "Barcode No: "+data, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btnFlash).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleFlash();
}
});
findViewById(R.id.btnSkip).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
protected void onResume()
{
super.onResume();
ZXDecoder decoder = new ZXDecoder();
decoder.setScanAreaPercent(0.5);
camera.setDecoder(decoder);
camera.startScanner();
}
#Override
protected void onPause()
{
camera.stopScanner();
super.onPause();
}
public void toggleFlash()
{
try {
flashStatus = !flashStatus;
camera.getCamera().getController().switchFlashlight(flashStatus);
}
catch (NullPointerException e){
e.printStackTrace();
}
}
Android activity has overridden method onUserInteraction.But how can I check the user interaction in the dialog?I want to develop some logic based on this.Please help
This code works (onUserInteraction)fine with Activity
public class MyBaseActivity extends Activity {
public static final long DISCONNECT_TIMEOUT = 300000; // 5 min = 5 * 60 * 1000 ms
private Handler disconnectHandler = new Handler(){
public void handleMessage(Message msg) {
}
};
private Runnable disconnectCallback = new Runnable() {
#Override
public void run() {
// Perform any required operation on disconnect
}
};
public void resetDisconnectTimer(){
disconnectHandler.removeCallbacks(disconnectCallback);
disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}
public void stopDisconnectTimer(){
disconnectHandler.removeCallbacks(disconnectCallback);
}
#Override
public void onUserInteraction(){
resetDisconnectTimer();
}
#Override
public void onResume() {
super.onResume();
resetDisconnectTimer();
}
#Override
public void onStop() {
super.onStop();
stopDisconnectTimer();
}
}
But how can I use 'onUserInteraction' method with Dialog ?
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
final Dialog dialog = new Dialog(MainActivity.this);
//setting custom layout to dialog
dialog.setContentView(R.layout.cusotm_dialog_layout);
dialog.setTitle("Custom Dialog");
//adding text dynamically
TextView txt = (TextView) dialog.findViewById(R.id.textView);
txt.setText("Put your dialog text here.");
ImageView image = (ImageView)dialog.findViewById(R.id.image);
image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));
//adding button click event
Button dismissButton = (Button) dialog.findViewById(R.id.button);
dismissButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
I have implemented onUserInteraction for DialogFragment. This has the same handling as Activity for onUserInteraction. The setUserInteractionEnabled method can also be implemented in an Activity subclass.
public abstract class BaseDialogFragment
extends DialogFragment
{
protected void onUserInteraction()
{
Activity activity = getActivity();
if(activity != null)
{
activity.onUserInteraction();
}
}
protected void setUserInteractionEnabled(Dialog dialog, boolean enabled)
{
if(!enabled)
{
dialog.getWindow().setCallback(dialog);
return;
}
dialog.getWindow().setCallback(new WindowCallbackWrapper(dialog)
{
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
onUserInteraction();
return super.dispatchKeyEvent(event);
}
#Override
public boolean dispatchKeyShortcutEvent(KeyEvent event)
{
onUserInteraction();
return super.dispatchKeyShortcutEvent(event);
}
#Override
public boolean dispatchTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
onUserInteraction();
}
return super.dispatchTouchEvent(event);
}
#Override
public boolean dispatchTrackballEvent(MotionEvent event)
{
onUserInteraction();
return super.dispatchTrackballEvent(event);
}
#Override
public boolean dispatchGenericMotionEvent(MotionEvent event)
{
onUserInteraction();
return super.dispatchGenericMotionEvent(event);
}
});
}
}
Call this in your onCreateDialog.
public class MyDialogFragment
extends BaseDialogFragment
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog dialog = new Dialog(getContext());
setUserInteractionEnabled(dialog, true);
return dialog;
}
}
Note: this will not call onUserInteraction from the dialog's soft keyboard. That should be handled from a TextWatcher that calls onUserInteraction in afterTextChanged.
I have a shutter button which implements it's own listener for handling focus and click events
Here,there are two methods,one for focus and another for click.I find that these methods are not being called...
My custom view extends an ImageView is as follows:
public class ShutterButton extends ImageView {
public ShutterButton(Context context)
{
super(context);
}
public ShutterButton(Context context,AttributeSet attrs)
{
super(context,attrs);
}
public ShutterButton(Context context,AttributeSet attrs,int defStyle)
{
super(context,attrs,defStyle);
}
interface ShutterButtonListener
{
public void onShutterButtonFocus(ShutterButton shutterButton,boolean pressed);
public void onShutterButtonClick(ShutterButton shutterButton);
}
ShutterButtonListener mListener;
boolean mOldPressed;
boolean mTouchEnabled=true;
#Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
if(mTouchEnabled)
return super.dispatchTouchEvent(ev);
else
return false;
}
public void enableTouch(boolean enable)
{
mTouchEnabled=enable;
}
#Override
public void drawableStateChanged()
{
super.drawableStateChanged();
final boolean pressed=isPressed();
/*
* When pressing a physical camera button:
* pressed(true),optional click,pressed(false)
* Another option that we could use is:
* pressed(true),pressed(false),optional click...
* The second set of events occurs when the drawable state gets updated first.
* To emulate a physical camera button,push back pressed(false) in the event queue.
* */
if(pressed!=mOldPressed)//indicates state change
{
if(!pressed)//setting to false once shutter has been clicked(true->false)
{
post(new Runnable(){
#Override
public void run()
{
callShutterButtonFocus(pressed);
}
});
}
else//(false->true)clicking the shutter
callShutterButtonFocus(pressed);
}
mOldPressed=pressed;
}
private void callShutterButtonFocus(boolean pressed)
{
if(mListener!=null)
{
mListener.onShutterButtonFocus(this,pressed);
}
}
public void setShutterButtonListener(ShutterButtonListener listener)
{
mListener=listener;
}
#Override
public boolean performClick()
{
boolean result=super.performClick();
if(mListener!=null &&getVisibility()==View.VISIBLE)
{
mListener.onShutterButtonClick(this);
}
return result;
}
}
In the Activity that uses this ShutterButton:
btn_capture=(com.example.mycameraapp.ShutterButton)findViewById(R.id.btn_capture);
btn_capture.setShutterButtonListener(this);
btn_capture.setVisibility(View.VISIBLE);
#Override
public void onShutterButtonFocus(ShutterButton shutterButton,
boolean pressed) {
// TODO Auto-generated method stub
Log.d(TAG, "Shutter button focus called");
switch(shutterButton.getId())
{
case R.id.btn_capture:
doFocus(pressed);
break;
}
}
#Override
public void onShutterButtonClick(ShutterButton shutterButton) {
// TODO Auto-generated method stub
Log.d(TAG, "Shutter button click called");
switch(shutterButton.getId())
{
case R.id.btn_capture:
doSnap();
break;
}
}
All the code here has been taken from :
ShutterButton
Activity
Try to make clickable=true in xml layout or setClickalbe(true) in your code.
Anyway, using performClick in not a really good way to work with Views in android.