How to enable the sound effect on touching a button? - java

If I implement the OnClickListener, the sound effect will work on clicking the button. While on implementing the OnTouchListener the sound effect doesn't work on touching it. So how to enable the sound effect on implementing the OnTouchListener?
EDIT:
Here is the code if I use clicking approach:
public class CalculatorActivity extends Activity implements OnClickListener
{
//...
private Button btn1;
private EditText edLCD;
//...
public void onCreate(Bundle savedInstanceState)
{
//...
btn1 = (Button) findViewById(R.id.d1);
btn1.setOnClickListener(this);
edLCD = (EditText) findViewById(R.id.edLCD);
}
//...
public void onClick(View v)
{
edLCD.setText(edLCD.getText().toString() + "1");
}
}
And here is the code if I use touching approach:
public class CalculatorActivity extends Activity implements OnTouchListener
{
//...
private Button btn1;
private EditText edLCD;
//...
public void onCreate(Bundle savedInstanceState)
{
//...
btn1 = (Button) findViewById(R.id.d1);
btn1.setOnTouchListener(this);
edLCD = (EditText) findViewById(R.id.edLCD);
}
//...
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
edLCD.setText(edLCD.getText().toString() + "1");
}
return true;
}
}

Try...
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
v.playSoundEffect(SoundEffectConstants.CLICK);
edLCD.setText(edLCD.getText().toString() + "1");
}
return true;
}

Related

Hold the button for 2 seconds and then show an AlertDialog

I have a button and when I hold the button for 2 seconds, I want to show an AlertDialog.
This is what I have tried:
btn.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Helper.setTimeout(() -> {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Message");
builder.setMessage(text);
builder.show();
System.out.println("I am text");
}
}, 2000);
return super.onTouch(view, motionEvent);
}
}
My method setTimeout():
public static void setTimeout(Runnable runnable, int delay){
new Handler(Looper.getMainLooper()).postDelayed(runnable, delay);
}
My problem is that sometimes the AlertDialog shows up multiple times and I always get this warning:
W/InputEventReceiver: Attempted to finish an input event but the input
event receiver has already been disposed.
What am I doing wrong?
I don't know if there is a better solution. I have also tried It with
btn.setOnLongClickListener(v -> {
System.out.println("hold");
return true;
});
but that doesn't output anything.
You can try this
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Message");
builder.setMessage("hello");
builder.show();
}
}, 2000);
return false;
}
});
}
}

How to call a method of MainActivity by a class that extends Button

I want to call a method showMessage() which is defined in my main activity from a TestButton class that extend Button. The code I use below throw a null pointer exception. I am not sure how to solve this. Can you please help me?
MainActivity
public class MainActivity extends MultiTouchActivity {
TestButton btn;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (TestButton)findViewById(R.id.button4);
btn.setOnTouchListener(this);
}
public void showMessage()
{
Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
}
TestButton class
public class TestButton extends Button {
MainActivity m;
public TestButton(final Context context, final AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
public boolean onTouchEvent(final MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP)
{
m = new MultitouchtestActivity();
m.showMessage();
}
return super.onTouchEvent(event);
}
}
EDIT
Thank you #ΦXoce 웃 Пepeúpa this solve my issue but I made a little bit changes.
btn.setCallback(this); from btn.setCallback();
and
public void setCallback(final ICallback iCallback)
2 things:
m = new MultitouchtestActivity(); is not the way ANDROID wants you to create / instantiate Activities.. so you have more chances to break your code than doing something right by calling the constructor directly..
On the other hand trying to call a method from the Activity inside of a button class is forcing you to make circle-dependencies... now your Activity needs a button Object and your button needs an Activity Object..(not a good approach)
How to solve it
Implement a callback like doing:
interface ICallback{
void showMessage();
}
public class MainActivity extends MultiTouchActivity implements ICallback{
TestButton btn;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (TestButton)findViewById(R.id.button4);
btn.setOnTouchListener(this);
//register here the callback
btn.setCallback(this);
}
#Override
public void showMessage()
{
Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
}
public class TestButton extends Button {
MainActivity m;
ICallback iCallback;
public setCallback(final ICallback iCallback)
{
this.iCallback = iCallback;
}
public TestButton(final Context context, final AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
public boolean onTouchEvent(final MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP)
{
iCallback.showMessage();
}
return super.onTouchEvent(event);
}
}

how to get scroll mouse event inside the button in android

after clicking the button mouse scroll will be activated and scroll up, button value should be increment and when scroll down value decrement. but button outside code working,then button inside code not work.my sample code here.
public class MainActivity extends Activity {
Button button;
int x,f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
// i need implement code here ,how can i do?
return false;
}
});
}
#SuppressLint("NewApi") #Override
public boolean onGenericMotionEvent(MotionEvent event) {
//if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:
if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
//selectNext();
{
x=Integer.parseInt(button.getText().toString());
f=x+5;
button.setText(""+f);
}
else
{
x=Integer.parseInt(button.getText().toString());
f=x-5;
button.setText(""+f);
return true;
}
}
return super.onGenericMotionEvent(event);
} }
i need those function inside the touch listener i don't know how can i do,please help me!

Android image fade animation

When I'd ran this code, it did nothing, I'm sure that this event is called when I touch the button. but It doesn't change the opacity of imageView.
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f);
fadeAltAnim.start();
}
};
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
Is there anything wrong with my code?
Try this code, its using ViewPropertyAnimator :
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.animate().alpha(0.2f).setDuration(1000);
}
};
Its always good to set a duration, so the Animation knows how long its supposed to run.
EDIT : You might want to attach it to a MotionEvent if you are using onTouchListener , something like :
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent == MotionEvent.ACTION_DOWN)
view.animate().alpha(0.2f).setDuration(1000);
}
};
EDIT 2 :
If you want to use a Button its best to use an OnClickListener instead of onTouchListener, if you want to attach an Image you have to initiate it(for example in an ImageView) :
Button button = (Button) findViewById(R.id.your_button);
ImageView imageView = (ImageView) findViewById(R.id.imgTest);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.animate().alpha(0.2f).setDuration(1000);
}
};
It's because you're passing in an id when the method requires the View. Try passing in the View.
So basically change ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f); to ObjectAnimator.ofFloat(view, "alpha", 0.2f);
Here's an example
findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(v, "alpha", 0.2f);
fadeAltAnim.start();
}
});
Is there any reason you're using ObjectAnimator? Can you do:
AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);
**try this**
public class Animationtest extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
imageView = (ImageView)findViewById(R.id.text);
setAnimation();
}
private void setAnimation(){
imageView.setOnTouchListener(new OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "touch", Toast.LENGTH_LONG).show();
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(imageView, "alpha", 0.2f);
fadeAltAnim.start();
return false;
}
});
}
}

Android: Broken menu button when text used

I have defined a standard onCreateOptionsMenu. The menu button works fine when my EditText box is empty. But when the EditText box has data in it, the menu button doesnt work. Any clue? Please help, i have no clue how to solve this. Thanks!
public class TipCalc extends Activity {
private EditText total;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
total = (EditText)findViewById(R.id.EditText01);
total.setOnKeyListener(mKeyListener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
private OnKeyListener mKeyListener = new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if((v.getId() == R.id.EditText01
&& (total.getText().toString().length() > 0) {
calculate();
return true;
}
}
return false;
}
}
I dont know if that would be correct but create setOnClickListener rather than OnKeyListener
something like this
TextView total = (TextView) findViewById(R.id.EditText01);
total.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//do some stuff
}
});

Categories

Resources