How to check If a view has been touched? - android

I'm using a third party video player in my app. The library provides enough functionalities like scroll video, pause, play video etc. And all those features will auto-hide after a few seconds. I need a few more controls over the player. So I put a toolbar on it ( It consists of the back, favorite, share buttons ). I need to enable this toolbar only If someone clicks on the player or even taps on it. And it must hide after a few seconds like the video player's controller. I tried using this,
youtubePlayerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
LogUtils.LOGE("youtubePlayerView.setOnClick", "called");
return true;
}
return false;
}
});
But in vain. Nothing is happening.
I want to detect If the user has clicked or tapped on the player.

Try Like this
public void onClickHandler(View v)
{
switch (v.getId()) {
case R.id.youtubePlayerView:
//Player Clicked
break;
}
}

This should work:
youtubePlayerView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// enable toolbar here!
}
});

Related

onClickListener for YouTubePlayer

I want to add an onClickListener event for YouTube Player API
I tried many many solutions but all of them didn't work
Some of my attempts
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtubePlayer);
youTubePlayerView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d(TAG,"onClick");
}
});
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtubePlayer);
youTubePlayerView.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Log.d(TAG,"onTouch");
return false;
}
});
Also, I tried to add a layout above the YouTube player view and make it transparent then add a click listener to this layout but the player stops after few seconds every time
And my last attempt was working with GestureDetector class but also didn't work
Thank you in advance
I tried to add a layout above the YouTube player view and make it
transparent then add a click listener to this layout but the player
stops after few seconds
YouTube Player API does not allow any view above it be it a visible or not and will close with an ErrorReason.
It also consumes any and all touch events without sharing them. However you can Override dispatchTouchEvent(MotionEvent) method of the parent ViewGroup to the YouTubePlayerViewand steal back these touch events to kick you own onClick() callback.
What you have to do is, first create a container in the xml layout file where you've placed your YouTubePlayerView.
youtube_player_view_layout.xml
<MyCustomFrameLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
<YouTubePlayerView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</YouTubePlayerView>
</MyCustomFrameLayout>
MyCustomFrameLayout must be added with fully qualified name in layout xml i.e. with [my.class.package]+[ClassName]
MyCustomFrameLayout.java
public class MyCustomFrameLayout extends FrameLayout {
private boolean actionDownReceived = false;
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
actionDownReceived = true;
break;
}
case MotionEvent.ACTION_MOVE: {
// No longer a click, probably a gesture.
actionDownReceived = false;
break;
}
case MotionEvent.ACTION_UP: {
if (actionDownReceived) {
performClick();
}
break;
}
}
return super.dispatchTouchEvent(event);
}
}
and then all you need to do is set click listener on MyCustomFrameLayout.
Disclaimer : The above code is not tested in an IDE and will probably not run correctly if/when copied as is. However the essence
of the thought is properly projected here.

Android How Can I move an ImageView when the user clicks a button (For a game)

I have yet to find the solution to this problem. I'm trying to make a game. I want to make an image move to the left, right, up or down when a button is clicked. Please help me as I have struggled with this problem for two days. Please show and explain to me how to move an ImageVıew as explained above.
Thanks in advance.
bt = (Button) findViewById(R.id.button2);
bt.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
playerY+=20;`
player.setY(playerY);
return true;
}
});
Try this
UpButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do something
return true;
case MotionEvent.ACTION_UP:
// No longer down
return true;
}
return false;
}
});
Action Down means the button is clicked and action up gets triggered when the finger is no longer on the button

How other buttons disable when I press the button?

I have a few imageview which have onclicklistener. If I press one (not release), I can press click others or I can click them same time. I do not want it. Everytime when I press one of them others should be disable to click.
imageview1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getMethod();
}
});
I guess, I tried setClickable(false); but it did not work properly, if I clicked one button after that it worked.
Try using onTouchListener instead of onClickListener and calling setEnabled(false); on the other views there. Here's a fairly basic example:
OnTouchListener onTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
imageView1.setEnabled(false);
imageView2.setEnabled(false);
}
return true;
}
};
And then apply it to the image views with:
imageView1.setOnTouchListener(onTouchListener);
That should work. One thing is, though, that while you'll only be able to push one button no matter what, you also won't be able to push anything after you let go - but, you can fix that by adding some logic to see if the view actually got clicked or if the user touched it, changed their mind and slid away. The (event.getAction() == MotionEvent.ACTION_DOWN) check will be true even if the user is just scrolling.
//button on which press u want to disable others
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2.setEnabled(false); //button which u want to disable
button3.setEnabled(false); //button which u want to disable
}
});
//update fixed a spelling error
try to disable the button and
button.setEnable(false);
enable the button
button1.setEnable(true);

Android button onclick multitouch

I have 2 buttons and I need to read onClick event from second button when first is pressed down now and v.v. Like in keyboards. How to do that?
Edit
No, no! I don't need to check was first button clicked or not. I need to listen another onClick events when first or second button is in ACTION_DOWN state couse if I press first button, I can't press second, but I have multitouch.
May be You could try the following code :
Declare a boolean variable in class.
private boolean button1IsPressed = false;
Write following code for button 1 :
button1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
button1IsPressed=true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
button1IsPressed=false;
}
}
};
For Button 2 You can do the following:
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(button1IsPressed){
//Write your to do code here
}
}
});
You could try with onTouchListeners. In the first button modify a boolean on the down and up event, in the second button, only perform an action when the boolean is true.
If the buttons are toggle buttons, what I think is the case then:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Do something
} else {
// Disable vibrate
}
}
The main thing here is the isChecked() function, which may be used when checking which one is checked, so to execute something then. You can set in the XML of the two buttons the following:
android:onClick="onToggleClicked" then with isChecked determine which one is checked like this:
boolean on1 = ((ToggleButton) view1).isChecked();
boolean on2 = ((ToggleButton) view2).isChecked();
if (on1)
//do something with button2
if (on2)
//do something with button1
Cheers
There's a sample code in android-16/ApiDemos project called "Views -> Splitting Touches across Views" (SplitTouchView.java). In that sample enclosing LinearLayout has an attribute android:splitMotionEvents="true" which allows to scroll two list views simultaneously.
According to Android 3.0 API Overview this attribute appeared in this api version:
Previously, only a single view could accept touch events at one time. Android 3.0 adds support for splitting touch events across views and even windows, so different views can accept simultaneous touch events.

No sound on touch button

If I use:
final Button btnNught = (Button) findViewById(R.id.night);
btnNught.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
btnNught.setPressed(true);
}
});
And if I click on button I listen click sound, but if I use:
btnNught.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (btnNught.isPressed()) {
btnNught.setPressed(false);
return true;
}
else {
btnNught.setPressed(true);
return false;
}
}
});
I do not listen any sound on touch.
Where I made mistake?
Edit:
I want to create button like togglebutton with two state: pressed and non-pressed with two different colors of course (normal-light gray non pressed and green when pressed).
In this case (above), when I touch button I do not listen any sound, but if I use onClick method I listen 'click' sound.
How can I get 'click' or 'touch' sound when I use onTouch method?
Doing this works for me:
btnNught.setSoundEffectsEnabled(true);
btnNught.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()!=MotionEvent.ACTION_UP ||
event.getAction()!=MotionEvent.ACTION_DOWN)
{
return false;
}
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
btnNught.playSoundEffect(SoundEffectConstants.CLICK);
//Set whatever color you want to set
}
else
{
}
return true;
}
});
Please, on what conditions could you get to the btnNught.setPressed(true); in the second example? As I see, you simply can't press the button. On Action Down you consume the action, so button won't be pressed.
Edit: Put a breakpoint at the start of listener and enter it in the different variants of the button use. And check the behaviour.
Try adding android:soundEffectsEnabled="true" to your <Button> tag.
Your question is not very clear over when you want to play the sound but I am answering your question assuming that you wanna play it when the Action_DOWN occurs.If you plan to create a toggle button your code should look something like this
btnNught.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()!=MotionEvent.ACTION_UP ||
event.getAction()!=MotionEvent.ACTION_DOWN)
{
return false;
}
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
btnNught.setPressed(true);
//Set whatever color you want to set
}
else
{
btnNught.setPressed(false);
}
return true;
}
});
Following line will play the click sound:
btnNught.playSoundEffect(SoundEffectConstants.CLICK);
There are two ways to listen sound
1 use inbuilt sound
Go to Settings -> Sound ->
And do the following settings
sound profile set **Normal**
Touch sound set **ON**
use custom sound
play the user defined sound

Categories

Resources