How to implement "reverse click" event with "buttondown" as normal state? - android

My Android app needs a special button whose normal state is MouseDown. The Click event is fired when MouseUp event is fired.
So this button is actually opposite of normal button
It doesn't needs a DoubleClick event. Tried search all over the web; couldn't find anything.

Here is how I implemented the "ReverseButton":
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
public class CustomButton extends Button {
private CustomButtonListener mListener;
private boolean mPressedState;
public CustomButton(Context context) {
super(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPressedState = true;
break;
case MotionEvent.ACTION_UP:
if(mPressedState) {
mListener.onRelease(event);
}
mPressedState = false;
break;
}
return super.onTouchEvent(event);
}
public void setOnReleaseListener(CustomButtonListener listener) {
mListener = listener;
}
public interface CustomButtonListener{
void onRelease(MotionEvent event);
}
}

Related

Hide a Preference-item depending on the state (enabled/disabled) of another Preference in PreferenceScreen

I'm creating a settings screen, and I have a few settings items:
SwitchPreferenceCompat and my CustomPreference.
I need the СustomPreference to disappear(hide) when the SwitchPreferenceCompat is turned off and appear(show) when the SwitchPreferenceCompat is turned on.
Out of the box, only the "dependency" mechanism is available by specifying the "dependency" attribute for some Preferences, which only disables/enables items, and I need to change visibility.
Is there a way to achieve this?
create customPreference and override onDependencyChanged().
package com.paul.ttcapp.p9988030.helper.ui;
import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
public class PaulEditTextPreference extends EditTextPreference {
public PaulEditTextPreference(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public PaulEditTextPreference(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public PaulEditTextPreference(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public PaulEditTextPreference(#NonNull Context context) {
super(context);
}
#Override
public void onDependencyChanged(#NonNull Preference dependency, boolean disableDependent) {
// If the corresponding dependency is disabled, disable (hide) this setting as well.
setVisible(!disableDependent);
super.onDependencyChanged(dependency, disableDependent);
}
}

Getting null pointer exception virtual method 'long android.graphics.Paint.getNativeInstance()' on a null object reference

I am trying to create a todo list app. I am adding entered text to the listitems on the enter, it's working fine without the style.
After adding style with paint and drawing its giving null pointer exception. I am initializing all the drawing and paint objects in the init method of "ToDoListItemView". I tried with "log.i" but it's not entering into ToDoListItemView's init method.
Here is my code.
MainActivity:
package todo.prac.com.todolist;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylistView=(ListView)findViewById(R.id.myListView);
final EditText editText=(EditText)findViewById(R.id.myEditText);
final ArrayList<String>todoItems=new ArrayList<String>();
final ArrayAdapter<String>adapter;
adapter=new ArrayAdapter<String>(this,R.layout.todolist_view,todoItems);
mylistView.setAdapter(adapter);
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(keyEvent.getAction()==KeyEvent.ACTION_DOWN){
if(i==KeyEvent.KEYCODE_ENTER){
todoItems.add(0,editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
return true;
}
}
return false;
}
});
}
}
and my ToDoListItemView
package todo.prac.com.todolist;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class ToDoListItemView extends TextView {
private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;
public ToDoListItemView(Context context) {
super(context);
}
public ToDoListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
private void init(){
Log.i("init","inside");
Resources myResources=getResources();
marginPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
linePaint.setColor(myResources.getColor(R.color.notepad_line));
paperColor=myResources.getColor(R.color.notepad_paper);
margin=myResources.getDimension(R.dimen.notepad_margin);
}
#Override
public void onDraw(Canvas canvas){
canvas.drawColor(paperColor);
if(linePaint==null){
Log.i("linepaint","is null");
}
canvas.drawLine(0,0,getMeasuredHeight(),0,linePaint);
canvas.drawLine(0,getMeasuredHeight(),getMeasuredWidth(),getMeasuredHeight(),linePaint);
canvas.drawLine(margin,0,margin,getMeasuredHeight(),marginPaint);
canvas.save();
canvas.translate(margin,0);
super.onDraw(canvas);
canvas.restore();
}
}
and my TodoList layout xml:
<?xml version="1.0" encoding="utf-8"?>
<todo.prac.com.todolist.ToDoListItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="#color/notepad_text"
android:fadingEdge="vertical"
/>
You are not calling the init() method inside the constructors of your class.
public ToDoListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
I solve this problem by this method.
public ToDoListItemView(Context context) {
this(context,null);
}
public ToDoListItemView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// initiate your some things
}
Pay attention to: only one super. others in place of this .

Make uneditable EditText respond to first click

I'm trying to make a popup menu appear when the user clicks on an EditText but I don't want the EditText itself to be editable. I've tried many things like setting its KeyListener to null, setting it's InputType to null, but what always happens is that the first click gives the View focus and the second click actually registers with my OnClickListener. So user has to click twice to get the menu to popup. Any ideas?
public class PopupEditText extends EditText implements OnClickListener
{
private PopupMenu mMenu;
private Context mContext;
public PopupEditText(Context context)
{
super(context);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public PopupEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
mContext = context;
setKeyListener(null);
mMenu = new PopupMenu(context, this);
}
#Override
public void onClick(View view)
{
mMenu.show();
}
You're not far off. I'd recommend just overriding onTouchEvent() directly, and responding only to ACTION_UP events. Optionally, force it to be disabled and non-focusable. For example:
public class UnmodifiableEditText extends EditText {
private PopupMenu mPopupMenu;
public UnmodifiableEditText(Context context) {
super(context);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public UnmodifiableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context ctx) {
super.setEnabled(false);
super.setFocusable(false);
mPopupMenu = new PopupMenu(ctx, this);
}
#Override public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(getContext(), "Showing Popup", Toast.LENGTH_SHORT).show();
mPopupMenu.show();
}
return true;
}
#Override public void setEnabled(boolean enabled) {
// Do not allow enabling the EditText
}
#Override public void setFocusable(boolean focusable) {
// Do not allow focusability changes
}
}

Detect end of fling on ScrollView

I've overridden ScrollView to pass MotionEvents to a GestureDetector to detect fling events on the ScrollView. I need to be able to detect when the scrolling stops. This doesn't coincide with the MotionEvent.ACTION_UP event because this usually happens at the start of a fling gesture, which is followed by a flurry of onScrollChanged() calls on the ScrollView.
So basically what we are dealing with here is the following events:
onFling
onScrollChanged, onScrollChanged, onScrollChanged, ... , onScrollChanged
There's no callback for when the onScrollChanged events are done firing. I was thinking of posting a message to the event queue using a Handler during onFling and waiting for the Runnable to execute to signal the end of the fling, unfortunately it fires after the first onScrollChanged call.
Any other ideas?
I've combined a few of the answers from here to construct a working listener that resembles the way AbsListView does it. It's essentially what you describe, and it works well in my testing.
Note: you can simply override ScrollView.fling(int velocityY) rather than use your own GestureDetector.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
private static final int DELAY_MILLIS = 100;
public interface OnFlingListener {
public void onFlingStarted();
public void onFlingStopped();
}
private OnFlingListener mFlingListener;
private Runnable mScrollChecker;
private int mPreviousPosition;
public CustomScrollView(Context context) {
this(context, null, 0);
}
public CustomScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScrollChecker = new Runnable() {
#Override
public void run() {
int position = getScrollY();
if (mPreviousPosition - position == 0) {
mFlingListener.onFlingStopped();
removeCallbacks(mScrollChecker);
} else {
mPreviousPosition = getScrollY();
postDelayed(mScrollChecker, DELAY_MILLIS);
}
}
};
}
#Override
public void fling(int velocityY) {
super.fling(velocityY);
if (mFlingListener != null) {
mFlingListener.onFlingStarted();
post(mScrollChecker);
}
}
public OnFlingListener getOnFlingListener() {
return mFlingListener;
}
public void setOnFlingListener(OnFlingListener mOnFlingListener) {
this.mFlingListener = mOnFlingListener;
}
}
Thank you #PaulBurke +1
Xamarin Solution
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using System;
public class CustomScrollView : ScrollView
{
public event EventHandler FlingEnded;
public event EventHandler FlingStarted;
private Action ScrollChecker;
private int PreviousPosition;
private const int DELAY_MILLIS = 100;
public CustomScrollView(Context context) : base(context) => Init();
public CustomScrollView(Context context, IAttributeSet attrs) : base(context, attrs) => Init();
public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) => Init();
public CustomScrollView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) => Init();
public CustomScrollView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }
private void Init()
{
ScrollChecker = () =>
{
int position = ScrollY;
if (PreviousPosition - position == 0)
{
FlingEnded?.Invoke(this, new EventArgs());
RemoveCallbacks(ScrollChecker);
}
else
{
PreviousPosition = ScrollY;
PostDelayed(ScrollChecker, DELAY_MILLIS);
}
};
}
public override void Fling(int velocityY)
{
base.Fling(velocityY);
FlingStarted?.Invoke(this, new EventArgs());
Post(ScrollChecker);
}
}
Usage:
myCustomScrollView.FlingEnded += myCustomScrollView_FlingEnded;
protected void myCustomScrollView_FlingEnded(object sender, EventArgs e) =>
{
//Do onFlingEnded code here
};

Subclass of EditText isn't behaving like EditText

I've written a subclass of EditText. Here is that subclass:
package com.package.foo;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
public class FuturaEditText extends EditText{
public FuturaEditText(Context context) {
this(context, null, 0);
}
public FuturaEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FuturaEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
}
yet it doesn't behave like an EditText, but a TextView. In particular, the soft keyboard isn't rising on focus and it has no EditText formatting. Why?
For completeness - from the source:
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
So, if the constructor is called with no default style, it defaults to editTextStyle
This implementation, with each constructor calling the matched super constructor:
package com.package.foo;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
public class FuturaEditText extends EditText{
public FuturaEditText(Context context) {
super(context);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
public FuturaEditText(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
public FuturaEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
}
works. I can only assume that EditText(context, attrs, 0) is not the same as EditText(context, attrs).
You should not invoke the super constructor completing the missing arguments, otherwise there's no way for a client to invoke a constructor with less than 3 arguments (i.e. super.(Context context, AttributeSet attrs)).
The second version is the correct one.

Categories

Resources